From d4d1fb820ba2976ca0bfc18384a8767dae9dbe66 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Fri, 10 Mar 2023 15:25:01 -0600 Subject: [PATCH] Handle all navigations from the state layer --- src/lib/notifee.ts | 6 ++---- src/lib/routes/helpers.ts | 16 +--------------- src/lib/routes/types.ts | 2 +- src/routes.tsx | 20 +++++++++++++++++++- src/state/models/root-store.ts | 5 +++-- src/view/com/modals/DeleteAccount.tsx | 3 ++- 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/lib/notifee.ts b/src/lib/notifee.ts index 02b647694b..5c05081db0 100644 --- a/src/lib/notifee.ts +++ b/src/lib/notifee.ts @@ -1,10 +1,9 @@ import notifee, {EventType} from '@notifee/react-native' -import {StackActions} from '@react-navigation/native' import {AppBskyEmbedImages} from '@atproto/api' import {RootStoreModel} from 'state/models/root-store' import {NotificationsViewItemModel} from 'state/models/notifications-view' import {enforceLen} from 'lib/strings/helpers' -import {navigation} from '../Routes' +import {resetToTab} from '../Routes' export function init(store: RootStoreModel) { store.onUnreadNotifications(count => notifee.setBadgeCount(count)) @@ -17,8 +16,7 @@ export function init(store: RootStoreModel) { store.log.debug('Notifee foreground event', {type}) if (type === EventType.PRESS) { store.log.debug('User pressed a notifee, opening notifications') - navigation.navigate('NotificationsTab') - navigation.dispatch(StackActions.popToTop()) + resetToTab('NotificationsTab') } }) notifee.onBackgroundEvent(async _e => {}) // notifee requires this but we handle it with onForegroundEvent diff --git a/src/lib/routes/helpers.ts b/src/lib/routes/helpers.ts index 013569aa65..c8df8d79a7 100644 --- a/src/lib/routes/helpers.ts +++ b/src/lib/routes/helpers.ts @@ -1,18 +1,4 @@ -import {State, NavigationProp} from './types' - -// TODO needed? -// export function getCurrentTabName( -// navigator: NavigationProp | undefined, -// ): string { -// if (!navigator) { -// throw new Error('Failed to get current tab') -// } -// const state = navigator.getState() -// if (state.type !== 'tab') { -// return getCurrentTabName(navigator.getParent()) -// } -// return state.routes[state.index].name -// } +import {State} from './types' export function getCurrentRoute(state: State) { let node = state.routes[state.index || 0] diff --git a/src/lib/routes/types.ts b/src/lib/routes/types.ts index e5b62d8731..6867bff262 100644 --- a/src/lib/routes/types.ts +++ b/src/lib/routes/types.ts @@ -43,7 +43,7 @@ export type NotificationsDrawerNavigatorParams = { export type AllNavigatorParams = CommonNavigatorParams & { HomeTab: undefined Home: undefined - SeachTab: undefined + SearchTab: undefined Search: undefined NotificationsTab: undefined Notifications: undefined diff --git a/src/routes.tsx b/src/routes.tsx index 196d2b2643..8970c9cfe2 100644 --- a/src/routes.tsx +++ b/src/routes.tsx @@ -2,6 +2,7 @@ import * as React from 'react' import { NavigationContainer, createNavigationContainerRef, + StackActions, } from '@react-navigation/native' import {createNativeStackNavigator} from '@react-navigation/native-stack' import {createDrawerNavigator} from '@react-navigation/drawer' @@ -277,4 +278,21 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) { ) } -export {navigationRef as navigation, matchPath, TabsNavigator, RoutesContainer} +function navigate( + name: K, + params?: AllNavigatorParams[K], +) { + if (navigationRef.isReady()) { + // @ts-ignore I dont know what would make typescript happy but I have a life -prf + navigationRef.navigate(name, params) + } +} + +function resetToTab(tabName: 'HomeTab' | 'SearchTab' | 'NotificationsTab') { + if (navigationRef.isReady()) { + navigate(tabName) + navigationRef.dispatch(StackActions.popToTop()) + } +} + +export {navigate, resetToTab, matchPath, TabsNavigator, RoutesContainer} diff --git a/src/state/models/root-store.ts b/src/state/models/root-store.ts index 54be125d8a..88b690ebdb 100644 --- a/src/state/models/root-store.ts +++ b/src/state/models/root-store.ts @@ -16,6 +16,7 @@ import {ProfilesViewModel} from './profiles-view' import {LinkMetasViewModel} from './link-metas-view' import {NotificationsViewItemModel} from './notifications-view' import {MeModel} from './me' +import {resetToTab} from '../../Routes' export const appInfo = z.object({ build: z.string(), @@ -138,7 +139,7 @@ export class RootStoreModel { */ async handleSessionDrop() { this.log.debug('RootStoreModel:handleSessionDrop') - // this.nav.clear() TODO + resetToTab('HomeTab') this.me.clear() this.emitSessionDropped() } @@ -149,7 +150,7 @@ export class RootStoreModel { clearAllSessionState() { this.log.debug('RootStoreModel:clearAllSessionState') this.session.clear() - // this.nav.clear() TODO + resetToTab('HomeTab') this.me.clear() } diff --git a/src/view/com/modals/DeleteAccount.tsx b/src/view/com/modals/DeleteAccount.tsx index 45348d44c6..ee739c1504 100644 --- a/src/view/com/modals/DeleteAccount.tsx +++ b/src/view/com/modals/DeleteAccount.tsx @@ -14,6 +14,7 @@ import {s, colors, gradients} from 'lib/styles' import {usePalette} from 'lib/hooks/usePalette' import {ErrorMessage} from '../util/error/ErrorMessage' import {cleanError} from 'lib/strings/errors' +import {resetToTab} from '../../../Routes' export const snapPoints = ['60%'] @@ -46,7 +47,7 @@ export function Component({}: {}) { token: confirmCode, }) Toast.show('Your account has been deleted') - // store.nav.tab.fixedTabReset() TODO + resetToTab('HomeTab') store.session.clear() store.shell.closeModal() } catch (e: any) {