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
4 changes: 2 additions & 2 deletions src/libs/Navigation/NavigationRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function NavigationRoot({authenticated, lastVisitedPath, initialUrl, onReady}: N
Navigation.navigate(ROUTES.MIGRATED_USER_WELCOME_MODAL.getRoute());
});

return getAdaptedStateFromPath(lastVisitedPath, linkingConfig.config);
return getAdaptedStateFromPath(lastVisitedPath);
}

if (!account || account.isFromPublicDomain) {
Expand All @@ -132,7 +132,7 @@ function NavigationRoot({authenticated, lastVisitedPath, initialUrl, onReady}: N

if (!isSpecificDeepLink) {
Log.info('Restoring last visited path on app startup', false, {lastVisitedPath, initialUrl, path});
return getAdaptedStateFromPath(lastVisitedPath, linkingConfig.config);
return getAdaptedStateFromPath(lastVisitedPath);
}
}

Expand Down
69 changes: 47 additions & 22 deletions src/libs/Navigation/helpers/getAdaptedStateFromPath.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,29 @@
import type {NavigationState, PartialState, Route} from '@react-navigation/native';
import {findFocusedRoute, getStateFromPath} from '@react-navigation/native';
import pick from 'lodash/pick';
import type {OnyxCollection} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import getInitialSplitNavigatorState from '@libs/Navigation/AppNavigator/createSplitNavigator/getInitialSplitNavigatorState';
import {config} from '@libs/Navigation/linkingConfig/config';
import {RHP_TO_DOMAIN, RHP_TO_HOME, RHP_TO_SEARCH, RHP_TO_SETTINGS, RHP_TO_SIDEBAR, RHP_TO_WORKSPACE, RHP_TO_WORKSPACES_LIST} from '@libs/Navigation/linkingConfig/RELATIONS';
import type {NavigationPartialRoute, RootNavigatorParamList} from '@libs/Navigation/types';
import {getReportOrDraftReport} from '@libs/ReportUtils';
import {getSearchParamFromPath} from '@libs/Url';
import type {NavigationState, PartialState, getStateFromPath as RNGetStateFromPath, Route} from '@react-navigation/native';
import {findFocusedRoute} from '@react-navigation/native';
import CONST from '@src/CONST';
import {getSearchParamFromPath} from '@src/libs/Url';
import NAVIGATORS from '@src/NAVIGATORS';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Route as RoutePath} from '@src/ROUTES';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import type {Report} from '@src/types/onyx';
import pick from 'lodash/pick';
import getLastSuffixFromPath from './getLastSuffixFromPath';
import getMatchingNewRoute from './getMatchingNewRoute';
import getParamsFromRoute from './getParamsFromRoute';
import getRedirectedPath from './getRedirectedPath';
import getStateFromPath from './getStateFromPath';
import isDynamicRouteSuffix from './isDynamicRouteSuffix';
import {isFullScreenName} from './isNavigatorName';
import normalizePath from './normalizePath';
import replacePathInNestedState from './replacePathInNestedState';

let allReports: OnyxCollection<Report>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
allReports = value;
},
});

type GetAdaptedStateReturnType = ReturnType<typeof getStateFromPath>;

type GetAdaptedStateFromPath = (...args: [...Parameters<typeof getStateFromPath>, shouldReplacePathInNestedState?: boolean]) => GetAdaptedStateReturnType;
type GetAdaptedStateFromPath = (...args: [...Parameters<typeof RNGetStateFromPath>, shouldReplacePathInNestedState?: boolean]) => GetAdaptedStateReturnType;

