1 | import * as React from 'react';
|
2 | import NavigationBuilderContext from './NavigationBuilderContext';
|
3 | import useOnPreventRemove, { shouldPreventRemove } from './useOnPreventRemove';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export default function useOnAction(_ref) {
|
15 | let {
|
16 | router,
|
17 | getState,
|
18 | setState,
|
19 | key,
|
20 | actionListeners,
|
21 | beforeRemoveListeners,
|
22 | routerConfigOptions,
|
23 | emitter
|
24 | } = _ref;
|
25 | const {
|
26 | onAction: onActionParent,
|
27 | onRouteFocus: onRouteFocusParent,
|
28 | addListener: addListenerParent,
|
29 | onDispatchAction
|
30 | } = React.useContext(NavigationBuilderContext);
|
31 | const routerConfigOptionsRef = React.useRef(routerConfigOptions);
|
32 | React.useEffect(() => {
|
33 | routerConfigOptionsRef.current = routerConfigOptions;
|
34 | });
|
35 | const onAction = React.useCallback(function (action) {
|
36 | let visitedNavigators = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Set();
|
37 | const state = getState();
|
38 |
|
39 |
|
40 | if (visitedNavigators.has(state.key)) {
|
41 | return false;
|
42 | }
|
43 |
|
44 | visitedNavigators.add(state.key);
|
45 |
|
46 | if (typeof action.target !== 'string' || action.target === state.key) {
|
47 | let result = router.getStateForAction(state, action, routerConfigOptionsRef.current);
|
48 |
|
49 |
|
50 | result = result === null && action.target === state.key ? state : result;
|
51 |
|
52 | if (result !== null) {
|
53 | onDispatchAction(action, state === result);
|
54 |
|
55 | if (state !== result) {
|
56 | const isPrevented = shouldPreventRemove(emitter, beforeRemoveListeners, state.routes, result.routes, action);
|
57 |
|
58 | if (isPrevented) {
|
59 | return true;
|
60 | }
|
61 |
|
62 | setState(result);
|
63 | }
|
64 |
|
65 | if (onRouteFocusParent !== undefined) {
|
66 |
|
67 |
|
68 | const shouldFocus = router.shouldActionChangeFocus(action);
|
69 |
|
70 | if (shouldFocus && key !== undefined) {
|
71 | onRouteFocusParent(key);
|
72 | }
|
73 | }
|
74 |
|
75 | return true;
|
76 | }
|
77 | }
|
78 |
|
79 | if (onActionParent !== undefined) {
|
80 |
|
81 | if (onActionParent(action, visitedNavigators)) {
|
82 | return true;
|
83 | }
|
84 | }
|
85 |
|
86 |
|
87 | for (let i = actionListeners.length - 1; i >= 0; i--) {
|
88 | const listener = actionListeners[i];
|
89 |
|
90 | if (listener(action, visitedNavigators)) {
|
91 | return true;
|
92 | }
|
93 | }
|
94 |
|
95 | return false;
|
96 | }, [actionListeners, beforeRemoveListeners, emitter, getState, key, onActionParent, onDispatchAction, onRouteFocusParent, router, setState]);
|
97 | useOnPreventRemove({
|
98 | getState,
|
99 | emitter,
|
100 | beforeRemoveListeners
|
101 | });
|
102 | React.useEffect(() => addListenerParent === null || addListenerParent === void 0 ? void 0 : addListenerParent('action', onAction), [addListenerParent, onAction]);
|
103 | return onAction;
|
104 | }
|
105 |
|
\ | No newline at end of file |