-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Navigation.js
231 lines (201 loc) · 6.92 KB
/
Navigation.js
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
import _ from 'lodash';
import lodashGet from 'lodash/get';
import {CommonActions, getPathFromState, StackActions} from '@react-navigation/native';
import {getActionFromState} from '@react-navigation/core';
import Log from '../Log';
import DomUtils from '../DomUtils';
import linkTo from './linkTo';
import ROUTES from '../../ROUTES';
import linkingConfig from './linkingConfig';
import navigationRef from './navigationRef';
import NAVIGATORS from '../../NAVIGATORS';
import originalGetTopmostReportId from './getTopmostReportId';
import getStateFromPath from './getStateFromPath';
let resolveNavigationIsReadyPromise;
const navigationIsReadyPromise = new Promise((resolve) => {
resolveNavigationIsReadyPromise = resolve;
});
let pendingRoute = null;
/**
* @param {String} methodName
* @param {Object} params
* @returns {Boolean}
*/
function canNavigate(methodName, params = {}) {
if (navigationRef.isReady()) {
return true;
}
Log.hmmm(`[Navigation] ${methodName} failed because navigation ref was not yet ready`, params);
return false;
}
// Re-exporting the getTopmostReportId here to fill in default value for state. The getTopmostReportId isn't defined in this file to avoid cyclic dependencies.
const getTopmostReportId = (state = navigationRef.getState()) => originalGetTopmostReportId(state);
/**
* Method for finding on which index in stack we are.
* @param {Object} route
* @param {Number} index
* @returns {Number}
*/
const getActiveRouteIndex = function (route, index) {
if (route.routes) {
const childActiveRoute = route.routes[route.index || 0];
return getActiveRouteIndex(childActiveRoute, route.index || 0);
}
if (route.state && route.state.routes) {
const childActiveRoute = route.state.routes[route.state.index || 0];
return getActiveRouteIndex(childActiveRoute, route.state.index || 0);
}
if (route.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR) {
return 0;
}
return index;
};
/**
* Main navigation method for redirecting to a route.
* @param {String} route
* @param {String} type - Type of action to perform. Currently UP is supported.
*/
function navigate(route = ROUTES.HOME, type) {
if (!canNavigate('navigate', {route})) {
// Store intended route if the navigator is not yet available,
// we will try again after the NavigationContainer is ready
Log.hmmm(`[Navigation] Container not yet ready, storing route as pending: ${route}`);
pendingRoute = route;
return;
}
// A pressed navigation button will remain focused, keeping its tooltip visible, even if it's supposed to be out of view.
// To prevent that we blur the button manually (especially for Safari, where the mouse leave event is missing).
// More info: https://github.com/Expensify/App/issues/13146
DomUtils.blurActiveElement();
linkTo(navigationRef.current, route, type);
}
/**
* @param {String} fallbackRoute - Fallback route if pop/goBack action should, but is not possible within RHP
* @param {Bool} shouldEnforceFallback - Enforces navigation to fallback route
*/
function goBack(fallbackRoute = ROUTES.HOME, shouldEnforceFallback = false) {
if (!canNavigate('goBack')) {
return;
}
if (!navigationRef.current.canGoBack()) {
Log.hmmm('[Navigation] Unable to go back');
return;
}
if (shouldEnforceFallback || (!getActiveRouteIndex(navigationRef.current.getState()) && fallbackRoute)) {
navigate(fallbackRoute, 'UP');
return;
}
navigationRef.current.goBack();
}
/**
* Update route params for the specified route.
*
* @param {Object} params
* @param {String} routeKey
*/
function setParams(params, routeKey) {
navigationRef.current.dispatch({
...CommonActions.setParams(params),
source: routeKey,
});
}
/**
* Dismisses the last modal stack if there is any
*
* @param {String | undefined} targetReportID - The reportID to navigate to after dismissing the modal
*/
function dismissModal(targetReportID) {
if (!canNavigate('dismissModal')) {
return;
}
const rootState = navigationRef.getRootState();
const lastRoute = _.last(rootState.routes);
if (lastRoute.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR || lastRoute.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR) {
// if we are not in the target report, we need to navigate to it after dismissing the modal
if (targetReportID && targetReportID !== getTopmostReportId(rootState)) {
const state = getStateFromPath(ROUTES.getReportRoute(targetReportID));
const action = getActionFromState(state, linkingConfig.config);
action.type = 'REPLACE';
navigationRef.current.dispatch(action);
} else {
navigationRef.current.dispatch({...StackActions.pop(), target: rootState.key});
}
} else {
Log.hmmm('[Navigation] dismissModal failed because there is no modal stack to dismiss');
}
}
/**
* Returns the current active route
* @returns {String}
*/
function getActiveRoute() {
const currentRouteHasName = navigationRef.current && navigationRef.current.getCurrentRoute().name;
if (!currentRouteHasName) {
return '';
}
const routeFromState = getPathFromState(navigationRef.getRootState(), linkingConfig.config);
if (routeFromState) {
return routeFromState;
}
return '';
}
/**
* @returns {String}
*/
function getReportIDFromRoute() {
if (!navigationRef.current) {
return '';
}
const drawerState = lodashGet(navigationRef.current.getState(), ['routes', 0, 'state']);
const reportRoute = lodashGet(drawerState, ['routes', 0]);
return lodashGet(reportRoute, ['params', 'reportID'], '');
}
/**
* Check whether the passed route is currently Active or not.
*
* Building path with getPathFromState since navigationRef.current.getCurrentRoute().path
* is undefined in the first navigation.
*
* @param {String} routePath Path to check
* @return {Boolean} is active
*/
function isActiveRoute(routePath) {
// We remove First forward slash from the URL before matching
return getActiveRoute().substring(1) === routePath;
}
/**
* Navigate to the route that we originally intended to go to
* but the NavigationContainer was not ready when navigate() was called
*/
function goToPendingRoute() {
if (pendingRoute === null) {
return;
}
Log.hmmm(`[Navigation] Container now ready, going to pending route: ${pendingRoute}`);
navigate(pendingRoute);
pendingRoute = null;
}
/**
* @returns {Promise}
*/
function isNavigationReady() {
return navigationIsReadyPromise;
}
function setIsNavigationReady() {
goToPendingRoute();
resolveNavigationIsReadyPromise();
}
export default {
canNavigate,
navigate,
setParams,
dismissModal,
isActiveRoute,
getActiveRoute,
goBack,
isNavigationReady,
setIsNavigationReady,
getReportIDFromRoute,
getTopmostReportId,
};
export {navigationRef};