Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
71b90a8
fixed all console errors related to navigation initialization
collectioneur Sep 25, 2025
3893b2f
small cleanup
collectioneur Sep 26, 2025
255dc91
changed to eraly returns
collectioneur Sep 26, 2025
227c2b1
Merge branch 'main' into collectioneur/fix-navigation-console-errors
collectioneur Sep 26, 2025
bde4161
Merge branch 'main' into collectioneur/fix-navigation-console-errors
collectioneur Sep 29, 2025
31c1fcd
testing why tests are broken
collectioneur Sep 29, 2025
cc09dba
trying to avoid typecheck errors
collectioneur Sep 29, 2025
a558bdf
fixed tests
collectioneur Sep 29, 2025
bd5f272
added logs and empty state
collectioneur Sep 30, 2025
a3ba7dd
add names
collectioneur Sep 30, 2025
f5704cb
small fix
collectioneur Oct 1, 2025
9d239ed
small fix in log
collectioneur Oct 1, 2025
47432f2
Merge branch 'main' into collectioneur/fix-navigation-console-errors
collectioneur Oct 1, 2025
91bc8ba
changed the logic of handling navigationRef errors to simulate logic …
collectioneur Oct 3, 2025
aa05cbf
removed unneccessary test fixes
collectioneur Oct 6, 2025
1a3b898
safe handling when navigation isn't yet initialized
collectioneur Oct 6, 2025
5cf2119
changed to early return when handling undefined state in navigateWork…
collectioneur Oct 7, 2025
3e37214
changed early return value to false when state is not initialized
collectioneur Oct 7, 2025
b58a588
Merge branch 'main' into collectioneur/fix-navigation-console-errors
collectioneur Oct 7, 2025
89b57a8
added undefined state handling after merge
collectioneur Oct 7, 2025
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
5 changes: 5 additions & 0 deletions src/components/Search/SearchRouter/SearchRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
const textInputRef = useRef<AnimatedTextInputRef>(null);

