Skip to content
Merged
1 change: 0 additions & 1 deletion contributingGuides/NAVIGATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,6 @@ A dynamic route is a URL suffix (e.g. `verify-account`) that can be appended to
Do not use dynamic routes when:
- Your use case falls under the [current limitations](#current-limitations-work-in-progress):
- You need to stack multiple dynamic route suffixes (e.g. `/a/verify-account/another-flow`).
- Your suffix requires multiple path segments (e.g. `step-one/details`).
- Your suffix includes path or query parameters (e.g. `verify-account/:id` or `verify-account?tab=details`).
- The screen has a single, fixed entry and a fixed back destination. In this case, use a normal static route instead.

Expand Down
17 changes: 11 additions & 6 deletions src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,23 @@ type DynamicRoutes = Record<string, DynamicRouteConfig>;
* Use for: verification flows, confirmations, multi-entry workflows
* Avoid for: regular navigation, single-entry workflows
*
* WIP - DO NOT USE FOR NEW ROUTES
*/
const DYNAMIC_ROUTES = {
VERIFY_ACCOUNT: {
path: 'verify-account',
entryScreens: [SCREENS.SETTINGS.WALLET.ROOT],
},
ADD_BANK_ACCOUNT_VERIFY_ACCOUNT: {
path: 'add-bank-account/verify-account',
entryScreens: [
SCREENS.SETTINGS.WALLET.ROOT,
SCREENS.RIGHT_MODAL.SEARCH_REPORT,
SCREENS.RIGHT_MODAL.EXPENSE_REPORT,
SCREENS.REPORT,
SCREENS.RIGHT_MODAL.SEARCH_MONEY_REQUEST_REPORT,
SCREENS.SEARCH.ROOT,
],
},
OWNER_SELECTOR: {
path: 'owner-selector',
entryScreens: [],
Expand Down Expand Up @@ -436,11 +446,6 @@ const ROUTES = {
return getUrlWithBackToParam(`settings/wallet/add-bank-account/${subPage}${action ? `/${action}` : ''}`, backTo);
},
},
SETTINGS_ADD_BANK_ACCOUNT_VERIFY_ACCOUNT: {
route: `settings/wallet/add-bank-account/${VERIFY_ACCOUNT}`,
getRoute: (params: {backTo?: string}) => getUrlWithParams(`settings/wallet/add-bank-account/${VERIFY_ACCOUNT}`, params),
},

SETTINGS_ADD_US_BANK_ACCOUNT: 'settings/wallet/add-us-bank-account',
SETTINGS_ADD_US_BANK_ACCOUNT_ENTRY_POINT: 'settings/wallet/add-us-bank-account/entry-point',
SETTINGS_ADD_BANK_ACCOUNT_SELECT_COUNTRY_VERIFY_ACCOUNT: `settings/wallet/add-bank-account/select-country/${VERIFY_ACCOUNT}`,
Expand Down
2 changes: 1 addition & 1 deletion src/SCREENS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ const SCREENS = {
APP_DOWNLOAD_LINKS: 'Settings_App_Download_Links',
ADD_DEBIT_CARD: 'Settings_Add_Debit_Card',
ADD_PAYMENT_CARD_CHANGE_CURRENCY: 'Settings_Add_Payment_Card_Change_Currency',
ADD_BANK_ACCOUNT_VERIFY_ACCOUNT: 'Settings_Add_Bank_Account_Verify_Account',
ADD_BANK_ACCOUNT: 'Settings_Add_Bank_Account',
ADD_US_BANK_ACCOUNT: 'Settings_Add_US_Bank_Account',
ADD_US_BANK_ACCOUNT_ENTRY_POINT: 'Settings_Add_US_Bank_Account_Entry_Point',
Expand All @@ -120,6 +119,7 @@ const SCREENS = {
REPORT_CARD_LOST_OR_DAMAGED_CONFIRM_MAGIC_CODE: 'Settings_ReportCardLostOrDamaged_ConfirmMagicCode',
TROUBLESHOOT: 'Settings_Troubleshoot',
DYNAMIC_VERIFY_ACCOUNT: 'Dynamic_Verify_Account',
DYNAMIC_ADD_BANK_ACCOUNT_VERIFY_ACCOUNT: 'Dynamic_Add_Bank_Account_Verify_Account',

PROFILE: {
ROOT: 'Settings_Profile',
Expand Down
8 changes: 3 additions & 5 deletions src/hooks/useDynamicBackPath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import getPathFromState from '@libs/Navigation/helpers/getPathFromState';
import getPathWithoutDynamicSuffix from '@libs/Navigation/helpers/getPathWithoutDynamicSuffix';
import splitPathAndQuery from '@libs/Navigation/helpers/splitPathAndQuery';
import type {State} from '@libs/Navigation/types';
import type {DynamicRouteSuffix, Route} from '@src/ROUTES';
Expand Down Expand Up @@ -27,13 +28,10 @@ function useDynamicBackPath(dynamicRouteSuffix: DynamicRouteSuffix): Route {
// Remove leading slashes for consistent processing
const pathWithoutLeadingSlash = path.replace(/^\/+/, '');

const [normalizedPath, query] = splitPathAndQuery(pathWithoutLeadingSlash);
const [normalizedPath] = splitPathAndQuery(pathWithoutLeadingSlash);

if (normalizedPath?.endsWith(`/${dynamicRouteSuffix}`)) {
const backPathWithoutQuery = normalizedPath.slice(0, -(dynamicRouteSuffix.length + 1));
const backPath = `${backPathWithoutQuery}${query ? `?${query}` : ''}`;

return backPath as Route;
return getPathWithoutDynamicSuffix(pathWithoutLeadingSlash, dynamicRouteSuffix);
}

// If suffix is not the last segment, return the original path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ const NewTeachersUniteNavigator = createModalStackNavigator<TeachersUniteNavigat

const SettingsModalStackNavigator = createModalStackNavigator<SettingsNavigatorParamList>({
[SCREENS.SETTINGS.DYNAMIC_VERIFY_ACCOUNT]: () => require<ReactComponentModule>('../../../../pages/settings/DynamicVerifyAccountPage').default,
[SCREENS.SETTINGS.DYNAMIC_ADD_BANK_ACCOUNT_VERIFY_ACCOUNT]: () => require<ReactComponentModule>('../../../../pages/settings/Wallet/DynamicAddBankAccountVerifyAccountPage').default,
[SCREENS.SETTINGS.SHARE_CODE]: () => require<ReactComponentModule>('../../../../pages/ShareCodePage').default,
[SCREENS.SETTINGS.PROFILE.PRONOUNS]: () => require<ReactComponentModule>('../../../../pages/settings/Profile/PronounsPage').default,
[SCREENS.SETTINGS.PROFILE.DISPLAY_NAME]: () => require<ReactComponentModule>('../../../../pages/settings/Profile/DisplayNamePage').default,
Expand Down Expand Up @@ -436,7 +437,6 @@ const SettingsModalStackNavigator = createModalStackNavigator<SettingsNavigatorP
[SCREENS.SETTINGS.WALLET.PERSONAL_CARD_WARNING]: () => require<ReactComponentModule>('../../../../pages/settings/Wallet/PersonalCards/PersonalCardWarning').default,
[SCREENS.SETTINGS.WALLET.PERSONAL_CARD_UPGRADE]: () => require<ReactComponentModule>('../../../../pages/settings/Wallet/PersonalCards/upgrade/PersonalCardUpgradePage').default,
[SCREENS.SETTINGS.ADD_DEBIT_CARD]: () => require<ReactComponentModule>('../../../../pages/settings/Wallet/AddDebitCardPage').default,
[SCREENS.SETTINGS.ADD_BANK_ACCOUNT_VERIFY_ACCOUNT]: () => require<ReactComponentModule>('../../../../pages/settings/Wallet/NewBankAccountVerifyAccountPage').default,
[SCREENS.SETTINGS.ADD_BANK_ACCOUNT]: () => require<ReactComponentModule>('../../../../pages/settings/Wallet/InternationalDepositAccount').default,
[SCREENS.SETTINGS.ADD_US_BANK_ACCOUNT]: () => require<ReactComponentModule>('../../../../pages/AddPersonalBankAccountPage').default,
[SCREENS.SETTINGS.ADD_US_BANK_ACCOUNT_ENTRY_POINT]: () =>
Expand Down
31 changes: 31 additions & 0 deletions src/libs/Navigation/helpers/findMatchingDynamicSuffix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import isDynamicRouteSuffix from './isDynamicRouteSuffix';
import splitPathAndQuery from './splitPathAndQuery';

/**
* Finds a registered dynamic route suffix that matches the end of the given path.
* Iterates path sub-suffixes from longest to shortest and checks each against a
* pre-built Set of registered dynamic paths, ensuring the longest match wins
* when overlapping suffixes exist (e.g. "address/country" vs "country").
*
* @param path - The path to find the matching dynamic suffix for
* @returns The matching dynamic suffix, or undefined if no matching suffix is found
*/
function findMatchingDynamicSuffix(path = ''): string | undefined {
const [normalizedPath] = splitPathAndQuery(path);
if (!normalizedPath) {
return undefined;
}

const segments = normalizedPath.split('/').filter(Boolean);

for (let i = 0; i < segments.length; i++) {
const candidate = segments.slice(i).join('/');
if (isDynamicRouteSuffix(candidate)) {
return candidate;
}
}

return undefined;
}

export default findMatchingDynamicSuffix;
17 changes: 10 additions & 7 deletions src/libs/Navigation/helpers/getAdaptedStateFromPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import NAVIGATORS from '@src/NAVIGATORS';
import type {Route as RoutePath} from '@src/ROUTES';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import getLastSuffixFromPath from './getLastSuffixFromPath';
import findMatchingDynamicSuffix from './findMatchingDynamicSuffix';
import getMatchingNewRoute from './getMatchingNewRoute';
import getParamsFromRoute from './getParamsFromRoute';
import getPathWithoutDynamicSuffix from './getPathWithoutDynamicSuffix';
import getRedirectedPath from './getRedirectedPath';
import getStateFromPath from './getStateFromPath';
import isDynamicRouteSuffix from './isDynamicRouteSuffix';
import {isFullScreenName} from './isNavigatorName';
import normalizePath from './normalizePath';
import replacePathInNestedState from './replacePathInNestedState';
Expand Down Expand Up @@ -199,13 +199,16 @@ function getMatchingFullScreenRoute(route: NavigationPartialRoute) {

// Handle dynamic routes: find the appropriate full screen route
if (route.path) {
const dynamicRouteSuffix = getLastSuffixFromPath(route.path);
if (isDynamicRouteSuffix(dynamicRouteSuffix)) {
// Remove dynamic suffix to get the base path
const pathWithoutDynamicSuffix = route.path?.replace(`/${dynamicRouteSuffix}`, '');
const dynamicRouteSuffix = findMatchingDynamicSuffix(route.path);
if (dynamicRouteSuffix) {
const pathWithoutDynamicSuffix = getPathWithoutDynamicSuffix(route.path, dynamicRouteSuffix);

if (!pathWithoutDynamicSuffix) {
return undefined;
}

// Get navigation state for the base path without dynamic suffix
const stateUnderDynamicRoute = getStateFromPath(pathWithoutDynamicSuffix as RoutePath);
const stateUnderDynamicRoute = getStateFromPath(pathWithoutDynamicSuffix);
const lastRoute = stateUnderDynamicRoute?.routes.at(-1);

if (!stateUnderDynamicRoute || !lastRoute || lastRoute.name === SCREENS.NOT_FOUND) {
Expand Down
23 changes: 0 additions & 23 deletions src/libs/Navigation/helpers/getLastSuffixFromPath.ts

This file was deleted.

5 changes: 1 addition & 4 deletions src/libs/Navigation/helpers/getPathFromState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import {findFocusedRoute, getPathFromState as RNGetPathFromState} from '@react-n
import type {NavigationState, PartialState} from '@react-navigation/routers';
import {linkingConfig} from '@libs/Navigation/linkingConfig';
import {normalizedConfigs} from '@libs/Navigation/linkingConfig/config';
import {DYNAMIC_ROUTES} from '@src/ROUTES';
import type {DynamicRouteSuffix} from '@src/ROUTES';
import type {Screen} from '@src/SCREENS';
import {dynamicRoutePaths} from './isDynamicRouteSuffix';

type State = NavigationState | Omit<PartialState<NavigationState>, 'stale'>;

const dynamicRouteEntries = Object.values(DYNAMIC_ROUTES);
const dynamicRoutePaths = new Set(dynamicRouteEntries.map(({path}) => path));

/**
* Checks if a screen name is a dynamic route screen
*/
Expand Down
22 changes: 22 additions & 0 deletions src/libs/Navigation/helpers/getPathWithoutDynamicSuffix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type {Route} from '@src/ROUTES';
import splitPathAndQuery from './splitPathAndQuery';

/**
* Returns the path without a dynamic route suffix while preserving query parameters.
*
* @param fullPath - The full URL path possibly containing query params (e.g., '/settings/wallet/verify-account?param=value')
* @param dynamicSuffix - The dynamic suffix to strip (e.g., 'verify-account')
* @returns The path without the suffix and with query params re-appended
*/
function getPathWithoutDynamicSuffix(fullPath: string, dynamicSuffix: string): Route {
const [pathWithoutQuery, query] = splitPathAndQuery(fullPath);
const pathWithoutDynamicSuffix = pathWithoutQuery?.slice(0, -(dynamicSuffix.length + 1)) ?? '';

if (!pathWithoutDynamicSuffix || pathWithoutDynamicSuffix === '/') {
return '';
}

return `${pathWithoutDynamicSuffix}${query ? `?${query}` : ''}` as Route;
}

export default getPathWithoutDynamicSuffix;
14 changes: 7 additions & 7 deletions src/libs/Navigation/helpers/getStateFromPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import type {Route} from '@src/ROUTES';
import {DYNAMIC_ROUTES} from '@src/ROUTES';
import type {Screen} from '@src/SCREENS';
import SCREENS from '@src/SCREENS';
import getLastSuffixFromPath from './getLastSuffixFromPath';
import findMatchingDynamicSuffix from './findMatchingDynamicSuffix';
import getMatchingNewRoute from './getMatchingNewRoute';
import getPathWithoutDynamicSuffix from './getPathWithoutDynamicSuffix';
import getRedirectedPath from './getRedirectedPath';
import getStateForDynamicRoute from './getStateForDynamicRoute';
import isDynamicRouteSuffix from './isDynamicRouteSuffix';

/**
* @param path - The path to parse
Expand All @@ -21,9 +21,9 @@ function getStateFromPath(path: Route): PartialState<NavigationState> {
const redirectedPath = getRedirectedPath(normalizedPath);
const normalizedPathAfterRedirection = getMatchingNewRoute(redirectedPath) ?? redirectedPath;

const dynamicRouteSuffix = getLastSuffixFromPath(normalizedPathAfterRedirection);
if (isDynamicRouteSuffix(dynamicRouteSuffix)) {
const pathWithoutDynamicSuffix = normalizedPathAfterRedirection.replace(`/${dynamicRouteSuffix}`, '');
const dynamicRouteSuffix = findMatchingDynamicSuffix(normalizedPathAfterRedirection);
if (dynamicRouteSuffix) {
const pathWithoutDynamicSuffix = getPathWithoutDynamicSuffix(normalizedPathAfterRedirection, dynamicRouteSuffix);

type DynamicRouteKey = keyof typeof DYNAMIC_ROUTES;
const dynamicRouteKeys = Object.keys(DYNAMIC_ROUTES) as DynamicRouteKey[];
Expand All @@ -32,7 +32,7 @@ function getStateFromPath(path: Route): PartialState<NavigationState> {
const dynamicRoute: string = dynamicRouteKeys.find((key) => DYNAMIC_ROUTES[key].path === dynamicRouteSuffix) ?? '';

// Get the currently focused route from the base path to check permissions
const focusedRoute = findFocusedRoute(getStateFromPath(pathWithoutDynamicSuffix as Route) ?? {});
const focusedRoute = findFocusedRoute(getStateFromPath(pathWithoutDynamicSuffix) ?? {});
const entryScreens: Screen[] = DYNAMIC_ROUTES[dynamicRoute as DynamicRouteKey]?.entryScreens ?? [];

// Check if the focused route is allowed to access this dynamic route
Expand All @@ -44,7 +44,7 @@ function getStateFromPath(path: Route): PartialState<NavigationState> {
}

// Fallback to not found page so users can't land on dynamic suffix directly.
if (pathWithoutDynamicSuffix === '/' || pathWithoutDynamicSuffix === '') {
if (!pathWithoutDynamicSuffix) {
const state = {routes: [{name: SCREENS.NOT_FOUND, path: normalizedPathAfterRedirection}]};

return state;
Expand Down
6 changes: 5 additions & 1 deletion src/libs/Navigation/helpers/isDynamicRouteSuffix.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {DYNAMIC_ROUTES} from '@src/ROUTES';
import type {DynamicRouteSuffix} from '@src/ROUTES';

const dynamicRouteEntries = Object.values(DYNAMIC_ROUTES);
const dynamicRoutePaths = new Set<string>(dynamicRouteEntries.map((r) => r.path));

/**
* Checks if a suffix matches any dynamic route path in DYNAMIC_ROUTES.
*/
function isDynamicRouteSuffix(suffix: string): suffix is DynamicRouteSuffix {
return Object.values(DYNAMIC_ROUTES).some((route) => route.path === suffix);
return dynamicRoutePaths.has(suffix);
}

export {dynamicRoutePaths};
export default isDynamicRouteSuffix;
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ const SETTINGS_TO_RHP: Partial<Record<keyof SettingsSplitNavigatorParamList, str
SCREENS.SETTINGS.BANK_ACCOUNT_PURPOSE,
SCREENS.SETTINGS.REPORT_CARD_LOST_OR_DAMAGED,
SCREENS.SETTINGS.REPORT_CARD_LOST_OR_DAMAGED_CONFIRM_MAGIC_CODE,
SCREENS.SETTINGS.ADD_BANK_ACCOUNT_VERIFY_ACCOUNT,
SCREENS.SETTINGS.WALLET.PERSONAL_CARD_WARNING,
SCREENS.SETTINGS.WALLET.PERSONAL_CARD_UPGRADE,
],
Expand Down
5 changes: 1 addition & 4 deletions src/libs/Navigation/linkingConfig/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,6 @@ const config: LinkingOptions<RootNavigatorParamList>['config'] = {
path: ROUTES.SETTINGS_ADD_BANK_ACCOUNT.route,
exact: true,
},
[SCREENS.SETTINGS.ADD_BANK_ACCOUNT_VERIFY_ACCOUNT]: {
path: ROUTES.SETTINGS_ADD_BANK_ACCOUNT_VERIFY_ACCOUNT.route,
exact: true,
},
[SCREENS.SETTINGS.ADD_US_BANK_ACCOUNT]: {
path: ROUTES.SETTINGS_ADD_US_BANK_ACCOUNT,
exact: true,
Expand Down Expand Up @@ -414,6 +410,7 @@ const config: LinkingOptions<RootNavigatorParamList>['config'] = {
exact: true,
},
[SCREENS.SETTINGS.DYNAMIC_VERIFY_ACCOUNT]: DYNAMIC_ROUTES.VERIFY_ACCOUNT.path,
[SCREENS.SETTINGS.DYNAMIC_ADD_BANK_ACCOUNT_VERIFY_ACCOUNT]: DYNAMIC_ROUTES.ADD_BANK_ACCOUNT_VERIFY_ACCOUNT.path,
[SCREENS.SETTINGS.PROFILE.CONTACT_METHODS]: {
path: ROUTES.SETTINGS_CONTACT_METHODS.route,
exact: true,
Expand Down
6 changes: 1 addition & 5 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ type SettingsNavigatorParamList = {
[SCREENS.SETTINGS.LOCK.UNLOCK_ACCOUNT]: undefined;
[SCREENS.SETTINGS.LOCK.FAILED_TO_LOCK_ACCOUNT]: undefined;
[SCREENS.SETTINGS.DYNAMIC_VERIFY_ACCOUNT]: undefined;
[SCREENS.SETTINGS.DYNAMIC_ADD_BANK_ACCOUNT_VERIFY_ACCOUNT]: undefined;
[SCREENS.SETTINGS.WALLET.CARDS_DIGITAL_DETAILS_UPDATE_ADDRESS]: undefined;
[SCREENS.SETTINGS.WALLET.DOMAIN_CARD]: {
/** cardID of selected card */
Expand Down Expand Up @@ -225,11 +226,6 @@ type SettingsNavigatorParamList = {
subPage?: string;
action?: 'edit';
};
[SCREENS.SETTINGS.ADD_BANK_ACCOUNT_VERIFY_ACCOUNT]: {
// TODO will be removed once dynamic routes are implemented https://github.com/Expensify/App/issues/73825
// eslint-disable-next-line no-restricted-syntax -- `backTo` usages in this file are legacy. Do not add new `backTo` params to screens. See contributingGuides/NAVIGATION.md
backTo?: Routes;
};
[SCREENS.SETTINGS.ADD_US_BANK_ACCOUNT]: undefined;
[SCREENS.SETTINGS.ADD_US_BANK_ACCOUNT_ENTRY_POINT]: undefined;
[SCREENS.SETTINGS.BANK_ACCOUNT_PURPOSE]: undefined;
Expand Down
5 changes: 3 additions & 2 deletions src/libs/actions/BankAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import type {SaveCorpayOnboardingCompanyDetails} from '@libs/API/parameters/Save
import type SaveCorpayOnboardingDirectorInformationParams from '@libs/API/parameters/SaveCorpayOnboardingDirectorInformationParams';
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import {getMicroSecondOnyxErrorWithTranslationKey} from '@libs/ErrorUtils';
import createDynamicRoute from '@libs/Navigation/helpers/createDynamicRoute';
import Navigation from '@libs/Navigation/Navigation';
import type {MemberForList} from '@libs/OptionsListUtils';
import {getFormattedStreet} from '@libs/PersonalDetailsUtils';
import CONST from '@src/CONST';
import type {Country} from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import ROUTES, {DYNAMIC_ROUTES} from '@src/ROUTES';
import type {Route} from '@src/ROUTES';
import type {InternationalBankAccountForm, PersonalBankAccountForm} from '@src/types/form';
import type {ACHContractStepProps, BeneficialOwnersStepProps, CompanyStepProps, ReimbursementAccountForm, RequestorStepProps} from '@src/types/form/ReimbursementAccountForm';
Expand Down Expand Up @@ -117,7 +118,7 @@ function openPersonalBankAccountSetupView({exitReportID, policyID, source, shoul
Onyx.merge(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {source});
}
if (!isUserValidated) {
Navigation.navigate(ROUTES.SETTINGS_ADD_BANK_ACCOUNT_VERIFY_ACCOUNT.getRoute({backTo: Navigation.getActiveRoute()}));
Navigation.navigate(createDynamicRoute(DYNAMIC_ROUTES.ADD_BANK_ACCOUNT_VERIFY_ACCOUNT.path));
return;
}
if (shouldSetUpUSBankAccount) {
Expand Down
Loading
Loading