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
11 changes: 9 additions & 2 deletions src/components/SidePanel/RHPVariantTest/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SidePanelActions from '@libs/actions/SidePanel';
import isReportTopmostSplitNavigator from '@libs/Navigation/helpers/isReportTopmostSplitNavigator';
import Navigation from '@libs/Navigation/Navigation';

import CONST from '@src/CONST';
Expand Down Expand Up @@ -66,15 +67,21 @@ const shouldOpenRHPVariant: ShouldOpenRHPVariant = (variantOverride) => {
const handleRHPVariantNavigation: HandleRHPVariantNavigation = (onboardingPolicyID, variantOverride) => {
const variant = variantOverride ?? onboardingRHPVariant;
if (variant === CONST.ONBOARDING_RHP_VARIANT.TRACK_EXPENSES_WITH_CONCIERGE) {
Navigation.navigate(ROUTES.HOME);
const shouldPreserveRevealedReport = isReportTopmostSplitNavigator();
if (!shouldPreserveRevealedReport) {
Navigation.navigate(ROUTES.HOME);
}
SidePanelActions.openSidePanel(true);
return;
}

const isRHPHomePage = variant === CONST.ONBOARDING_RHP_VARIANT.RHP_HOME_PAGE;

if (isRHPHomePage) {
Navigation.navigate(ROUTES.HOME);
const shouldPreserveRevealedReport = isReportTopmostSplitNavigator();
if (!shouldPreserveRevealedReport) {
Navigation.navigate(ROUTES.HOME);
}
} else {
Navigation.navigate(ROUTES.WORKSPACE_OVERVIEW.getRoute(onboardingPolicyID));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ export {
handleToggleModalWithHistoryAction,
getPreInsertedOriginalTabRoute,
clearPreInsertedOriginalTabRoute,
MODAL_ROUTES_TO_DISMISS,
// Exported for unit-test access; not used outside of testing.
withSanitizedDeepLinkParams,
getTabStateWithFocusedTarget,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
handleToggleMfaModalNavigatorWithHistoryAction,
handleToggleModalWithHistoryAction,
handleToggleSidePanelWithHistoryAction,
MODAL_ROUTES_TO_DISMISS,
} from './GetStateForActionHandlers';
import syncBrowserHistory from './syncBrowserHistory';

Expand Down Expand Up @@ -76,6 +77,14 @@ function isPreloadAction(action: RootStackNavigatorAction): action is PreloadAct
return action.type === CONST.NAVIGATION.ACTION_TYPE.PRELOAD;
}

// Onboarding REDIRECT layers a modal on top of whatever was already on screen.
// Preserve the underlying fullscreen base rather than replacing the entire stack.
const MODAL_GUARD_REDIRECT_TARGETS = new Set<string>([NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR]);

function isModalGuardRedirectTarget(name: string | undefined): boolean {
return !!name && MODAL_GUARD_REDIRECT_TARGETS.has(name);
}

/**
* Evaluates navigation guards and handles BLOCK/REDIRECT results
*
Expand Down Expand Up @@ -106,6 +115,52 @@ function handleNavigationGuards(
return null;
}

const isModalGuardRedirect = redirectState.routes.some((r) => isModalGuardRedirectTarget(r.name));
const focusedRouteName = state.routes[state.index]?.name;
const redirectTargetName = redirectState.routes.at(-1)?.name;

// Skip re-applying a modal redirect whose target is already the focused route: re-running it
// produces a redundant state reset that re-triggers the guard - the same infinite navigation
// loop OnboardingGuard prevents (APP-7FR).
// Scoped to modal redirects only: a non-modal redirect like HOME must still apply even when
// HOME is already focused, so it can reset nested tab state.
if (isModalGuardRedirect && focusedRouteName && redirectTargetName && focusedRouteName === redirectTargetName) {
return state;
}

if (isModalGuardRedirect) {
// Drop dismissible-modal routes (RHP, SignIn modal, CONCIERGE, etc.) and anything
// above them so the new stack doesn't end up with two modals on top of
// each other - regression #86258 (two Expensify logos when SignIn RHP was still
// on top at REDIRECT time).
const firstDismissibleModalIndex = state.routes.findIndex((route) => MODAL_ROUTES_TO_DISMISS.has(route.name));
const cleanedRoutes = firstDismissibleModalIndex === -1 ? state.routes : state.routes.slice(0, firstDismissibleModalIndex);

const underlyingFullScreen = cleanedRoutes.findLast((r) => isFullScreenName(r.name));
const redirectModal = redirectState.routes.findLast((r) => isModalGuardRedirectTarget(r.name));

// Rebuild the stack as [fullscreen report, onboarding modal] so the deep-linked report
// stays underneath the onboarding modal instead of being replaced by Home. If no
// fullscreen route survived the cleanup above (e.g. opening `/concierge` from a
// force-closed app leaves the stack as just [CONCIERGE]), skip this and fall through to
// the unmodified redirect state below, which restores the Home baseline the `/concierge`
// flow relies on - replacing it caused regression #90303.
if (underlyingFullScreen && redirectModal) {
const redirectModalWithKey: StackNavigationState<ParamListBase>['routes'][number] = {
...redirectModal,
key: redirectModal.key ?? `${redirectModal.name}-modal-redirect`,
};
const modalResetState: StackNavigationState<ParamListBase> = {
...state,
index: 1,
routes: [underlyingFullScreen, redirectModalWithKey],
preloadedRoutes: [],
};
const modalResetAction = CommonActions.reset(modalResetState);
return stackRouter.getStateForAction(state, modalResetAction, configOptions);
}
}

const resetAction = CommonActions.reset({
index: redirectState.index ?? redirectState.routes.length - 1,
routes: redirectState.routes,
Expand Down
3 changes: 2 additions & 1 deletion src/libs/navigateAfterOnboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Onyx from 'react-native-onyx';
import {setDisableDismissOnEscape} from './actions/Modal';
import SidePanelActions from './actions/SidePanel';
import {setOnboardingRHPVariant} from './actions/Welcome';
import isReportTopmostSplitNavigator from './Navigation/helpers/isReportTopmostSplitNavigator';
import shouldOpenOnAdminRoom from './Navigation/helpers/shouldOpenOnAdminRoom';
import Navigation from './Navigation/Navigation';
import {findLastAccessedReport, isConciergeChatReport, isSelfDM} from './ReportUtils';
Expand Down Expand Up @@ -99,7 +100,7 @@ function navigateAfterOnboarding(
);
if (reportID) {
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(reportID));
} else {
} else if (!isReportTopmostSplitNavigator()) {
// Navigate to home to trigger guard evaluation
Navigation.navigate(ROUTES.HOME);
}
Expand Down
5 changes: 5 additions & 0 deletions src/pages/signin/SignInModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import useTheme from '@hooks/useTheme';

import {openApp} from '@libs/actions/App';
import {isMobileSafari} from '@libs/Browser';
import isReportTopmostSplitNavigator from '@libs/Navigation/helpers/isReportTopmostSplitNavigator';
import Navigation from '@libs/Navigation/Navigation';
import {waitForIdle} from '@libs/Network/SequentialQueue';

Expand Down Expand Up @@ -63,7 +64,11 @@ function SignInModal() {
return;
}

const shouldPreserveRevealedReport = isReportTopmostSplitNavigator();
Navigation.dismissModal();
if (shouldPreserveRevealedReport) {
return;
Comment thread
KJ21-ENG marked this conversation as resolved.
Resolved
}
Navigation.navigate(ROUTES.HOME);
}, [isLoadingApp]);

Expand Down
199 changes: 199 additions & 0 deletions tests/unit/Navigation/guards/handleNavigationGuardRedirect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import RootStackRouter from '@libs/Navigation/AppNavigator/createRootStackNavigator/RootStackRouter';
import {evaluateGuards} from '@libs/Navigation/guards';
import getAdaptedStateFromPath from '@libs/Navigation/helpers/getAdaptedStateFromPath';

import NAVIGATORS from '@src/NAVIGATORS';
import SCREENS from '@src/SCREENS';

import type {ParamListBase, StackNavigationState} from '@react-navigation/native';

jest.mock('@expensify/react-native-hybrid-app', () => ({
__esModule: true,
default: {
isHybridApp: jest.fn(() => false),
shouldUseStaging: jest.fn(),
closeReactNativeApp: jest.fn(),
completeOnboarding: jest.fn(),
switchAccount: jest.fn(),
sendAuthToken: jest.fn(),
getHybridAppSettings: jest.fn(() => Promise.resolve(null)),
getInitialURL: jest.fn(() => Promise.resolve(null)),
onURLListenerAdded: jest.fn(),
signInToOldDot: jest.fn(),
signOutFromOldDot: jest.fn(),
startSignOut: jest.fn(),
cancelSignOut: jest.fn(),
clearOldDotAfterSignOut: jest.fn(),
},
}));

jest.mock('@libs/Navigation/guards', () => ({
__esModule: true,
createGuardContext: jest.fn(() => ({
isAuthenticated: true,
isLoading: false,
currentUrl: '',
})),
evaluateGuards: jest.fn(() => ({type: 'ALLOW'})),
registerGuard: jest.fn(),
clearGuards: jest.fn(),
getRegisteredGuards: jest.fn(() => []),
}));

jest.mock('@libs/Navigation/helpers/getAdaptedStateFromPath', () => ({
__esModule: true,
default: jest.fn(),
}));

const mockedEvaluateGuards = jest.mocked(evaluateGuards);
const mockedGetAdaptedStateFromPath = jest.mocked(getAdaptedStateFromPath);

const routeNames = [
NAVIGATORS.TAB_NAVIGATOR,
NAVIGATORS.REPORTS_SPLIT_NAVIGATOR,
NAVIGATORS.RIGHT_MODAL_NAVIGATOR,
NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR,
NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR,
SCREENS.CONCIERGE,
SCREENS.HOME,
SCREENS.REPORT,
];

type StackRoute = StackNavigationState<ParamListBase>['routes'][number];

function createState(routes: StackRoute[], index = routes.length - 1): StackNavigationState<ParamListBase> {
return {
key: 'root',
index,
routeNames,
routes,
stale: false,
type: 'stack',
preloadedRoutes: [],
};
}

describe('handleNavigationGuards REDIRECT stack preservation', () => {
const router = RootStackRouter({});
const configOptions = {
routeNames,
routeParamList: {} as ParamListBase,
routeGetIdList: {} as Record<string, ((params: Record<string, unknown>) => string) | undefined>,
};
const action = {type: 'NAVIGATE' as const, payload: {name: NAVIGATORS.TAB_NAVIGATOR}};
const onboardingRedirectState = {
routes: [
{key: 'home', name: NAVIGATORS.TAB_NAVIGATOR},
{key: 'onboarding', name: NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR},
],
};

beforeEach(() => {
jest.clearAllMocks();
mockedEvaluateGuards.mockReturnValue({type: 'REDIRECT', route: 'onboarding/work-email'});
mockedGetAdaptedStateFromPath.mockReturnValue(onboardingRedirectState);
});

it('drops the SignIn right modal before preserving a deep-linked report under onboarding', () => {
const state = createState([
{key: 'report', name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR},
{key: 'sign-in-rhp', name: NAVIGATORS.RIGHT_MODAL_NAVIGATOR},
]);

const result = router.getStateForAction(state, action, configOptions);

expect(result?.routes.map((route) => route.name)).toEqual([NAVIGATORS.REPORTS_SPLIT_NAVIGATOR, NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR]);
expect(result?.index).toBe(1);
});

it('falls back to the default onboarding redirect state when no fullscreen route survives', () => {
const state = createState([{key: 'concierge', name: SCREENS.CONCIERGE}]);

const result = router.getStateForAction(state, action, configOptions);

expect(result?.routes.map((route) => route.name)).toEqual([NAVIGATORS.TAB_NAVIGATOR, NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR]);
expect(result?.index).toBe(1);
});

it('keeps non-onboarding redirects as full state replacements', () => {
const state = createState([{key: 'report', name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR}]);
mockedEvaluateGuards.mockReturnValue({type: 'REDIRECT', route: 'settings'});
mockedGetAdaptedStateFromPath.mockReturnValue({
routes: [{key: 'settings', name: NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR}],
});

const result = router.getStateForAction(state, action, configOptions);

expect(result?.routes.map((route) => route.name)).toEqual([NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR]);
expect(result?.index).toBe(0);
});

it('does not suppress HOME redirects when TabNavigator is already focused on another tab', () => {
const state = createState([
{
key: 'tabs',
name: NAVIGATORS.TAB_NAVIGATOR,
state: {
key: 'tabs-state',
index: 1,
routeNames: [SCREENS.HOME, NAVIGATORS.REPORTS_SPLIT_NAVIGATOR],
routes: [
{key: 'home', name: SCREENS.HOME},
{
key: 'reports',
name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR,
state: {
key: 'reports-state',
index: 0,
routeNames: [SCREENS.REPORT],
routes: [{key: 'report', name: SCREENS.REPORT, params: {reportID: '7075912447943023'}}],
stale: false,
type: 'stack',
},
},
],
stale: false,
type: 'tab',
},
},
]);
mockedEvaluateGuards.mockReturnValue({type: 'REDIRECT', route: 'home'});
mockedGetAdaptedStateFromPath.mockReturnValue({
routes: [
{
key: 'tabs-home',
name: NAVIGATORS.TAB_NAVIGATOR,
state: {
key: 'tabs-home-state',
index: 0,
routeNames: [SCREENS.HOME, NAVIGATORS.REPORTS_SPLIT_NAVIGATOR],
routes: [{key: 'home', name: SCREENS.HOME}],
type: 'tab',
},
},
],
});

const result = router.getStateForAction(state, action, configOptions);

expect(result).not.toBe(state);
expect(result?.routes).toHaveLength(1);
expect(result?.routes.at(0)?.name).toBe(NAVIGATORS.TAB_NAVIGATOR);
expect(result?.routes.at(0)?.state?.index).toBe(0);
expect(result?.routes.at(0)?.state?.routes.at(0)?.name).toBe(SCREENS.HOME);
});

it('returns the current state when the redirect target is already focused', () => {
const state = createState(
[
{key: 'home', name: NAVIGATORS.TAB_NAVIGATOR},
{key: 'onboarding', name: NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR},
],
1,
);

const result = router.getStateForAction(state, action, configOptions);

expect(result).toBe(state);
});
});
Loading
Loading