1- import { findFocusedRoute } from '@react-navigation/native' ;
1+ import { findFocusedRoute , StackActions } from '@react-navigation/native' ;
22import type { NavigationState , PartialState } from '@react-navigation/native' ;
33import interceptAnonymousUser from '@libs/interceptAnonymousUser' ;
4+ import { getPreservedNavigatorState } from '@libs/Navigation/AppNavigator/createSplitNavigator/usePreserveNavigatorState' ;
45import Navigation from '@libs/Navigation/Navigation' ;
56import navigationRef from '@libs/Navigation/navigationRef' ;
67import { isPendingDeletePolicy , shouldShowPolicy as shouldShowPolicyUtil } from '@libs/PolicyUtils' ;
@@ -11,6 +12,7 @@ import SCREENS from '@src/SCREENS';
1112import type { Domain , Policy } from '@src/types/onyx' ;
1213import getActiveTabName from './getActiveTabName' ;
1314import getPathFromState from './getPathFromState' ;
15+ import { getTabState } from './tabNavigatorUtils' ;
1416
1517type RouteType = NavigationState [ 'routes' ] [ number ] | PartialState < NavigationState > [ 'routes' ] [ number ] ;
1618
@@ -51,6 +53,29 @@ const navigateToWorkspacesPage = ({currentUserLogin, shouldUseNarrowLayout, poli
5153 return ;
5254 }
5355
56+ // Pop to the older TAB_NAVIGATOR holding the workspace state. Target the root stack
57+ // explicitly so POP bypasses SearchFullscreenNavigator's PUSH_PARAMS interceptor.
58+ // https://github.com/Expensify/App/issues/89009
59+ if ( rootState ) {
60+ const topRootIndex = rootState . index ?? rootState . routes . length - 1 ;
61+ const olderTabIdx = rootState . routes . findLastIndex ( ( route , idx ) => {
62+ if ( idx >= topRootIndex || route . name !== NAVIGATORS . TAB_NAVIGATOR ) {
63+ return false ;
64+ }
65+ const tabState = getTabState ( route as Parameters < typeof getTabState > [ 0 ] ) ;
66+ const focusedTab = tabState ?. routes ?. at ( tabState . index ?? 0 ) ;
67+ if ( focusedTab ?. name !== NAVIGATORS . WORKSPACE_NAVIGATOR ) {
68+ return false ;
69+ }
70+ const wsState = focusedTab . state ?? ( focusedTab . key ? getPreservedNavigatorState ( focusedTab . key ) : undefined ) ;
71+ return ! ! wsState ?. routes ?. length ;
72+ } ) ;
73+ if ( olderTabIdx !== - 1 ) {
74+ navigationRef . dispatch ( { ...StackActions . pop ( topRootIndex - olderTabIdx ) , target : rootState . key } ) ;
75+ return ;
76+ }
77+ }
78+
5479 // Check if user is already on a workspace or domain inside WORKSPACE_NAVIGATOR (within TabNavigator)
5580 const isWorkspaceOrDomainOnTop =
5681 lastWorkspacesTabNavigatorRoute ?. name === NAVIGATORS . WORKSPACE_SPLIT_NAVIGATOR || lastWorkspacesTabNavigatorRoute ?. name === NAVIGATORS . DOMAIN_SPLIT_NAVIGATOR ;
0 commit comments