Factor out some helpers

This commit is contained in:
Paul Frazee
2023-03-09 21:39:08 -06:00
parent de5ee3c768
commit bf1f743382
3 changed files with 37 additions and 42 deletions
+21
View File
@@ -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`
)
}
+8 -21
View File
@@ -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 (
<Animated.View
+8 -21
View File
@@ -38,15 +38,7 @@ import {useTheme} from 'lib/ThemeContext'
import {usePalette} from 'lib/hooks/usePalette'
import {useAnalytics} from 'lib/analytics'
import {pluralize} from 'lib/strings/helpers'
// 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 Drawer = observer(({navigation}: DrawerContentComponentProps) => {
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 (
<View