-
-
Notifications
You must be signed in to change notification settings - Fork 643
Expand file tree
/
Copy pathreducer.tsx
More file actions
209 lines (185 loc) · 5.42 KB
/
reducer.tsx
File metadata and controls
209 lines (185 loc) · 5.42 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import type {
TabRoute,
TabRouteConfig,
TabsContainerState,
TabsNavState,
TabsNavigationAction,
TabsNavigationActionSelectTab,
TabsNavigationActionNativeSelectTab,
TabsNavigationActionSetOptions,
} from './TabsContainer.types';
const NOT_FOUND_INDEX = -1;
export function tabsNavigationReducer(
state: TabsContainerState,
action: TabsNavigationAction,
): TabsContainerState {
switch (action.type) {
case 'tab-select':
return tabsActionSelectTabHandler(state, action);
case 'native-tab-select':
return tabsActionNativeSelectTabHandler(state, action);
case 'set-options':
return tabsActionSetOptionsHandler(state, action);
}
// @ts-ignore
throw new Error(
`[Tabs] Unhandled navigation action: ${JSON.stringify(action)}`,
);
}
export function tabsNavigationReducerWithLogging(
state: TabsContainerState,
action: TabsNavigationAction,
): TabsContainerState {
console.log(`[Tabs] Handling action: ${JSON.stringify(action)}`);
console.log(`[Tabs] BEFORE state: ${JSON.stringify(state, undefined, 2)}`);
const newState = tabsNavigationReducer(state, action);
if (state === newState) {
console.log('[Tabs] AFTER state: unchanged');
} else {
console.log(
`[Tabs] AFTER state: ${JSON.stringify(newState, undefined, 2)}`,
);
}
return newState;
}
/**
* Models JS-driven tab select request.
*/
function tabsActionSelectTabHandler(
state: TabsContainerState,
action: TabsNavigationActionSelectTab,
): TabsContainerState {
const routeIndex = state.routes.findIndex(
r => r.routeKey === action.routeKey,
);
if (routeIndex === NOT_FOUND_INDEX) {
console.error(
`[Tabs] select-tab: route with key "${action.routeKey}" not found in state. Ignoring.`,
);
return state;
}
if (state.confirmedState.selectedRouteKey === action.routeKey && !action.forceAction) {
return state;
}
return navStateWithSuggestedState(state, {
selectedRouteKey: action.routeKey,
provenance: state.confirmedState.provenance,
});
}
function tabsActionNativeSelectTabHandler(
state: TabsContainerState,
action: TabsNavigationActionNativeSelectTab,
): TabsContainerState {
const routeIndex = state.routes.findIndex(
r => r.routeKey === action.routeKey,
);
if (routeIndex === NOT_FOUND_INDEX) {
console.error(
`[Tabs] select-tab: route with key "${action.routeKey}" not found in state. Ignoring.`,
);
return state;
}
if (
state.confirmedState.selectedRouteKey === action.routeKey &&
state.confirmedState.provenance === action.nativeEvent.provenance
) {
console.warn(
`[Tabs] Duplicated state confirmation event! ${JSON.stringify(
action.nativeEvent,
)}`,
);
return state;
}
return navStateWithConfirmedState(state, {
selectedRouteKey: action.routeKey,
provenance: action.nativeEvent.provenance,
});
}
function tabsActionSetOptionsHandler(
state: TabsContainerState,
action: TabsNavigationActionSetOptions,
): TabsContainerState {
const routeIndex = state.routes.findIndex(
r => r.routeKey === action.routeKey,
);
if (routeIndex === NOT_FOUND_INDEX) {
throw new Error(
`[Tabs] Cannot set options. Route with key "${action.routeKey}" not found`,
);
}
const routeCopy = { ...state.routes[routeIndex] };
routeCopy.options = {
...routeCopy.options,
...action.options,
};
return {
...state,
routes: state.routes.toSpliced(routeIndex, 1, routeCopy),
};
}
function createTabRouteFromConfig(config: TabRouteConfig): TabRoute {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { Component, ...rest } = config;
return {
...rest,
// Tab names are required to be unique (enforced by useSanitizeRouteConfigs),
// so the name itself serves as a stable unique key.
routeKey: config.name,
};
}
export type TabsContainerStateInitArg = {
routeConfigs: TabRouteConfig[];
defaultRouteName?: string;
};
export function determineInitialTabsContainerState(
arg: TabsContainerStateInitArg,
): TabsContainerState {
const { routeConfigs, defaultRouteName } = arg;
const routes = routeConfigs.map(createTabRouteFromConfig);
let selectedRouteKey: string;
if (defaultRouteName != null) {
const matchingRoute = routes.find(r => r.name === defaultRouteName);
if (!matchingRoute) {
throw new Error(
`[Tabs] defaultRouteName "${defaultRouteName}" does not match any route config name`,
);
}
selectedRouteKey = matchingRoute.routeKey;
} else {
selectedRouteKey = routes[0].routeKey;
}
// suggestedState & confirmedState are aligned here. We assume that JS
// decides what is rendered after first render, therefore we don't need
// to wait for confirmation. This simplifies implementation of the reducer.
return {
routes,
suggestedState: {
selectedRouteKey: selectedRouteKey,
provenance: 0,
},
confirmedState: {
selectedRouteKey: selectedRouteKey,
provenance: 0,
},
};
}
function navStateWithSuggestedState(
state: TabsContainerState,
suggestedState: TabsNavState,
): TabsContainerState {
return {
routes: state.routes,
confirmedState: state.confirmedState,
suggestedState: suggestedState,
};
}
function navStateWithConfirmedState(
state: TabsContainerState,
confirmedState: TabsNavState,
): TabsContainerState {
return {
routes: state.routes,
confirmedState: confirmedState,
suggestedState: state.suggestedState,
};
}