From bf1f743382a0cbe2fd9663518202b53c4e51b719 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Thu, 9 Mar 2023 21:39:08 -0600 Subject: [PATCH] Factor out some helpers --- src/lib/routes/helpers.ts | 21 +++++++++++++++++++++ src/view/shell/BottomBar.tsx | 29 ++++++++--------------------- src/view/shell/Drawer.tsx | 29 ++++++++--------------------- 3 files changed, 37 insertions(+), 42 deletions(-) create mode 100644 src/lib/routes/helpers.ts diff --git a/src/lib/routes/helpers.ts b/src/lib/routes/helpers.ts new file mode 100644 index 0000000000..98c7f03f08 --- /dev/null +++ b/src/lib/routes/helpers.ts @@ -0,0 +1,21 @@ +import {State} from './types' + +export function getCurrentRoute(state: State) { + let node = state.routes[state.index] + while (node.state?.routes && typeof node.state?.index === 'number') { + node = node.state?.routes[node.state?.index] + } + return node +} + +export function isTab(current: string, route: string) { + // NOTE + // our tab routes can be variously referenced by 3 different names + // this helper deals with that weirdness + // -prf + return ( + current === route || + current === `${route}Stack` || + current === `${route}Inner` + ) +} diff --git a/src/view/shell/BottomBar.tsx b/src/view/shell/BottomBar.tsx index 5d9d1c4932..21be1c3c3d 100644 --- a/src/view/shell/BottomBar.tsx +++ b/src/view/shell/BottomBar.tsx @@ -26,15 +26,7 @@ import { } from 'lib/icons' import {colors} from 'lib/styles' import {usePalette} from 'lib/hooks/usePalette' - -// TODO move to lib -function currentRoute(state) { - let node = state.routes[state.index] - while (node.state?.routes && typeof node.state?.index === 'number') { - node = node.state?.routes[node.state?.index] - } - return node -} +import {getCurrentRoute, isTab} from 'lib/routes/helpers' export const BottomBar = observer(({navigation}: BottomTabBarProps) => { const store = useStores() @@ -68,10 +60,10 @@ export const BottomBar = observer(({navigation}: BottomTabBarProps) => { (tab: string) => { track(`MobileShell:${tab}ButtonPressed`) const state = navigation.getState() - const curr = currentRoute(state).name - if (curr === tab || curr === `${tab}Stack` || curr === `${tab}Inner`) { + const currentRoute = getCurrentRoute(state) + if (isTab(currentRoute.name, tab)) { store.emitScreenSoftReset() - } else if (state.routes[state.index].name === `${tab}Stack`) { + } else if (isTab(state.routes[state.index].name, tab)) { navigation.dispatch(StackActions.popToTop()) } else { navigation.navigate(`${tab}Stack`) @@ -93,15 +85,10 @@ export const BottomBar = observer(({navigation}: BottomTabBarProps) => { navigation.navigate('Profile', {name: store.me.handle}) }, [navigation, track, store.me.handle]) - const curr = currentRoute(navigation.getState()).name - const isAtHome = - curr === 'HomeStack' || curr === 'Home' || curr === 'HomeInner' - const isAtSearch = - curr === 'SearchStack' || curr === 'Search' || curr === 'SearchInner' - const isAtNotifications = - curr === 'NotificationsStack' || - curr === 'Notifications' || - curr === 'NotificationsInner' + const currentRoute = getCurrentRoute(navigation.getState()) + const isAtHome = isTab(currentRoute.name, 'Home') + const isAtSearch = isTab(currentRoute.name, 'Search') + const isAtNotifications = isTab(currentRoute.name, 'Notifications') return ( { const theme = useTheme() @@ -68,10 +60,10 @@ export const Drawer = observer(({navigation}: DrawerContentComponentProps) => { track('Menu:ItemClicked', {url: tab}) const state = navigation.getState() navigation.closeDrawer() - const curr = currentRoute(state).name - if (curr === tab || curr === `${tab}Stack`) { + const currentRoute = getCurrentRoute(state) + if (isTab(currentRoute.name, tab)) { store.emitScreenSoftReset() - } else if (state.routes[state.index].name === `${tab}Stack`) { + } else if (isTab(state.routes[state.index].name, tab)) { navigation.dispatch(StackActions.popToTop()) } else { // wait for drawer anim to finish @@ -154,15 +146,10 @@ export const Drawer = observer(({navigation}: DrawerContentComponentProps) => { store.shell.setDarkMode(!store.shell.darkMode) } - const curr = currentRoute(navigation.getState()).name - const isAtHome = - curr === 'HomeStack' || curr === 'Home' || curr === 'HomeInner' - const isAtSearch = - curr === 'SearchStack' || curr === 'Search' || curr === 'SearchInner' - const isAtNotifications = - curr === 'NotificationsStack' || - curr === 'Notifications' || - curr === 'NotificationsInner' + const currentRoute = getCurrentRoute(navigation.getState()) + const isAtHome = isTab(currentRoute.name, 'Home') + const isAtSearch = isTab(currentRoute.name, 'Search') + const isAtNotifications = isTab(currentRoute.name, 'Notifications') return (