const contextualReportID = useRootNavigationState((state) => {
// Safe handling when navigation is not yet initialized
if (!state) {
return undefined;
}

const focusedRoute = findFocusedRoute(state);
if (focusedRoute?.name === SCREENS.REPORT) {
// We're guaranteed that the type of params is of SCREENS.REPORT
Expand Down
9 changes: 9 additions & 0 deletions src/components/SidePanel/HelpComponents/HelpContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ function HelpContent({closeSidePanel}: HelpContentProps) {
const [expandedIndex, setExpandedIndex] = useState(0);

const {params, routeName, currentState} = useRootNavigationState((rootState) => {
// Safe handling when navigation is not yet initialized
if (!rootState) {
return {
routeName: '' as Screen,
params: {} as Record<string, string>,
currentState: undefined,
};
}

const focusedRoute = findFocusedRoute(rootState);
setExpandedIndex(0);
return {
Expand Down
9 changes: 9 additions & 0 deletions src/components/WideRHPContextProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ function WideRHPContextProvider({children}: React.PropsWithChildren) {

// Return undefined if RHP is not the last route
const lastVisibleRHPRouteKey = useRootNavigationState((state) => {
// Safe handling when navigation is not yet initialized
if (!state) {
return undefined;
}
const lastFullScreenRouteIndex = state?.routes.findLastIndex((route) => isFullScreenName(route.name));
const lastRHPRouteIndex = state?.routes.findLastIndex((route) => route.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR);

Expand Down Expand Up @@ -118,6 +122,11 @@ function WideRHPContextProvider({children}: React.PropsWithChildren) {
* Shows second overlay when RHP is open and there is a wide RHP route open but there is another regular route on the top.
*/
const shouldShowSecondaryOverlay = useRootNavigationState((state) => {
// Safe handling when navigation is not yet initialized
if (!state) {
return false;
}

const focusedRoute = findFocusedRoute(state);
const isRHPLastRootRoute = state?.routes.at(-1)?.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR;

Expand Down
15 changes: 13 additions & 2 deletions src/hooks/useRootNavigationState.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import type {NavigationState} from '@react-navigation/routers';
import {useEffect, useRef, useState} from 'react';
import Log from '@libs/Log';
import navigationRef from '@libs/Navigation/navigationRef';

type Selector<T> = (state: NavigationState) => T;
type Selector<T> = (state: NavigationState | undefined) => T;

/**
* Hook to get a value from the current root navigation state using a selector.
*
* If navigation is not yet initialized, undefined will be passed to the selector function
* instead of NavigationState. Therefore, the selector must handle undefined
* and return a safe default value.
*
* @param selector Selector function to get a value from the state.
*/
function useRootNavigationState<T>(selector: Selector<T>): T {
const [result, setResult] = useState(() => selector(navigationRef.getRootState()));
const [result, setResult] = useState<T>(() => {
if (!navigationRef.isReady()) {
Log.warn('[src/hooks/useRootNavigationState.ts] NavigationRef is not ready. Returning selector value with undefined.');
return selector(undefined);
}
return selector(navigationRef.getRootState());
});

// We store the selector in a ref to avoid re-subscribing listeners every render
const selectorRef = useRef(selector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {CommonActions, ParamListBase, PartialState, RouterConfigOptions, St
import {StackActions, StackRouter} from '@react-navigation/native';
import pick from 'lodash/pick';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import Log from '@libs/Log';
import getParamsFromRoute from '@libs/Navigation/helpers/getParamsFromRoute';
import navigationRef from '@libs/Navigation/navigationRef';
import type {NavigationPartialRoute} from '@libs/Navigation/types';
Expand Down Expand Up @@ -33,9 +34,12 @@ type AdaptStateIfNecessaryArgs = {
* @param options - Configuration options including sidebarScreen, defaultCentralScreen, and parentRoute
*/
function adaptStateIfNecessary({state, options: {sidebarScreen, defaultCentralScreen, parentRoute}}: AdaptStateIfNecessaryArgs) {
const isNarrowLayout = getIsNarrowLayout();
const rootState = navigationRef.getRootState();
if (!navigationRef.isReady()) {
Log.warn('[src/libs/Navigation/AppNavigator/createSplitNavigator/SplitRouter.ts] NavigationRef is not ready. Returning the original state without adaptation.');
}

const isNarrowLayout = getIsNarrowLayout();
const rootState = navigationRef.isReady() ? navigationRef.getRootState() : undefined;
const lastRoute = state.routes.at(-1) as NavigationPartialRoute;

const routes = [...state.routes];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import {navigationRef} from '@libs/Navigation/Navigation';
import type {RootNavigatorParamList, State} from '@libs/Navigation/types';
import Log from '@libs/Log';
import navigationRef from '@libs/Navigation/navigationRef';
import SCREENS from '@src/SCREENS';

const isOnSearchMoneyRequestReportPage = (): boolean => {
const rootState = navigationRef.getRootState() as State<RootNavigatorParamList> | undefined;
if (!navigationRef.isReady()) {
Log.warn('[src/libs/Navigation/helpers/isOnSearchMoneyRequestReportPage.ts] NavigationRef is not ready. Returning false.');
return false;
}

const rootState = navigationRef.getRootState();

if (!rootState) {
return false;
}
Expand Down
6 changes: 6 additions & 0 deletions src/libs/Navigation/helpers/navigateToWorkspacesPage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import interceptAnonymousUser from '@libs/interceptAnonymousUser';
import Log from '@libs/Log';
import {getPreservedNavigatorState} from '@libs/Navigation/AppNavigator/createSplitNavigator/usePreserveNavigatorState';
import Navigation from '@libs/Navigation/Navigation';
import navigationRef from '@libs/Navigation/navigationRef';
Expand All @@ -19,6 +20,11 @@ type Params = {

// Gets the latest workspace navigation state, restoring from session or preserved state if needed.
const getWorkspaceNavigationRouteState = () => {
if (!navigationRef.isReady()) {
Log.warn('[src/libs/Navigation/helpers/navigateToWorkspacesPage.ts] NavigationRef is not ready. Returning empty object.');
Comment thread
collectioneur marked this conversation as resolved.
Resolved
return {};
}

const rootState = navigationRef.getRootState();

// Only consider main (fullscreen) routes for top-level navigation context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function InternationalDepositAccountContent({privatePersonalDetails, corpayField

const skippedSteps = getSkippedSteps(skipAccountTypeStep, skipAccountHolderInformationStep);

const topmostFullScreenRoute = useRootNavigationState((state) => state.routes.findLast((route) => isFullScreenName(route.name)));
const topmostFullScreenRoute = useRootNavigationState((state) => state?.routes.findLast((route) => isFullScreenName(route.name)));

const goBack = useCallback(() => {
switch (topmostFullScreenRoute?.name) {
Expand Down
1 change: 1 addition & 0 deletions tests/unit/navigateToWorkspacesPageTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const mockParams = {currentUserLogin: 'test@example.com', shouldUseNarrowLayout:
describe('navigateToWorkspacesPage', () => {
beforeEach(() => {
jest.clearAllMocks();
(navigationRef.isReady as jest.Mock).mockReturnValue(true);
});

function mockIntercept() {
Expand Down