From b3d3f4a6984d70f99ba373b83241e7405b3f35f2 Mon Sep 17 00:00:00 2001 From: "Pujan Shah (via MelvinBot)" Date: Thu, 5 Nov 2026 12:51:11 +0000 Subject: [PATCH 1/3] Fix workspace scope when returning from Wallet spend rules Co-authored-by: Pujan Shah --- src/libs/Navigation/Navigation.ts | 20 ++++++++++++++++---- tests/navigation/GoBackTests.tsx | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/libs/Navigation/Navigation.ts b/src/libs/Navigation/Navigation.ts index 74c3dbf9d582..4d7071baa748 100644 --- a/src/libs/Navigation/Navigation.ts +++ b/src/libs/Navigation/Navigation.ts @@ -38,6 +38,7 @@ import type {NavigationPartialRoute, NavigationRef, NavigationRoute, NavigationS import {consumePreMountReplaceOutcome, getFocusedRouteFromNavigatorState, getPreInsertedOriginalTabRoute} from './AppNavigator/createRootStackNavigator/GetStateForActionHandlers'; import getInitialSplitNavigatorState from './AppNavigator/createSplitNavigator/getInitialSplitNavigatorState'; +import {getPreservedNavigatorState} from './AppNavigator/createSplitNavigator/usePreserveNavigatorState'; import originalCloseRHPFlow from './helpers/closeRHPFlow'; import getActiveRoute from './helpers/getActiveRoute'; import getActiveTabName from './helpers/getActiveTabName'; @@ -589,6 +590,7 @@ 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))) { @@ -596,15 +598,25 @@ function goUp(backToRoute: Route, options?: GoBackOptions): boolean { } // 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; + // jumpTo reuses the mounted split, so another workspace would keep the old sidebar. navigate creates a split with the requested scope instead. + if (activeSplit && splitPayload && isNamedActionPayload(splitPayload) && hasDifferentSplitScope(activeSplit, splitPayload)) { + navigate(backToRoute); return true; } dispatch({ ...TabActions.jumpTo(payload.name, jumpParams), - target: targetState.key, + target: destinationTabState.key, }); return true; } diff --git a/tests/navigation/GoBackTests.tsx b/tests/navigation/GoBackTests.tsx index 3b13e6868ff2..37c6149fa815 100644 --- a/tests/navigation/GoBackTests.tsx +++ b/tests/navigation/GoBackTests.tsx @@ -791,6 +791,31 @@ 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(); + + // 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.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(); + 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}}); + }); + it('Should close the modal and reach the matching workspace split', () => { render(); const dispatchSpy = jest.spyOn(requireNavigationContainer(), 'dispatch'); From 8fdca93fe5303404b1ddf64ce0f23bf399fc5fad Mon Sep 17 00:00:00 2001 From: "Pujan Shah (via MelvinBot)" Date: Thu, 12 Nov 2026 15:14:19 +0000 Subject: [PATCH 2/3] Keep cross-workspace back redirects out of forward history Co-authored-by: Pujan Shah --- src/libs/Navigation/Navigation.ts | 5 +++-- tests/navigation/GoBackTests.tsx | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/libs/Navigation/Navigation.ts b/src/libs/Navigation/Navigation.ts index cd7c9dd460c8..36a097caec3f 100644 --- a/src/libs/Navigation/Navigation.ts +++ b/src/libs/Navigation/Navigation.ts @@ -610,9 +610,10 @@ function goUp(backToRoute: Route, options?: GoBackOptions): boolean { 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; - // jumpTo reuses the mounted split, so another workspace would keep the old sidebar. navigate creates a split with the requested scope instead. + // 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 (activeSplit && splitPayload && isNamedActionPayload(splitPayload) && hasDifferentSplitScope(activeSplit, splitPayload)) { - navigate(backToRoute); + dispatch({...action, type: CONST.NAVIGATION.ACTION_TYPE.REPLACE, target: navigationContainer.getRootState().key}); return true; } dispatch({ diff --git a/tests/navigation/GoBackTests.tsx b/tests/navigation/GoBackTests.tsx index 37c6149fa815..6d0ab725bb31 100644 --- a/tests/navigation/GoBackTests.tsx +++ b/tests/navigation/GoBackTests.tsx @@ -799,6 +799,7 @@ describe('Go back on the narrow layout', () => { ? {index: 2, routes: [...buildWorkspaceNavigationState(buildWorkspaceSplitRoute(policyB)).routes, ...accountState.routes]} : accountState; render(); + const dispatchSpy = jest.spyOn(requireNavigationContainer(), 'dispatch'); // When a card belonging to workspace A redirects to its disabled feature act(() => { @@ -807,13 +808,25 @@ describe('Go back on the narrow layout', () => { // 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('Should close the modal and reach the matching workspace split', () => { From 827be331bb7e82057ccffa59af57b04e6049eb85 Mon Sep 17 00:00:00 2001 From: "Pujan Shah (via MelvinBot)" Date: Mon, 16 Nov 2026 20:33:53 +0000 Subject: [PATCH 3/3] Replace scoped back redirects when destination state is unknown Co-authored-by: Pujan Shah --- src/libs/Navigation/Navigation.ts | 15 ++++++- tests/navigation/GoBackTests.tsx | 67 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/libs/Navigation/Navigation.ts b/src/libs/Navigation/Navigation.ts index 36a097caec3f..97ca1a76f765 100644 --- a/src/libs/Navigation/Navigation.ts +++ b/src/libs/Navigation/Navigation.ts @@ -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'; @@ -43,6 +43,7 @@ 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'; @@ -610,9 +611,19 @@ function goUp(backToRoute: Route, options?: GoBackOptions): boolean { 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 (activeSplit && splitPayload && isNamedActionPayload(splitPayload) && hasDifferentSplitScope(activeSplit, splitPayload)) { + if (hasUnknownTargetScope || (activeSplit && splitPayload && isNamedActionPayload(splitPayload) && hasDifferentSplitScope(activeSplit, splitPayload))) { dispatch({...action, type: CONST.NAVIGATION.ACTION_TYPE.REPLACE, target: navigationContainer.getRootState().key}); return true; } diff --git a/tests/navigation/GoBackTests.tsx b/tests/navigation/GoBackTests.tsx index 6d0ab725bb31..b5c969d0c54f 100644 --- a/tests/navigation/GoBackTests.tsx +++ b/tests/navigation/GoBackTests.tsx @@ -829,6 +829,73 @@ describe('Go back on the narrow layout', () => { 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(); + 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(); + 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(); + 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(); const dispatchSpy = jest.spyOn(requireNavigationContainer(), 'dispatch');