1 | {"version":3,"names":["React","NavigationBuilderContext","useOnPreventRemove","shouldPreventRemove","useOnAction","router","getState","setState","key","actionListeners","beforeRemoveListeners","routerConfigOptions","emitter","onAction","onActionParent","onRouteFocus","onRouteFocusParent","addListener","addListenerParent","onDispatchAction","useContext","routerConfigOptionsRef","useRef","useEffect","current","useCallback","action","visitedNavigators","Set","state","has","add","target","result","getStateForAction","isPrevented","routes","undefined","shouldFocus","shouldActionChangeFocus","i","length","listener"],"sources":["useOnAction.tsx"],"sourcesContent":["import type {\n NavigationAction,\n NavigationState,\n PartialState,\n Router,\n RouterConfigOptions,\n} from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationBuilderContext, {\n ChildActionListener,\n ChildBeforeRemoveListener,\n} from './NavigationBuilderContext';\nimport type { EventMapCore } from './types';\nimport type { NavigationEventEmitter } from './useEventEmitter';\nimport useOnPreventRemove, { shouldPreventRemove } from './useOnPreventRemove';\n\ntype Options = {\n router: Router<NavigationState, NavigationAction>;\n key?: string;\n getState: () => NavigationState;\n setState: (state: NavigationState | PartialState<NavigationState>) => void;\n actionListeners: ChildActionListener[];\n beforeRemoveListeners: Record<string, ChildBeforeRemoveListener | undefined>;\n routerConfigOptions: RouterConfigOptions;\n emitter: NavigationEventEmitter<EventMapCore<any>>;\n};\n\n/**\n * Hook to handle actions for a navigator, including state updates and bubbling.\n *\n * Bubbling an action is achieved in 2 ways:\n * 1. To bubble action to parent, we expose the action handler in context and then access the parent context\n * 2. To bubble action to child, child adds event listeners subscribing to actions from parent\n *\n * When the action handler handles as action, it returns `true`, otherwise `false`.\n */\nexport default function useOnAction({\n router,\n getState,\n setState,\n key,\n actionListeners,\n beforeRemoveListeners,\n routerConfigOptions,\n emitter,\n}: Options) {\n const {\n onAction: onActionParent,\n onRouteFocus: onRouteFocusParent,\n addListener: addListenerParent,\n onDispatchAction,\n } = React.useContext(NavigationBuilderContext);\n\n const routerConfigOptionsRef =\n React.useRef<RouterConfigOptions>(routerConfigOptions);\n\n React.useEffect(() => {\n routerConfigOptionsRef.current = routerConfigOptions;\n });\n\n const onAction = React.useCallback(\n (\n action: NavigationAction,\n visitedNavigators: Set<string> = new Set<string>()\n ) => {\n const state = getState();\n\n // Since actions can bubble both up and down, they could come to the same navigator again\n // We keep track of navigators which have already tried to handle the action and return if it's already visited\n if (visitedNavigators.has(state.key)) {\n return false;\n }\n\n visitedNavigators.add(state.key);\n\n if (typeof action.target !== 'string' || action.target === state.key) {\n let result = router.getStateForAction(\n state,\n action,\n routerConfigOptionsRef.current\n );\n\n // If a target is specified and set to current navigator, the action shouldn't bubble\n // So instead of `null`, we use the state object for such cases to signal that action was handled\n result =\n result === null && action.target === state.key ? state : result;\n\n if (result !== null) {\n onDispatchAction(action, state === result);\n\n if (state !== result) {\n const isPrevented = shouldPreventRemove(\n emitter,\n beforeRemoveListeners,\n state.routes,\n result.routes,\n action\n );\n\n if (isPrevented) {\n return true;\n }\n\n setState(result);\n }\n\n if (onRouteFocusParent !== undefined) {\n // Some actions such as `NAVIGATE` also want to bring the navigated route to focus in the whole tree\n // This means we need to focus all of the parent navigators of this navigator as well\n const shouldFocus = router.shouldActionChangeFocus(action);\n\n if (shouldFocus && key !== undefined) {\n onRouteFocusParent(key);\n }\n }\n\n return true;\n }\n }\n\n if (onActionParent !== undefined) {\n // Bubble action to the parent if the current navigator didn't handle it\n if (onActionParent(action, visitedNavigators)) {\n return true;\n }\n }\n\n // If the action wasn't handled by current navigator or a parent navigator, let children handle it\n for (let i = actionListeners.length - 1; i >= 0; i--) {\n const listener = actionListeners[i];\n\n if (listener(action, visitedNavigators)) {\n return true;\n }\n }\n\n return false;\n },\n [\n actionListeners,\n beforeRemoveListeners,\n emitter,\n getState,\n key,\n onActionParent,\n onDispatchAction,\n onRouteFocusParent,\n router,\n setState,\n ]\n );\n\n useOnPreventRemove({\n getState,\n emitter,\n beforeRemoveListeners,\n });\n\n React.useEffect(\n () => addListenerParent?.('action', onAction),\n [addListenerParent, onAction]\n );\n\n return onAction;\n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