From 70534a683265079c795cda85b6de1c302a8ef0f9 Mon Sep 17 00:00:00 2001 From: Adam Grzybowski Date: Fri, 20 Sep 2024 15:20:52 +0200 Subject: [PATCH] remember state between tabs --- src/libs/Navigation/newLinkTo/index.ts | 6 ++-- src/pages/home/sidebar/BottomTabAvatar.tsx | 36 ++++++++++++++++++-- src/pages/workspace/WorkspaceInitialPage.tsx | 18 +++++++--- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/libs/Navigation/newLinkTo/index.ts b/src/libs/Navigation/newLinkTo/index.ts index e19f02687f1c..f8ac74d6e61f 100644 --- a/src/libs/Navigation/newLinkTo/index.ts +++ b/src/libs/Navigation/newLinkTo/index.ts @@ -24,7 +24,7 @@ function shouldDispatchAction(currentState: NavigationState, return true; } -export default function linkTo(navigation: NavigationContainerRef | null, path: Route) { +export default function linkTo(navigation: NavigationContainerRef | null, path: Route, type?: typeof CONST.NAVIGATION.ACTION_TYPE.REPLACE) { if (!navigation) { throw new Error("Couldn't find a navigation object. Is your component inside a screen in a navigator?"); } @@ -49,7 +49,9 @@ export default function linkTo(navigation: NavigationContainerRef Navigation.navigate(ROUTES.SETTINGS)); + interceptAnonymousUser(() => { + const rootState = navigationRef.getRootState(); + const lastSettingsOrWorkspaceNavigatorRoute = rootState.routes.findLast( + (rootRoute) => rootRoute.name === NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR || rootRoute.name === NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR, + ); + + // If there is a workspace navigator route, then we should open the workspace initial screen as it should be "remembered". + if (lastSettingsOrWorkspaceNavigatorRoute?.name === NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR) { + const params = lastSettingsOrWorkspaceNavigatorRoute.params as AuthScreensParamList[typeof NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR]; + + // Screens of this navigator should always have policyID + if ('params' in params && params.params?.policyID) { + Navigation.navigate(ROUTES.WORKSPACE_INITIAL.getRoute(params.params.policyID)); + } + return; + } + + // If there is settings workspace screen in the settings navigator, then we should open the settings workspaces as it should be "remembered". + if ( + lastSettingsOrWorkspaceNavigatorRoute && + lastSettingsOrWorkspaceNavigatorRoute.state && + lastSettingsOrWorkspaceNavigatorRoute.state.routes.at(-1)?.name === SCREENS.SETTINGS.WORKSPACES + ) { + Navigation.navigate(ROUTES.SETTINGS_WORKSPACES); + return; + } + + // Otherwise we should simply open the settings navigator. + // This case also covers if there is no route to remember. + Navigation.navigate(ROUTES.SETTINGS); + }); }, [isCreateMenuOpen, shouldUseNarrowLayout, route.name]); let children; diff --git a/src/pages/workspace/WorkspaceInitialPage.tsx b/src/pages/workspace/WorkspaceInitialPage.tsx index ece2311b2d58..14c31e61bb5b 100644 --- a/src/pages/workspace/WorkspaceInitialPage.tsx +++ b/src/pages/workspace/WorkspaceInitialPage.tsx @@ -24,14 +24,15 @@ import useWaitForNavigation from '@hooks/useWaitForNavigation'; import {isConnectionInProgress} from '@libs/actions/connections'; import BottomTabBar from '@libs/Navigation/AppNavigator/createCustomBottomTabNavigator/BottomTabBar'; import getTopmostRouteName from '@libs/Navigation/getTopmostRouteName'; -import Navigation from '@libs/Navigation/Navigation'; +import Navigation, {navigationRef} from '@libs/Navigation/Navigation'; +import type {WorkspaceSplitNavigatorParamList} from '@libs/Navigation/types'; import * as PolicyUtils from '@libs/PolicyUtils'; import {getDefaultWorkspaceAvatar} from '@libs/ReportUtils'; -import type {WorkspaceNavigatorParamList} from '@navigation/types'; import * as Policy from '@userActions/Policy/Policy'; import * as ReimbursementAccount from '@userActions/ReimbursementAccount'; import CONST from '@src/CONST'; import type {TranslationPaths} from '@src/languages/types'; +import NAVIGATORS from '@src/NAVIGATORS'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; @@ -76,7 +77,7 @@ type WorkspaceInitialPageOnyxProps = { policyCategories: OnyxEntry; }; -type WorkspaceInitialPageProps = WithPolicyAndFullscreenLoadingProps & WorkspaceInitialPageOnyxProps & StackScreenProps; +type WorkspaceInitialPageProps = WithPolicyAndFullscreenLoadingProps & WorkspaceInitialPageOnyxProps & StackScreenProps; type PolicyFeatureStates = Record; @@ -370,7 +371,16 @@ function WorkspaceInitialPage({policyDraft, policy: policyProp, policyCategories Navigation.resetToHome(); Navigation.isNavigationReady().then(() => Navigation.navigate(route.params?.backTo as Route)); } else { - Navigation.dismissModal(); + // @TODO This part could be done with the new goBack method when it will be implemented. + const previousRoute = navigationRef.getRootState().routes.at(-2); + + // If there is the settings split navigator we can dismiss safely + if (previousRoute?.name === NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR) { + Navigation.dismissModal(); + } else { + // If not, we are going to replace this route with the settings route + Navigation.navigate(ROUTES.SETTINGS_WORKSPACES, CONST.NAVIGATION.ACTION_TYPE.REPLACE); + } } }} policyAvatar={policyAvatar}