Skip to content
Merged
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
37 changes: 31 additions & 6 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,22 +591,47 @@ 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))) {
// Without a target, the deepest focused stack (e.g. Settings) would take the pop.
if (!dispatch({...StackActions.pop(topRootIndex - underlyingTabNavIndex), target: rootState.key})) {
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;
}

// jumpTo with params keeps the split the tab shows, so a split of another workspace or domain would keep its
// sidebar. Replace it inside the tab instead, keeping the routes under it.
const destinationTabIndex = destinationTabState.routes.findIndex((route) => route.name === payload.name);
const destinationTabStack = destinationTabIndex === -1 ? undefined : destinationTabState.routes.at(destinationTabIndex)?.state;
// Only a mounted tab has a split to replace.
if (destinationTabStack?.key && destinationTabStack.stale === false) {
const {
action: splitAction,
targetState: splitTargetState,
isFocusedRouteInDifferentScope,
} = getMinimalAction(getNestedAction(minimalAction, destinationTabStack), destinationTabStack);
if (isFocusedRouteInDifferentScope && splitTargetState.key === destinationTabStack.key) {
// A guard kept the other split, so don't show the tab with it.
if (!dispatch({...splitAction, type: CONST.NAVIGATION.ACTION_TYPE.REPLACE, target: destinationTabStack.key})) {
return true;
}
if (destinationTabState.index !== destinationTabIndex) {
dispatch({...TabActions.jumpTo(payload.name), target: destinationTabState.key});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this caused an issue #103751 which only swaps the params of an already-mounted tab's focused split, so the requested workspace's page rendered beside the previously-open workspace's sidebar

}
return true;
}
return true;
}
dispatch({
...TabActions.jumpTo(payload.name, jumpParams),
target: targetState.key,
target: destinationTabState.key,
});
return true;
}
Expand Down
97 changes: 95 additions & 2 deletions tests/navigation/GoBackTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import SCREENS from '@src/SCREENS';

import type {EventArg, InitialState, NavigationAction} from '@react-navigation/native';

import {TabActions} from '@react-navigation/native';
import React, {createContext, useContext} from 'react';

