UNPKG

3.41 kBJavaScriptView Raw
1export default function getActionFromState(state, options) {
2 var _state$index, _normalizedConfig$scr;
3
4 // Create a normalized configs object which will be easier to use
5 const normalizedConfig = options ? createNormalizedConfigItem(options) : {};
6 const routes = state.index != null ? state.routes.slice(0, state.index + 1) : state.routes;
7
8 if (routes.length === 0) {
9 return undefined;
10 }
11
12 if (!(routes.length === 1 && routes[0].key === undefined || routes.length === 2 && routes[0].key === undefined && routes[0].name === (normalizedConfig === null || normalizedConfig === void 0 ? void 0 : normalizedConfig.initialRouteName) && routes[1].key === undefined)) {
13 return {
14 type: 'RESET',
15 payload: state
16 };
17 }
18
19 const route = state.routes[(_state$index = state.index) !== null && _state$index !== void 0 ? _state$index : state.routes.length - 1];
20 let current = route === null || route === void 0 ? void 0 : route.state;
21 let config = normalizedConfig === null || normalizedConfig === void 0 ? void 0 : (_normalizedConfig$scr = normalizedConfig.screens) === null || _normalizedConfig$scr === void 0 ? void 0 : _normalizedConfig$scr[route === null || route === void 0 ? void 0 : route.name];
22 let params = { ...route.params
23 };
24 let payload = route ? {
25 name: route.name,
26 path: route.path,
27 params
28 } : undefined;
29
30 while (current) {
31 var _config, _config2, _config2$screens;
32
33 if (current.routes.length === 0) {
34 return undefined;
35 }
36
37 const routes = current.index != null ? current.routes.slice(0, current.index + 1) : current.routes;
38 const route = routes[routes.length - 1]; // Explicitly set to override existing value when merging params
39
40 Object.assign(params, {
41 initial: undefined,
42 screen: undefined,
43 params: undefined,
44 state: undefined
45 });
46
47 if (routes.length === 1 && routes[0].key === undefined) {
48 params.initial = true;
49 params.screen = route.name;
50 } else if (routes.length === 2 && routes[0].key === undefined && routes[0].name === ((_config = config) === null || _config === void 0 ? void 0 : _config.initialRouteName) && routes[1].key === undefined) {
51 params.initial = false;
52 params.screen = route.name;
53 } else {
54 params.state = current;
55 break;
56 }
57
58 if (route.state) {
59 params.params = { ...route.params
60 };
61 params = params.params;
62 } else {
63 params.path = route.path;
64 params.params = route.params;
65 }
66
67 current = route.state;
68 config = (_config2 = config) === null || _config2 === void 0 ? void 0 : (_config2$screens = _config2.screens) === null || _config2$screens === void 0 ? void 0 : _config2$screens[route.name];
69 }
70
71 if (!payload) {
72 return;
73 } // Try to construct payload for a `NAVIGATE` action from the state
74 // This lets us preserve the navigation state and not lose it
75
76
77 return {
78 type: 'NAVIGATE',
79 payload
80 };
81}
82
83const createNormalizedConfigItem = config => typeof config === 'object' && config != null ? {
84 initialRouteName: config.initialRouteName,
85 screens: config.screens != null ? createNormalizedConfigs(config.screens) : undefined
86} : {};
87
88const createNormalizedConfigs = options => Object.entries(options).reduce((acc, _ref) => {
89 let [k, v] = _ref;
90 acc[k] = createNormalizedConfigItem(v);
91 return acc;
92}, {});
93//# sourceMappingURL=getActionFromState.js.map
\No newline at end of file