UNPKG

12.7 kBSource Map (JSON)View Raw
1{"version":3,"names":["linkingHandlers","useLinking","ref","independent","enabled","prefixes","filter","config","getInitialURL","Promise","race","Linking","resolve","setTimeout","subscribe","listener","callback","url","subscription","addEventListener","removeEventListener","bind","remove","getStateFromPath","getStateFromPathDefault","getActionFromState","getActionFromStateDefault","React","useEffect","process","env","NODE_ENV","undefined","length","console","error","Platform","OS","join","trim","handler","Symbol","push","index","indexOf","splice","enabledRef","useRef","prefixesRef","filterRef","configRef","getInitialURLRef","getStateFromPathRef","getActionFromStateRef","current","getStateFromURL","useCallback","path","extractPathFromURL","getInitialState","state","then","thenable","onfulfilled","catch","navigation","rootState","getRootState","routes","some","r","routeNames","includes","name","warn","action","dispatch","e","message","resetRoot"],"sources":["useLinking.native.tsx"],"sourcesContent":["import {\n getActionFromState as getActionFromStateDefault,\n getStateFromPath as getStateFromPathDefault,\n NavigationContainerRef,\n ParamListBase,\n} from '@react-navigation/core';\nimport * as React from 'react';\nimport { Linking, Platform } from 'react-native';\n\nimport extractPathFromURL from './extractPathFromURL';\nimport type { LinkingOptions } from './types';\n\ntype ResultState = ReturnType<typeof getStateFromPathDefault>;\n\ntype Options = LinkingOptions<ParamListBase> & {\n independent?: boolean;\n};\n\nlet linkingHandlers: Symbol[] = [];\n\nexport default function useLinking(\n ref: React.RefObject<NavigationContainerRef<ParamListBase>>,\n {\n independent,\n enabled = true,\n prefixes,\n filter,\n config,\n getInitialURL = () =>\n Promise.race([\n Linking.getInitialURL(),\n new Promise<undefined>((resolve) =>\n // Timeout in 150ms if `getInitialState` doesn't resolve\n // Workaround for https://github.com/facebook/react-native/issues/25675\n setTimeout(resolve, 150)\n ),\n ]),\n subscribe = (listener) => {\n const callback = ({ url }: { url: string }) => listener(url);\n\n const subscription = Linking.addEventListener('url', callback) as\n | { remove(): void }\n | undefined;\n\n // Storing this in a local variable stops Jest from complaining about import after teardown\n const removeEventListener = Linking.removeEventListener?.bind(Linking);\n\n return () => {\n // https://github.com/facebook/react-native/commit/6d1aca806cee86ad76de771ed3a1cc62982ebcd7\n if (subscription?.remove) {\n subscription.remove();\n } else {\n removeEventListener?.('url', callback);\n }\n };\n },\n getStateFromPath = getStateFromPathDefault,\n getActionFromState = getActionFromStateDefault,\n }: Options\n) {\n React.useEffect(() => {\n if (process.env.NODE_ENV === 'production') {\n return undefined;\n }\n\n if (independent) {\n return undefined;\n }\n\n if (enabled !== false && linkingHandlers.length) {\n console.error(\n [\n 'Looks like you have configured linking in multiple places. This is likely an error since deep links should only be handled in one place to avoid conflicts. Make sure that:',\n \"- You don't have multiple NavigationContainers in the app each with 'linking' enabled\",\n '- Only a single instance of the root component is rendered',\n Platform.OS === 'android'\n ? \"- You have set 'android:launchMode=singleTask' in the '<activity />' section of the 'AndroidManifest.xml' file to avoid launching multiple instances\"\n : '',\n ]\n .join('\\n')\n .trim()\n );\n }\n\n const handler = Symbol();\n\n if (enabled !== false) {\n linkingHandlers.push(handler);\n }\n\n return () => {\n const index = linkingHandlers.indexOf(handler);\n\n if (index > -1) {\n linkingHandlers.splice(index, 1);\n }\n };\n }, [enabled, independent]);\n\n // We store these options in ref to avoid re-creating getInitialState and re-subscribing listeners\n // This lets user avoid wrapping the items in `React.useCallback` or `React.useMemo`\n // Not re-creating `getInitialState` is important coz it makes it easier for the user to use in an effect\n const enabledRef = React.useRef(enabled);\n const prefixesRef = React.useRef(prefixes);\n const filterRef = React.useRef(filter);\n const configRef = React.useRef(config);\n const getInitialURLRef = React.useRef(getInitialURL);\n const getStateFromPathRef = React.useRef(getStateFromPath);\n const getActionFromStateRef = React.useRef(getActionFromState);\n\n React.useEffect(() => {\n enabledRef.current = enabled;\n prefixesRef.current = prefixes;\n filterRef.current = filter;\n configRef.current = config;\n getInitialURLRef.current = getInitialURL;\n getStateFromPathRef.current = getStateFromPath;\n getActionFromStateRef.current = getActionFromState;\n });\n\n const getStateFromURL = React.useCallback(\n (url: string | null | undefined) => {\n if (!url || (filterRef.current && !filterRef.current(url))) {\n return undefined;\n }\n\n const path = extractPathFromURL(prefixesRef.current, url);\n\n return path !== undefined\n ? getStateFromPathRef.current(path, configRef.current)\n : undefined;\n },\n []\n );\n\n const getInitialState = React.useCallback(() => {\n let state: ResultState | undefined;\n\n if (enabledRef.current) {\n const url = getInitialURLRef.current();\n\n if (url != null && typeof url !== 'string') {\n return url.then((url) => {\n const state = getStateFromURL(url);\n\n return state;\n });\n }\n\n state = getStateFromURL(url);\n }\n\n const thenable = {\n then(onfulfilled?: (state: ResultState | undefined) => void) {\n return Promise.resolve(onfulfilled ? onfulfilled(state) : state);\n },\n catch() {\n return thenable;\n },\n };\n\n return thenable as PromiseLike<ResultState | undefined>;\n }, [getStateFromURL]);\n\n React.useEffect(() => {\n const listener = (url: string) => {\n if (!enabled) {\n return;\n }\n\n const navigation = ref.current;\n const state = navigation ? getStateFromURL(url) : undefined;\n\n if (navigation && state) {\n // Make sure that the routes in the state exist in the root navigator\n // Otherwise there's an error in the linking configuration\n const rootState = navigation.getRootState();\n\n if (state.routes.some((r) => !rootState?.routeNames.includes(r.name))) {\n console.warn(\n \"The navigation state parsed from the URL contains routes not present in the root navigator. This usually means that the linking configuration doesn't match the navigation structure. See https://reactnavigation.org/docs/configuring-links for more details on how to specify a linking configuration.\"\n );\n return;\n }\n\n const action = getActionFromStateRef.current(state, configRef.current);\n\n if (action !== undefined) {\n try {\n navigation.dispatch(action);\n } catch (e) {\n // Ignore any errors from deep linking.\n // This could happen in case of malformed links, navigation object not being initialized etc.\n console.warn(\n `An error occurred when trying to handle the link '${url}': ${\n typeof e === 'object' && e != null && 'message' in e\n ? // @ts-expect-error: we're already checking for this\n e.message\n : e\n }`\n );\n }\n } else {\n navigation.resetRoot(state);\n }\n }\n };\n\n return subscribe(listener);\n }, [enabled, getStateFromURL, ref, subscribe]);\n\n return {\n getInitialState,\n };\n}\n"],"mappings":";;;;;;;AAAA;;AAMA;;AACA;;AAEA;;;;;;;;AASA,IAAIA,eAAyB,GAAG,EAAhC;;AAEe,SAASC,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