Skip to content

Commit

Permalink
remember state between tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgrzybowski committed Sep 20, 2024
1 parent 174a4eb commit 70534a6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/libs/Navigation/newLinkTo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function shouldDispatchAction(currentState: NavigationState<RootStackParamList>,
return true;
}

export default function linkTo(navigation: NavigationContainerRef<RootStackParamList> | null, path: Route) {
export default function linkTo(navigation: NavigationContainerRef<RootStackParamList> | 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?");
}
Expand All @@ -49,7 +49,9 @@ export default function linkTo(navigation: NavigationContainerRef<RootStackParam
return;
}

if (action.type === CONST.NAVIGATION.ACTION_TYPE.NAVIGATE) {
if (type === CONST.NAVIGATION.ACTION_TYPE.REPLACE) {
action.type = CONST.NAVIGATION.ACTION_TYPE.REPLACE;
} else if (action.type === CONST.NAVIGATION.ACTION_TYPE.NAVIGATE) {
// We want to PUSH by default to add entries to the browser history.
action.type = CONST.NAVIGATION.ACTION_TYPE.PUSH;
}
Expand Down
36 changes: 34 additions & 2 deletions src/pages/home/sidebar/BottomTabAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import useLocalize from '@hooks/useLocalize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import interceptAnonymousUser from '@libs/interceptAnonymousUser';
import Navigation from '@libs/Navigation/Navigation';
import Navigation, {navigationRef} from '@libs/Navigation/Navigation';
import type {AuthScreensParamList} from '@libs/Navigation/types';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
Expand Down Expand Up @@ -51,7 +53,37 @@ function BottomTabAvatar({isCreateMenuOpen = false, isSelected = false}: BottomT
return;
}

interceptAnonymousUser(() => 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;
Expand Down
18 changes: 14 additions & 4 deletions src/pages/workspace/WorkspaceInitialPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -76,7 +77,7 @@ type WorkspaceInitialPageOnyxProps = {
policyCategories: OnyxEntry<OnyxTypes.PolicyCategories>;
};

type WorkspaceInitialPageProps = WithPolicyAndFullscreenLoadingProps & WorkspaceInitialPageOnyxProps & StackScreenProps<WorkspaceNavigatorParamList, typeof SCREENS.WORKSPACE.INITIAL>;
type WorkspaceInitialPageProps = WithPolicyAndFullscreenLoadingProps & WorkspaceInitialPageOnyxProps & StackScreenProps<WorkspaceSplitNavigatorParamList, typeof SCREENS.WORKSPACE.INITIAL>;

type PolicyFeatureStates = Record<PolicyFeatureName, boolean>;

Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit 70534a6

Please sign in to comment.