-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
CustomRouter.ts
83 lines (70 loc) · 3.37 KB
/
CustomRouter.ts
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
import type {NavigationState, PartialState, RouterConfigOptions, StackNavigationState} from '@react-navigation/native';
import {StackRouter} from '@react-navigation/native';
import type {ParamListBase} from '@react-navigation/routers';
import getIsSmallScreenWidth from '@libs/getIsSmallScreenWidth';
import NAVIGATORS from '@src/NAVIGATORS';
import SCREENS from '@src/SCREENS';
import type {ResponsiveStackNavigatorRouterOptions} from './types';
type State = NavigationState | PartialState<NavigationState>;
const isAtLeastOneCentralPaneNavigatorInState = (state: State): boolean => !!state.routes.find((route) => route.name === NAVIGATORS.CENTRAL_PANE_NAVIGATOR);
const getTopMostReportIDFromRHP = (state: State): string => {
if (!state) {
return '';
}
const topmostRightPane = state.routes.filter((route) => route.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR).at(-1);
if (topmostRightPane?.state) {
return getTopMostReportIDFromRHP(topmostRightPane.state);
}
const topmostRoute = state.routes.at(-1);
if (topmostRoute?.state) {
return getTopMostReportIDFromRHP(topmostRoute.state);
}
if (topmostRoute?.params && 'reportID' in topmostRoute.params && typeof topmostRoute.params.reportID === 'string' && 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 state - react-navigation state
*/
const addCentralPaneNavigatorRoute = (state: 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, @typescript-eslint/non-nullable-type-assertion-style
(state.index as number) = state.routes.length - 1;
};
function CustomRouter(options: ResponsiveStackNavigatorRouterOptions) {
const stackRouter = StackRouter(options);
return {
...stackRouter,
getRehydratedState(partialState: StackNavigationState<ParamListBase>, {routeNames, routeParamList, routeGetIdList}: RouterConfigOptions): StackNavigationState<ParamListBase> {
const isSmallScreenWidth = getIsSmallScreenWidth();
// Make sure that there is at least one CentralPaneNavigator (ReportScreen by default) in the state if this is a wide layout
if (!isAtLeastOneCentralPaneNavigatorInState(partialState) && !isSmallScreenWidth) {
// 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 as boolean) = true;
addCentralPaneNavigatorRoute(partialState);
}
const state = stackRouter.getRehydratedState(partialState, {routeNames, routeParamList, routeGetIdList});
return state;
},
};
}
export default CustomRouter;