Repository navigation
Expand file tree
/
Copy pathRootStackRouter.ts
More file actions
253 lines (206 loc) · 11.7 KB
/
Copy pathRootStackRouter.ts
File metadata and controls
253 lines (206 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import {createGuardContext, evaluateGuards} from '@libs/Navigation/guards';
import getAdaptedStateFromPath from '@libs/Navigation/helpers/getAdaptedStateFromPath';
import {isFullScreenName} from '@libs/Navigation/helpers/isNavigatorName';
import isSideModalNavigator from '@libs/Navigation/helpers/isSideModalNavigator';
import {getTabScreenParam} from '@libs/Navigation/helpers/tabNavigatorUtils';
import {linkingConfig} from '@libs/Navigation/linkingConfig';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import type {RouterConfigOptions, StackActionType, StackNavigationState} from '@react-navigation/native';
import type {ParamListBase} from '@react-navigation/routers';
import {CommonActions, StackRouter} from '@react-navigation/native';
import type {
DismissModalActionType,
PreloadActionType,
PushActionType,
RemoveFullscreenUnderRHPActionType,
ReplaceActionType,
ReplaceFullscreenUnderRHPActionType,
RootStackNavigatorAction,
RootStackNavigatorRouterOptions,
ToggleMfaModalNavigatorWithHistoryActionType,
ToggleModalWithHistoryActionType,
ToggleSidePanelWithHistoryActionType,
} from './types';
import {
handleDismissModalAction,
handleNavigatingToModalFromModal,
handlePushFullscreenAction,
handleRemoveFullscreenUnderRHP,
handleReplaceFullscreenUnderRHP,
handleReplaceReportsSplitNavigatorAction,
handleToggleMfaModalNavigatorWithHistoryAction,
handleToggleModalWithHistoryAction,
handleToggleSidePanelWithHistoryAction,
MODAL_ROUTES_TO_DISMISS,
removeStalePreMountsFromResetAction,
} from './GetStateForActionHandlers';
import recordBlockedNavigation from './recordBlockedNavigation';
function isPushAction(action: RootStackNavigatorAction): action is PushActionType {
return action.type === CONST.NAVIGATION.ACTION_TYPE.PUSH;
}
function isReplaceAction(action: RootStackNavigatorAction): action is ReplaceActionType {
return action.type === CONST.NAVIGATION.ACTION_TYPE.REPLACE;
}
function isDismissModalAction(action: RootStackNavigatorAction): action is DismissModalActionType {
return action.type === CONST.NAVIGATION.ACTION_TYPE.DISMISS_MODAL;
}
function isReplaceFullscreenUnderRHPAction(action: RootStackNavigatorAction): action is ReplaceFullscreenUnderRHPActionType {
return action.type === CONST.NAVIGATION.ACTION_TYPE.REPLACE_FULLSCREEN_UNDER_RHP;
}
function isRemoveFullscreenUnderRHPAction(action: RootStackNavigatorAction): action is RemoveFullscreenUnderRHPActionType {
return action.type === CONST.NAVIGATION.ACTION_TYPE.REMOVE_FULLSCREEN_UNDER_RHP;
}
function isToggleSidePanelWithHistoryAction(action: RootStackNavigatorAction): action is ToggleSidePanelWithHistoryActionType {
return action.type === CONST.NAVIGATION.ACTION_TYPE.TOGGLE_SIDE_PANEL_WITH_HISTORY;
}
function isToggleMfaModalNavigatorWithHistoryAction(action: RootStackNavigatorAction): action is ToggleMfaModalNavigatorWithHistoryActionType {
return action.type === CONST.NAVIGATION.ACTION_TYPE.TOGGLE_MFA_MODAL_NAVIGATOR_WITH_HISTORY;
}
function isToggleModalWithHistoryAction(action: RootStackNavigatorAction): action is ToggleModalWithHistoryActionType {
return action.type === CONST.NAVIGATION.ACTION_TYPE.TOGGLE_MODAL_WITH_HISTORY;
}
function isPreloadAction(action: RootStackNavigatorAction): action is PreloadActionType {
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
*
* @param state - Current navigation state
* @param action - Navigation action being attempted
* @param configOptions - Router configuration options
* @param stackRouter - Stack router instance
* @returns Modified state if guard blocks/redirects, null if navigation should proceed
*/
function handleNavigationGuards(
state: StackNavigationState<ParamListBase>,
action: RootStackNavigatorAction,
configOptions: RouterConfigOptions,
stackRouter: ReturnType<typeof StackRouter>,
): ReturnType<ReturnType<typeof StackRouter>['getStateForAction']> | null {
const guardContext = createGuardContext();
const guardResult = evaluateGuards(state, action, guardContext);
if (guardResult.type === 'BLOCK') {
recordBlockedNavigation();
// React Navigation only syncs the browser history back after a blocked browser back press when the state object changes.
return {...state};
}
if (guardResult.type === 'REDIRECT') {
const redirectState = getAdaptedStateFromPath(guardResult.route, linkingConfig.config);
if (!redirectState?.routes) {
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,
});
return stackRouter.getStateForAction(state, resetAction, configOptions);
}
return null;
}
function isNavigatingToModalFromModal(state: StackNavigationState<ParamListBase>, action: CommonActions.Action | StackActionType): action is PushActionType {
if (action.type !== CONST.NAVIGATION.ACTION_TYPE.PUSH) {
return false;
}
const lastRoute = state.routes.at(-1);
// If the last route is a side modal navigator and the generated minimal action want's to push a new side modal navigator that means they are different ones.
// We want to dismiss the one that is currently on the top.
return isSideModalNavigator(lastRoute?.name) && isSideModalNavigator(action.payload.name);
}
function RootStackRouter(options: RootStackNavigatorRouterOptions) {
const stackRouter = StackRouter(options);
return {
...stackRouter,
getStateForAction(state: StackNavigationState<ParamListBase>, rawAction: RootStackNavigatorAction, configOptions: RouterConfigOptions) {
const action = removeStalePreMountsFromResetAction(rawAction);
// Evaluate navigation guards FIRST
const guardState = handleNavigationGuards(state, action, configOptions, stackRouter);
if (guardState) {
return guardState;
}
// Guards allowed navigation - continue with routing logic
if (isPreloadAction(action) && action.payload.name === state.routes.at(-1)?.name) {
return state;
}
if (isToggleSidePanelWithHistoryAction(action)) {
return handleToggleSidePanelWithHistoryAction(state, action);
}
if (isToggleMfaModalNavigatorWithHistoryAction(action)) {
return handleToggleMfaModalNavigatorWithHistoryAction(state, action);
}
if (isToggleModalWithHistoryAction(action)) {
return handleToggleModalWithHistoryAction(state, action);
}
if (isDismissModalAction(action)) {
return handleDismissModalAction(state, configOptions, stackRouter);
}
if (isReplaceFullscreenUnderRHPAction(action)) {
return handleReplaceFullscreenUnderRHP(state, action, configOptions, stackRouter);
}
if (isRemoveFullscreenUnderRHPAction(action)) {
return handleRemoveFullscreenUnderRHP(state, action, configOptions, stackRouter);
}
if (isReplaceAction(action) && (action.payload.name === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR || getTabScreenParam(action.payload) === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR)) {
return handleReplaceReportsSplitNavigatorAction(state, action, configOptions, stackRouter);
}
// When navigating to a specific workspace from WorkspaceListPage there should be entering animation for its sidebar (only case where we want animation for sidebar)
// That's why we have a separate handler for opening it called handleOpenWorkspaceSplitAction
// options for WorkspaceSplitNavigator can be found in AuthScreens.tsx > getWorkspaceSplitNavigatorOptions
if (isPushAction(action) && isFullScreenName(action.payload.name) && action.payload.name !== NAVIGATORS.WORKSPACE_NAVIGATOR) {
return handlePushFullscreenAction(state, action, configOptions, stackRouter);
}
if (isNavigatingToModalFromModal(state, action)) {
return handleNavigatingToModalFromModal(state, action, configOptions, stackRouter);
}
return stackRouter.getStateForAction(state, action, configOptions);
},
};
}
export default RootStackRouter;