-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathCustomRouter.js
84 lines (74 loc) · 2.82 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
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
import {StackRouter} from '@react-navigation/native';
import lodashFindLast from 'lodash/findLast';
import _ from 'underscore';
import NAVIGATORS from '@src/NAVIGATORS';
import SCREENS from '@src/SCREENS';
/**
* @param {Object} state - react-navigation state
* @returns {Boolean}
*/
const isAtLeastOneCentralPaneNavigatorInState = (state) => _.find(state.routes, (r) => r.name === NAVIGATORS.CENTRAL_PANE_NAVIGATOR);
/**
* @param {Object} state - react-navigation state
* @returns {String}
*/
const getTopMostReportIDFromRHP = (state) => {
if (!state) {
return '';
}
const topmostRightPane = lodashFindLast(state.routes, (route) => route.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR);
if (topmostRightPane) {
return getTopMostReportIDFromRHP(topmostRightPane.state);
}
const topmostRoute = lodashFindLast(state.routes);
if (topmostRoute.state) {
return getTopMostReportIDFromRHP(topmostRoute.state);
}
if (topmostRoute.params && topmostRoute.params.reportID) {
return topmostRoute.params.reportID;
}
return '';
};
/**
* 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) => {
const reportID = getTopMostReportIDFromRHP(state);
const centralPaneNavigatorRoute = {
name: NAVIGATORS.CENTRAL_PANE_NAVIGATOR,
state: {
routes: [
{
name: SCREENS.REPORT,
params: {
reportID,
},
},
],
},
};
state.routes.splice(1, 0, centralPaneNavigatorRoute);
// 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;