-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
useOnAction.tsx
183 lines (159 loc) · 5.45 KB
/
useOnAction.tsx
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
import type {
NavigationAction,
NavigationState,
PartialState,
Router,
RouterConfigOptions,
} from '@react-navigation/routers';
import * as React from 'react';
import { DeprecatedNavigationInChildContext } from './DeprecatedNavigationInChildContext';
import {
ChildActionListener,
ChildBeforeRemoveListener,
NavigationBuilderContext,
} from './NavigationBuilderContext';
import type { EventMapCore } from './types';
import type { NavigationEventEmitter } from './useEventEmitter';
import { shouldPreventRemove, useOnPreventRemove } from './useOnPreventRemove';
type Options = {
router: Router<NavigationState, NavigationAction>;
key?: string;
getState: () => NavigationState;
setState: (state: NavigationState | PartialState<NavigationState>) => void;
actionListeners: ChildActionListener[];
beforeRemoveListeners: Record<string, ChildBeforeRemoveListener | undefined>;
routerConfigOptions: RouterConfigOptions;
emitter: NavigationEventEmitter<EventMapCore<any>>;
stateForNextRouteNamesChange: PartialState<NavigationState> | null;
};
/**
* Hook to handle actions for a navigator, including state updates and bubbling.
*
* Bubbling an action is achieved in 2 ways:
* 1. To bubble action to parent, we expose the action handler in context and then access the parent context
* 2. To bubble action to child, child adds event listeners subscribing to actions from parent
*
* When the action handler handles as action, it returns `true`, otherwise `false`.
*/
export function useOnAction({
router,
getState,
setState,
key,
actionListeners,
beforeRemoveListeners,
routerConfigOptions,
emitter,
stateForNextRouteNamesChange,
}: Options) {
const {
onAction: onActionParent,
onRouteFocus: onRouteFocusParent,
addListener: addListenerParent,
onDispatchAction,
} = React.useContext(NavigationBuilderContext);
const navigationInChildEnabled = React.useContext(
DeprecatedNavigationInChildContext
);
const routerConfigOptionsRef =
React.useRef<RouterConfigOptions>(routerConfigOptions);
React.useEffect(() => {
routerConfigOptionsRef.current = routerConfigOptions;
});
const onAction = React.useCallback(
(
action: NavigationAction,
visitedNavigators: Set<string> = new Set<string>()
) => {
if (stateForNextRouteNamesChange) {
console.warn(
'Other navigation was invoked before we handling the unhandled deeplink (requested by invoking handleOnNextRouteNamesChange)'
);
}
const state = getState();
// Since actions can bubble both up and down, they could come to the same navigator again
// We keep track of navigators which have already tried to handle the action and return if it's already visited
if (visitedNavigators.has(state.key)) {
return false;
}
visitedNavigators.add(state.key);
if (typeof action.target !== 'string' || action.target === state.key) {
let result = router.getStateForAction(
state,
action,
routerConfigOptionsRef.current
);
// If a target is specified and set to current navigator, the action shouldn't bubble
// So instead of `null`, we use the state object for such cases to signal that action was handled
result =
result === null && action.target === state.key ? state : result;
if (result !== null) {
onDispatchAction(action, state === result);
if (state !== result) {
const isPrevented = shouldPreventRemove(
emitter,
beforeRemoveListeners,
state.routes,
result.routes,
action
);
if (isPrevented) {
return true;
}
setState(result);
}
if (onRouteFocusParent !== undefined) {
// Some actions such as `NAVIGATE` also want to bring the navigated route to focus in the whole tree
// This means we need to focus all of the parent navigators of this navigator as well
const shouldFocus = router.shouldActionChangeFocus(action);
if (shouldFocus && key !== undefined) {
onRouteFocusParent(key);
}
}
return true;
}
}
if (onActionParent !== undefined) {
// Bubble action to the parent if the current navigator didn't handle it
if (onActionParent(action, visitedNavigators)) {
return true;
}
}
if (typeof action.target === 'string' || navigationInChildEnabled) {
// If the action wasn't handled by current navigator or a parent navigator, let children handle it
// Handling this when target isn't specified is deprecated and will be removed in the future
for (let i = actionListeners.length - 1; i >= 0; i--) {
const listener = actionListeners[i];
if (listener(action, visitedNavigators)) {
return true;
}
}
}
return false;
},
[
actionListeners,
beforeRemoveListeners,
emitter,
getState,
navigationInChildEnabled,
key,
onActionParent,
onDispatchAction,
onRouteFocusParent,
router,
setState,
stateForNextRouteNamesChange,
]
);
useOnPreventRemove({
getState,
emitter,
beforeRemoveListeners,
});
React.useEffect(
() => addListenerParent?.('action', onAction),
[addListenerParent, onAction]
);
return onAction;
}