Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import {setupHadTabNavigation} from '@libs/hadTabNavigation';
import Log from '@libs/Log';
import {cancelSkipNextFocusRestore, skipNextFocusRestore} from '@libs/NavigationFocusReturn';
import {filterObject, shallowCompare} from '@libs/ObjectUtils';
import {filterObject, isRecord, shallowCompare} from '@libs/ObjectUtils';
import {holdPendingSearchWriteFlush, releasePendingSearchWriteFlush} from '@libs/pendingSearchWrite';
import {getSpan, startSpan} from '@libs/telemetry/activeSpans';

Expand Down Expand Up @@ -38,11 +38,12 @@ import type {NavigationPartialRoute, NavigationRef, NavigationRoute, NavigationS

import {consumePreMountReplaceOutcome, getFocusedRouteFromNavigatorState, getPreInsertedOriginalTabRoute} from './AppNavigator/createRootStackNavigator/GetStateForActionHandlers';
import getInitialSplitNavigatorState from './AppNavigator/createSplitNavigator/getInitialSplitNavigatorState';
import {clearPreservedNavigatorStates} from './AppNavigator/createSplitNavigator/usePreserveNavigatorState';
import {clearPreservedNavigatorStates, getPreservedNavigatorState} from './AppNavigator/createSplitNavigator/usePreserveNavigatorState';
import originalCloseRHPFlow from './helpers/closeRHPFlow';
import getActiveRoute from './helpers/getActiveRoute';
import getActiveTabName from './helpers/getActiveTabName';
import getFocusedReportParams from './helpers/getFocusedReportParams';
import getParamsFromRoute from './helpers/getParamsFromRoute';
import getStateFromPath from './helpers/getStateFromPath';
import getTopmostReportParams from './helpers/getTopmostReportParams';
import hasDifferentSplitScope from './helpers/hasDifferentSplitScope';
Expand Down Expand Up @@ -591,22 +592,44 @@ function goUp(backToRoute: Route, options?: GoBackOptions): boolean {
(route, idx) => idx < topRootIndex && route.name === NAVIGATORS.TAB_NAVIGATOR && route.state?.routes?.at(route.state?.index ?? 0)?.name === payload.name,
);
const jumpParams = 'params' in payload ? payload.params : undefined;
let destinationTabState = targetState;
if (underlyingTabNavIndex !== -1) {
// A prevented pop leaves the tab navigator covered, so jumping in it would change the tab under the prompt.
if (!dispatch(StackActions.pop(topRootIndex - underlyingTabNavIndex))) {
return true;
}
// The uncovered tab navigator has the right tab active, but not necessarily the requested screen inside
// it, so the jump still has to be applied there rather than to the tab navigator that was popped.
const underlyingTabStateKey = rootState.routes.at(underlyingTabNavIndex)?.state?.key;
if (underlyingTabStateKey) {
dispatch({...TabActions.jumpTo(payload.name, jumpParams), target: underlyingTabStateKey});
const underlyingTabState = rootState.routes.at(underlyingTabNavIndex)?.state;
if (!underlyingTabState?.key) {
return true;
}
destinationTabState = underlyingTabState;
}

const destinationTabRoute = destinationTabState.routes.find((route) => route.name === payload.name);
const destinationStack = destinationTabRoute?.state ?? (destinationTabRoute?.key ? getPreservedNavigatorState(destinationTabRoute.key) : undefined);
const activeSplit = destinationStack?.routes.at(destinationStack.index ?? -1);
const splitPayload = destinationStack ? getNestedAction(minimalAction, destinationStack).payload : undefined;
// Browser Back can rebuild a tab without live or preserved state. Jumping would restore its last workspace,
// so an explicit scope must also replace the root when there is no split to compare it against.
const targetSplitName = isRecord(jumpParams) ? jumpParams.screen : undefined;
const targetSplitParams = isRecord(jumpParams) && isRecord(jumpParams.params) ? jumpParams.params.params : undefined;
const hasUnknownTargetScope =
!activeSplit &&
typeof targetSplitName === 'string' &&
isSplitNavigatorName(targetSplitName) &&
isRecord(targetSplitParams) &&
getParamsFromRoute(SPLIT_TO_SIDEBAR[targetSplitName]).some((param) => typeof targetSplitParams[param] === 'string' || typeof targetSplitParams[param] === 'number');
// jumpTo would keep the other workspace's sidebar. Replace at the root because tabs cannot handle REPLACE,
// and forward navigation would leave the redirect's origin in the back stack.
if (hasUnknownTargetScope || (activeSplit && splitPayload && isNamedActionPayload(splitPayload) && hasDifferentSplitScope(activeSplit, splitPayload))) {
dispatch({...action, type: CONST.NAVIGATION.ACTION_TYPE.REPLACE, target: navigationContainer.getRootState().key});
return true;
}
dispatch({
...TabActions.jumpTo(payload.name, jumpParams),
target: targetState.key,
target: destinationTabState.key,
});
return true;
}
Expand Down
105 changes: 105 additions & 0 deletions tests/navigation/GoBackTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,111 @@ describe('Go back on the narrow layout', () => {
};
}

it.each([true, false])('Should keep the sidebar and More features in the card workspace with an underlying workspace tab=%s', (hasUnderlyingWorkspaceTab) => {
// Given Wallet in the Account tab with a modal open, while Workspaces still holds workspace B
const accountTabIndex = 3;
const accountState = buildStateWithModalOverTab(accountTabIndex, buildWorkspaceSplitRoute(policyB));
const initialState: InitialState = hasUnderlyingWorkspaceTab
? {index: 2, routes: [...buildWorkspaceNavigationState(buildWorkspaceSplitRoute(policyB)).routes, ...accountState.routes]}
: accountState;
render(<TestNavigationContainer initialState={initialState} />);
const dispatchSpy = jest.spyOn(requireNavigationContainer(), 'dispatch');

// When a card belonging to workspace A redirects to its disabled feature
act(() => {
Navigation.goBack(ROUTES.WORKSPACE_MORE_FEATURES.getRoute(policyA));
});

// Then both panes belong to A rather than showing A's settings beside B's sidebar
const rootState = navigationRef.getRootState();
expect(rootState.routes).toHaveLength(1);
expect(rootState.routes.at(rootState.index)?.name).toBe(NAVIGATORS.TAB_NAVIGATOR);
const tabState = rootState.routes.at(rootState.index)?.state;
expect(tabState?.routes.at(tabState.index ?? 0)?.name).toBe(NAVIGATORS.WORKSPACE_NAVIGATOR);
const workspaceState = getActiveWorkspaceState();
expect(workspaceState?.routes).toHaveLength(1);
const activeSplit = workspaceState?.routes.at(workspaceState.index ?? 0);
expect(activeSplit?.state?.routes.at(0)).toMatchObject({name: SCREENS.WORKSPACE.INITIAL, params: {policyID: policyA}});
expect(activeSplit?.state?.routes.at(-1)).toMatchObject({name: SCREENS.WORKSPACE.MORE_FEATURES, params: {policyID: policyA}});
expect(dispatchSpy).not.toHaveBeenCalledWith(expect.objectContaining({type: CONST.NAVIGATION.ACTION_TYPE.PUSH}));

// Back must stay in A rather than reopening Account or the discarded workspace B split.
act(() => {
Navigation.goBack();
});
expect(navigationRef.getRootState().routes).toHaveLength(1);
const workspaceStateAfterBack = getActiveWorkspaceState();
expect(workspaceStateAfterBack?.routes).toHaveLength(1);
expect(workspaceStateAfterBack?.routes.at(0)?.state?.routes.at(0)).toMatchObject({name: SCREENS.WORKSPACE.INITIAL, params: {policyID: policyA}});
});

it.each([
{scope: 'workspace', route: ROUTES.WORKSPACE_MORE_FEATURES.getRoute(policyA), sidebar: SCREENS.WORKSPACE.INITIAL, params: {policyID: policyA}},
{scope: 'domain', route: ROUTES.DOMAIN_SAML.getRoute(1), sidebar: SCREENS.DOMAIN.INITIAL, params: {domainAccountID: '1'}},
])('Should replace an unknown destination scope for a $scope redirect', ({route, sidebar, params}) => {
// Given browser Back rebuilt an inactive Workspaces route without live or preserved nested state
const initialState = buildStateWithModalOverTab(3);
const workspaceRoute = initialState.routes.at(0)?.state?.routes.at(WORKSPACES_TAB_INDEX);
if (workspaceRoute) {
delete workspaceRoute.state;
}
render(<TestNavigationContainer initialState={initialState} />);
const rootBeforeRedirect = navigationRef.getRootState();
const destinationRoute = rootBeforeRedirect.routes.at(0)?.state?.routes.find((tabRoute) => tabRoute.name === NAVIGATORS.WORKSPACE_NAVIGATOR);
expect(destinationRoute).toBeDefined();
expect(destinationRoute?.state).toBeUndefined();
expect(getPreservedNavigatorState(destinationRoute?.key ?? '')).toBeUndefined();
const dispatchSpy = jest.spyOn(requireNavigationContainer(), 'dispatch');

// When a scoped fallback must open despite the missing destination state
act(() => {
Navigation.goBack(route);
});

// Then replacement prevents the tab from restoring a previously visited scope
expect(dispatchSpy).toHaveBeenCalledWith(expect.objectContaining({type: CONST.NAVIGATION.ACTION_TYPE.REPLACE, target: rootBeforeRedirect.key}));
expect(dispatchSpy).not.toHaveBeenCalledWith(expect.objectContaining({type: 'JUMP_TO'}));
expect(dispatchSpy).not.toHaveBeenCalledWith(expect.objectContaining({type: CONST.NAVIGATION.ACTION_TYPE.PUSH}));
expect(navigationRef.getRootState().routes).toHaveLength(1);
const activeSplit = getActiveWorkspaceState()?.routes.at(-1);
expect(activeSplit?.state?.routes.at(0)).toMatchObject({name: sidebar, params});
expect(activeSplit?.state?.routes.at(-1)?.params).toMatchObject(params);
});

it.each([ROUTES.WORKSPACES_LIST.getRoute(), ROUTES.SETTINGS_PROFILE.getRoute()])('Should jump to an unknown unscoped destination for %s', (route) => {
// Given only Home is mounted and the destination tab has no saved nested state
render(<TestNavigationContainer initialState={buildStateWithModalOverTab(0)} />);
const dispatchSpy = jest.spyOn(requireNavigationContainer(), 'dispatch');

// When the fallback does not request a scoped split
act(() => {
Navigation.goBack(route);
});

// Then missing state alone must not force root replacement
expect(dispatchSpy).toHaveBeenCalledWith(expect.objectContaining({type: 'JUMP_TO'}));
expect(dispatchSpy).not.toHaveBeenCalledWith(expect.objectContaining({type: CONST.NAVIGATION.ACTION_TYPE.REPLACE}));
});

it('Should jump to a known matching workspace scope without replacing the root', () => {
// Given Account covers a Workspaces tab that already holds the requested scope
const accountState = buildStateWithModalOverTab(3, buildWorkspaceSplitRoute(policyA));
const initialState: InitialState = {index: 2, routes: [...buildWorkspaceNavigationState(buildWorkspaceSplitRoute(policyA)).routes, ...accountState.routes]};
render(<TestNavigationContainer initialState={initialState} />);
const rootKey = navigationRef.getRootState().key;
const dispatchSpy = jest.spyOn(requireNavigationContainer(), 'dispatch');

// When the card redirects to that same workspace
act(() => {
Navigation.goBack(ROUTES.WORKSPACE_MORE_FEATURES.getRoute(policyA));
});

// Then tab switching preserves the existing navigator instead of replacing it
expect(dispatchSpy).toHaveBeenCalledWith(expect.objectContaining({type: 'JUMP_TO'}));
expect(dispatchSpy).not.toHaveBeenCalledWith(expect.objectContaining({type: CONST.NAVIGATION.ACTION_TYPE.REPLACE}));
expect(navigationRef.getRootState().key).toBe(rootKey);
});

it('Should close the modal and reach the matching workspace split', () => {
render(<TestNavigationContainer initialState={buildStateWithModalOverWorkspaces(buildWorkspaceSplitRoute(policyA), buildWorkspaceSplitRoute(policyB))} />);
const dispatchSpy = jest.spyOn(requireNavigationContainer(), 'dispatch');
Expand Down
Loading