// The function getPathFromState that we are using in some places isn't working correctly without defined index.
const getRoutesWithIndex = (routes: NavigationPartialRoute[]): PartialState<NavigationState> => ({routes, index: routes.length - 1});
Expand Down Expand Up @@ -65,7 +56,7 @@ function getSearchScreenNameForRoute(route: NavigationPartialRoute): string {
function getMatchingFullScreenRoute(route: NavigationPartialRoute) {
// Check for backTo param. One screen with different backTo value may need different screens visible under the overlay.
if (isRouteWithBackToParam(route)) {
const stateForBackTo = getStateFromPath(route.params.backTo, config);
const stateForBackTo = getStateFromPath(route.params.backTo as RoutePath);

// This may happen if the backTo url is invalid.
const lastRoute = stateForBackTo?.routes.at(-1);
Expand All @@ -88,6 +79,39 @@ function getMatchingFullScreenRoute(route: NavigationPartialRoute) {
// If not, get the matching full screen route for the back to state.
return getMatchingFullScreenRoute(focusedStateForBackToRoute);
}

Comment thread
collectioneur marked this conversation as resolved.
Resolved
// 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}`, '');

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

if (!stateUnderDynamicRoute || !lastRoute || lastRoute.name === SCREENS.NOT_FOUND) {
return undefined;
}

const isLastRouteFullScreen = isFullScreenName(lastRoute.name);

if (isLastRouteFullScreen) {
return lastRoute;
}

const focusedStateForDynamicRoute = findFocusedRoute(stateUnderDynamicRoute);

if (!focusedStateForDynamicRoute) {
return undefined;
}

// Recursively find the matching full screen route for the focused dynamic route
return getMatchingFullScreenRoute(focusedStateForDynamicRoute);
}
}

const routeNameForLookup = getSearchScreenNameForRoute(route);
if (RHP_TO_SEARCH[routeNameForLookup]) {
const paramsFromRoute = getParamsFromRoute(RHP_TO_SEARCH[routeNameForLookup]);
Expand Down Expand Up @@ -187,7 +211,8 @@ function getDefaultFullScreenRoute(route?: NavigationPartialRoute) {
if (route && isRouteWithReportID(route)) {
const reportID = route.params.reportID;

if (!allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]?.reportID) {
// Only allReports should be checked here
if (!getReportOrDraftReport(reportID, undefined, undefined, {})) {
return {name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR};
}

Expand Down Expand Up @@ -297,7 +322,7 @@ const getAdaptedStateFromPath: GetAdaptedStateFromPath = (path, options, shouldR
normalizedPath = '/';
}

const state = getStateFromPath(normalizedPath, options) as PartialState<NavigationState<RootNavigatorParamList>>;
const state = getStateFromPath(normalizedPath as RoutePath) as PartialState<NavigationState<RootNavigatorParamList>>;
if (shouldReplacePathInNestedState) {
replacePathInNestedState(state, normalizedPath);
}
Expand Down
21 changes: 21 additions & 0 deletions src/libs/Navigation/helpers/getLastSuffixFromPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Extracts the last segment from a URL path, removing query parameters and trailing slashes.
*
* @param path - The URL path to extract the suffix from (can be undefined)
* @returns The last segment of the path as a string
*/
function getLastSuffixFromPath(path: string | undefined): string {
const pathWithoutParams = path?.split('?').at(0);
Comment thread
collectioneur marked this conversation as resolved.
Resolved

if (!pathWithoutParams) {
throw new Error('[getLastSuffixFromPath.ts] Failed to parse the path, path is empty');
}

const pathWithoutTrailingSlash = pathWithoutParams.endsWith('/') ? pathWithoutParams.slice(0, -1) : pathWithoutParams;

const lastSuffix = pathWithoutTrailingSlash.split('/').pop() ?? '';
Comment thread
collectioneur marked this conversation as resolved.
Resolved

return lastSuffix;
}

export default getLastSuffixFromPath;
67 changes: 67 additions & 0 deletions src/libs/Navigation/helpers/getStateForDynamicRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {normalizedConfigs} from '@libs/Navigation/linkingConfig/config';
import {DYNAMIC_ROUTES} from '@src/ROUTES';
import type {DynamicRouteSuffix} from '@src/ROUTES';

type LeafRoute = {
name: string;
path: string;
};

type NestedRoute = {
name: string;
state: {
routes: [RouteNode];
index: 0;
};
};

type RouteNode = LeafRoute | NestedRoute;

function getRouteNamesForDynamicRoute(dynamicRouteName: DynamicRouteSuffix): string[] | null {
// Search through normalized configs to find matching path and extract navigation hierarchy
// routeNames contains the sequence of screen/navigator names that should be present in the navigation state
for (const [, config] of Object.entries(normalizedConfigs)) {
if (config.path === dynamicRouteName) {
return config.routeNames;
}
}

return null;
}

function getStateForDynamicRoute(path: string, dynamicRouteName: keyof typeof DYNAMIC_ROUTES) {
const routeConfig = getRouteNamesForDynamicRoute(DYNAMIC_ROUTES[dynamicRouteName].path);

if (!routeConfig) {
throw new Error(`No route configuration found for dynamic route '${dynamicRouteName}'`);
}

// Build navigation state by creating nested structure
const buildNestedState = (routes: string[], currentIndex: number): RouteNode => {
const currentRoute = routes.at(currentIndex);
Comment thread
collectioneur marked this conversation as resolved.
Resolved

// If this is the last route, create leaf node with path
if (currentIndex === routes.length - 1) {
return {
name: currentRoute ?? '',
Comment thread
collectioneur marked this conversation as resolved.
Resolved
path,
};
}

// Create intermediate node with nested state
return {
name: currentRoute ?? '',
Comment thread
collectioneur marked this conversation as resolved.
Resolved
state: {
routes: [buildNestedState(routes, currentIndex + 1)],
Comment thread
collectioneur marked this conversation as resolved.
Resolved
index: 0,
},
};
};

// Start building from the first route
const rootRoute = {routes: [buildNestedState(routeConfig, 0)]};

return rootRoute;
}

export default getStateForDynamicRoute;
42 changes: 41 additions & 1 deletion src/libs/Navigation/helpers/getStateFromPath.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import type {NavigationState, PartialState} from '@react-navigation/native';
import {getStateFromPath as RNGetStateFromPath} from '@react-navigation/native';
import {findFocusedRoute, getStateFromPath as RNGetStateFromPath} from '@react-navigation/native';
import Log from '@libs/Log';
import {linkingConfig} from '@libs/Navigation/linkingConfig';
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 getMatchingNewRoute from './getMatchingNewRoute';
import getRedirectedPath from './getRedirectedPath';
import getStateForDynamicRoute from './getStateForDynamicRoute';
import isDynamicRouteSuffix from './isDynamicRouteSuffix';

/**
* @param path - The path to parse
Expand All @@ -14,6 +21,39 @@ 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}`, '');

type DynamicRouteKey = keyof typeof DYNAMIC_ROUTES;

// Find the dynamic route key that matches the extracted suffix
const dynamicRoute: string = Object.keys(DYNAMIC_ROUTES).find((key) => DYNAMIC_ROUTES[key as DynamicRouteKey].path === dynamicRouteSuffix) ?? '';

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

// Check if the focused route is allowed to access this dynamic route
if (focusedRoute?.name) {
if (entryScreens.includes(focusedRoute.name as Screen)) {
// Generate navigation state for the dynamic route
const dynamicRouteState = getStateForDynamicRoute(normalizedPath, dynamicRoute as DynamicRouteKey);
return dynamicRouteState;
}

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

return state;
}

// Log an error to quickly identify and add forgotten screens to the Dynamic Routes configuration
Log.warn(`[getStateFromPath.ts][DynamicRoute] Focused route ${focusedRoute.name} is not allowed to access dynamic route with suffix ${dynamicRouteSuffix}`);
}
}

// This function is used in the linkTo function where we want to use default getStateFromPath function.
const state = RNGetStateFromPath(normalizedPathAfterRedirection, linkingConfig.config);

Expand Down
3 changes: 2 additions & 1 deletion src/libs/actions/Welcome/OnboardingFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import CONST from '@src/CONST';
import IntlStore from '@src/languages/IntlStore';
import NAVIGATORS from '@src/NAVIGATORS';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Route} from '@src/ROUTES';
import ROUTES from '@src/ROUTES';
import {hasCompletedGuidedSetupFlowSelector} from '@src/selectors/Onboarding';
import type {Locale, Onboarding} from '@src/types/onyx';
Expand Down Expand Up @@ -82,7 +83,7 @@ Onyx.connectWithoutView({
*/
function startOnboardingFlow(startOnboardingFlowParams: GetOnboardingInitialPathParamsType) {
const currentRoute = navigationRef.getCurrentRoute();
const adaptedState = getAdaptedStateFromPath(getOnboardingInitialPath(startOnboardingFlowParams), linkingConfig.config, false);
const adaptedState = getAdaptedStateFromPath(getOnboardingInitialPath(startOnboardingFlowParams) as Route, linkingConfig.config, false);
const focusedRoute = findFocusedRoute(adaptedState as PartialState<NavigationState<RootNavigatorParamList>>);
if (focusedRoute?.name === currentRoute?.name) {
return;
Expand Down
Loading