UNPKG

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