import requireNavigationContainer from '../utils/requireNavigationContainer';
Expand Down Expand Up @@ -60,13 +61,18 @@ function buildDomainSplitRoute(domainAccountID: number, centralScreen: string =

type WorkspaceScopeRoute = ReturnType<typeof buildWorkspaceSplitRoute> | ReturnType<typeof buildDomainSplitRoute>;

const WORKSPACES_LIST_ROUTE = {name: SCREENS.WORKSPACES_LIST};

type WorkspaceStackRoute = WorkspaceScopeRoute | typeof WORKSPACES_LIST_ROUTE;

const ACCOUNT_TAB_INDEX = 3;
const WORKSPACES_TAB_INDEX = 4;

function buildWorkspaceNavigationState(...workspaceSplits: WorkspaceScopeRoute[]): InitialState {
function buildWorkspaceNavigationState(...workspaceSplits: WorkspaceStackRoute[]): InitialState {
return buildWorkspaceNavigationStateWithActiveTab(WORKSPACES_TAB_INDEX, ...workspaceSplits);
}

function buildWorkspaceNavigationStateWithActiveTab(activeTabIndex: number, ...workspaceSplits: WorkspaceScopeRoute[]): InitialState {
function buildWorkspaceNavigationStateWithActiveTab(activeTabIndex: number, ...workspaceSplits: WorkspaceStackRoute[]): InitialState {
return {
index: 0,
routes: [
Expand Down Expand Up @@ -1411,6 +1417,93 @@ describe('Go back on the wide layout', () => {
});
});

describe.each([true, false])('Go back across tabs into the Workspaces tab with the narrow layout: %s', (isNarrowLayout) => {
const policyA = 'policy-a';
const policyB = 'policy-b';
const rightModalRoute = {name: NAVIGATORS.RIGHT_MODAL_NAVIGATOR, state: {index: 0, routes: [{name: SCREENS.RIGHT_MODAL.SETTINGS}]}};

beforeEach(() => {
mockedGetIsNarrowLayout.mockReturnValue(isNarrowLayout);
mockedUseResponsiveLayout.mockReturnValue({
...CONST.NAVIGATION_TESTS.DEFAULT_USE_RESPONSIVE_LAYOUT_VALUE,
shouldUseNarrowLayout: isNarrowLayout,
isSmallScreenWidth: isNarrowLayout,
isLargeScreenWidth: !isNarrowLayout,
});
});

/** Workspaces tab with `workspaceRoutes`, then an RHP over the Account tab, like Wallet spend rules */
function renderRhpOverAccountTab(hasUnderlyingTabNavigator: boolean, ...workspaceRoutes: WorkspaceStackRoute[]) {
const workspacesTabState = buildWorkspaceNavigationState(...workspaceRoutes);
if (hasUnderlyingTabNavigator) {
const routes = [...workspacesTabState.routes, ...buildWorkspaceNavigationStateWithActiveTab(ACCOUNT_TAB_INDEX, ...workspaceRoutes).routes, rightModalRoute];
render(<TestNavigationContainer initialState={{index: routes.length - 1, routes}} />);
return;
}

render(<TestNavigationContainer initialState={{index: 1, routes: [...workspacesTabState.routes, rightModalRoute]}} />);
// Switching tabs keeps the visited Workspaces tab mounted
act(() => {
navigationRef.dispatch({...TabActions.jumpTo(NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR), target: navigationRef.getRootState().routes.at(0)?.state?.key});
});
}

/** Route names of the Workspaces stack, with each split's policyID */
function getWorkspacesStackSummary() {
return getActiveWorkspaceState()?.routes.map((route) => {
const sidebarParams = route.state?.routes.at(0)?.params as {policyID?: string} | undefined;
return sidebarParams?.policyID ? `${route.name}:${sidebarParams.policyID}` : route.name;
});
}

function getFocusedTabName() {
const rootState = navigationRef.getRootState();
const tabState = rootState.routes.at(rootState.index)?.state;
return tabState?.routes.at(tabState.index ?? 0)?.name;
}

it.each([false, true])('Should show the sidebar of the requested workspace, with an underlying tab navigator: %s', (hasUnderlyingTabNavigator) => {
// Given the Workspaces tab shows workspace B over the list
renderRhpOverAccountTab(hasUnderlyingTabNavigator, WORKSPACES_LIST_ROUTE, buildWorkspaceSplitRoute(policyB));

// When the RHP redirects to More features of workspace A
act(() => {
Navigation.goBack(ROUTES.WORKSPACE_MORE_FEATURES.getRoute(policyA));
});

// Then A's page doesn't show next to B's sidebar (issue #103751)
expect(navigationRef.getRootState().routes.map((route) => route.name)).toEqual([NAVIGATORS.TAB_NAVIGATOR]);
expect(getFocusedTabName()).toBe(NAVIGATORS.WORKSPACE_NAVIGATOR);
expect(getWorkspacesStackSummary()).toEqual([SCREENS.WORKSPACES_LIST, `${NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR}:${policyA}`]);
const activeSplitState = getActiveWorkspaceState()?.routes.at(-1)?.state;
expect(activeSplitState?.routes.at(-1)).toMatchObject({name: SCREENS.WORKSPACE.MORE_FEATURES, params: {policyID: policyA}});

// When the user goes back
act(() => {
Navigation.goBack();
});

// Then they stay in Workspaces and B doesn't come back
expect(getFocusedTabName()).toBe(NAVIGATORS.WORKSPACE_NAVIGATOR);
expect(getWorkspacesStackSummary()?.at(0)).toBe(SCREENS.WORKSPACES_LIST);
expect(getWorkspacesStackSummary()).not.toContain(`${NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR}:${policyB}`);
});

it('Should open the requested workspace over the list when the Workspaces tab shows only the list', () => {
// Given the Workspaces tab shows only the list
renderRhpOverAccountTab(false, WORKSPACES_LIST_ROUTE);

// When the RHP redirects to More features of workspace A
act(() => {
Navigation.goBack(ROUTES.WORKSPACE_MORE_FEATURES.getRoute(policyA));
});

// Then A opens over the list instead of replacing it
expect(getFocusedTabName()).toBe(NAVIGATORS.WORKSPACE_NAVIGATOR);
expect(getWorkspacesStackSummary()).toEqual([SCREENS.WORKSPACES_LIST, `${NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR}:${policyA}`]);
});
});

describe('Go back with nothing to pop', () => {
beforeEach(() => {
mockedGetIsNarrowLayout.mockReturnValue(false);
Expand Down
Loading