Factor out some helpers
This commit is contained in:
@@ -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`
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -26,15 +26,7 @@ import {
|
|||||||
} from 'lib/icons'
|
} from 'lib/icons'
|
||||||
import {colors} from 'lib/styles'
|
import {colors} from 'lib/styles'
|
||||||
import {usePalette} from 'lib/hooks/usePalette'
|
import {usePalette} from 'lib/hooks/usePalette'
|
||||||
|
import {getCurrentRoute, isTab} from 'lib/routes/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
|
|
||||||
}
|
|
||||||
|
|
||||||
export const BottomBar = observer(({navigation}: BottomTabBarProps) => {
|
export const BottomBar = observer(({navigation}: BottomTabBarProps) => {
|
||||||
const store = useStores()
|
const store = useStores()
|
||||||
@@ -68,10 +60,10 @@ export const BottomBar = observer(({navigation}: BottomTabBarProps) => {
|
|||||||
(tab: string) => {
|
(tab: string) => {
|
||||||
track(`MobileShell:${tab}ButtonPressed`)
|
track(`MobileShell:${tab}ButtonPressed`)
|
||||||
const state = navigation.getState()
|
const state = navigation.getState()
|
||||||
const curr = currentRoute(state).name
|
const currentRoute = getCurrentRoute(state)
|
||||||
if (curr === tab || curr === `${tab}Stack` || curr === `${tab}Inner`) {
|
if (isTab(currentRoute.name, tab)) {
|
||||||
store.emitScreenSoftReset()
|
store.emitScreenSoftReset()
|
||||||
} else if (state.routes[state.index].name === `${tab}Stack`) {
|
} else if (isTab(state.routes[state.index].name, tab)) {
|
||||||
navigation.dispatch(StackActions.popToTop())
|
navigation.dispatch(StackActions.popToTop())
|
||||||
} else {
|
} else {
|
||||||
navigation.navigate(`${tab}Stack`)
|
navigation.navigate(`${tab}Stack`)
|
||||||
@@ -93,15 +85,10 @@ export const BottomBar = observer(({navigation}: BottomTabBarProps) => {
|
|||||||
navigation.navigate('Profile', {name: store.me.handle})
|
navigation.navigate('Profile', {name: store.me.handle})
|
||||||
}, [navigation, track, store.me.handle])
|
}, [navigation, track, store.me.handle])
|
||||||
|
|
||||||
const curr = currentRoute(navigation.getState()).name
|
const currentRoute = getCurrentRoute(navigation.getState())
|
||||||
const isAtHome =
|
const isAtHome = isTab(currentRoute.name, 'Home')
|
||||||
curr === 'HomeStack' || curr === 'Home' || curr === 'HomeInner'
|
const isAtSearch = isTab(currentRoute.name, 'Search')
|
||||||
const isAtSearch =
|
const isAtNotifications = isTab(currentRoute.name, 'Notifications')
|
||||||
curr === 'SearchStack' || curr === 'Search' || curr === 'SearchInner'
|
|
||||||
const isAtNotifications =
|
|
||||||
curr === 'NotificationsStack' ||
|
|
||||||
curr === 'Notifications' ||
|
|
||||||
curr === 'NotificationsInner'
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Animated.View
|
<Animated.View
|
||||||
|
|||||||
@@ -38,15 +38,7 @@ import {useTheme} from 'lib/ThemeContext'
|
|||||||
import {usePalette} from 'lib/hooks/usePalette'
|
import {usePalette} from 'lib/hooks/usePalette'
|
||||||
import {useAnalytics} from 'lib/analytics'
|
import {useAnalytics} from 'lib/analytics'
|
||||||
import {pluralize} from 'lib/strings/helpers'
|
import {pluralize} from 'lib/strings/helpers'
|
||||||
|
import {getCurrentRoute, isTab} from 'lib/routes/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
|
|
||||||
}
|
|
||||||
|
|
||||||
export const Drawer = observer(({navigation}: DrawerContentComponentProps) => {
|
export const Drawer = observer(({navigation}: DrawerContentComponentProps) => {
|
||||||
const theme = useTheme()
|
const theme = useTheme()
|
||||||
@@ -68,10 +60,10 @@ export const Drawer = observer(({navigation}: DrawerContentComponentProps) => {
|
|||||||
track('Menu:ItemClicked', {url: tab})
|
track('Menu:ItemClicked', {url: tab})
|
||||||
const state = navigation.getState()
|
const state = navigation.getState()
|
||||||
navigation.closeDrawer()
|
navigation.closeDrawer()
|
||||||
const curr = currentRoute(state).name
|
const currentRoute = getCurrentRoute(state)
|
||||||
if (curr === tab || curr === `${tab}Stack`) {
|
if (isTab(currentRoute.name, tab)) {
|
||||||
store.emitScreenSoftReset()
|
store.emitScreenSoftReset()
|
||||||
} else if (state.routes[state.index].name === `${tab}Stack`) {
|
} else if (isTab(state.routes[state.index].name, tab)) {
|
||||||
navigation.dispatch(StackActions.popToTop())
|
navigation.dispatch(StackActions.popToTop())
|
||||||
} else {
|
} else {
|
||||||
// wait for drawer anim to finish
|
// wait for drawer anim to finish
|
||||||
@@ -154,15 +146,10 @@ export const Drawer = observer(({navigation}: DrawerContentComponentProps) => {
|
|||||||
store.shell.setDarkMode(!store.shell.darkMode)
|
store.shell.setDarkMode(!store.shell.darkMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
const curr = currentRoute(navigation.getState()).name
|
const currentRoute = getCurrentRoute(navigation.getState())
|
||||||
const isAtHome =
|
const isAtHome = isTab(currentRoute.name, 'Home')
|
||||||
curr === 'HomeStack' || curr === 'Home' || curr === 'HomeInner'
|
const isAtSearch = isTab(currentRoute.name, 'Search')
|
||||||
const isAtSearch =
|
const isAtNotifications = isTab(currentRoute.name, 'Notifications')
|
||||||
curr === 'SearchStack' || curr === 'Search' || curr === 'SearchInner'
|
|
||||||
const isAtNotifications =
|
|
||||||
curr === 'NotificationsStack' ||
|
|
||||||
curr === 'Notifications' ||
|
|
||||||
curr === 'NotificationsInner'
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View
|
<View
|
||||||
|
|||||||
Reference in New Issue
Block a user