-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathCustomRouter.js
42 lines (37 loc) · 1.72 KB
/
CustomRouter.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
import _ from 'underscore';
import {StackRouter} from '@react-navigation/native';
import NAVIGATORS from '../../../../NAVIGATORS';
/**
* @param {Object} state - react-navigation state
* @returns {Boolean}
*/
const isAtLeastOneCentralPaneNavigatorInState = (state) => _.find(state.routes, (r) => r.name === NAVIGATORS.CENTRAL_PANE_NAVIGATOR);
/**
* Adds report route without any specific reportID to the state.
* The report screen will self set proper reportID param based on the helper function findLastAccessedReport (look at ReportScreenWrapper for more info)
*
* @param {Object} state - react-navigation state
*/
const addCentralPaneNavigatorRoute = (state) => {
state.routes.splice(1, 0, {name: NAVIGATORS.CENTRAL_PANE_NAVIGATOR});
// eslint-disable-next-line no-param-reassign
state.index = state.routes.length - 1;
};
function CustomRouter(options) {
const stackRouter = StackRouter(options);
return {
...stackRouter,
getRehydratedState(partialState, {routeNames, routeParamList}) {
// Make sure that there is at least one CentralPaneNavigator (ReportScreen by default) in the state if this is a wide layout
if (!isAtLeastOneCentralPaneNavigatorInState(partialState) && !options.getIsSmallScreenWidth()) {
// If we added a route we need to make sure that the state.stale is true to generate new key for this route
// eslint-disable-next-line no-param-reassign
partialState.stale = true;
addCentralPaneNavigatorRoute(partialState);
}
const state = stackRouter.getRehydratedState(partialState, {routeNames, routeParamList});
return state;
},
};
}
export default CustomRouter;