import useUrl from './useUrl'; import * as React from 'react'; import type { Root } from './navigationUtils'; import { type BottomTabType, getScreenKey, type RootValue, } from './navigationUtils'; import { Platform } from 'react-native'; function withoutBasePath( url: string | undefined, basePath: string | undefined ) { if (!url) { return undefined; } return basePath ? url.replace(`/${basePath}`, '') : url; } export function useNavigationUrl({ navigationRoot, initialRootKey, basePath, }: { navigationRoot: Root; initialRootKey: string; basePath: string | undefined; }) { const cachedInitialRootKey = React.useRef(initialRootKey); const initialDefaultUrl = React.useMemo(() => { const defaultRootKey = cachedInitialRootKey.current || Object.keys(navigationRoot)[0]!; const defaultRoot = navigationRoot[defaultRootKey]!; return '/' + getPathFromRoot(defaultRoot, undefined, defaultRootKey); }, [navigationRoot]); const currentUrl = useUrl(); const firstUrl = React.useRef(currentUrl); // Web: only the URL the app was opened on is a deep link. Every later // window.location is a write of this navigator itself (push/replace) or a // browser back/forward that its history manager already handles. Reading it // again on a re-render replayed it through goToUrl; a web navigation writes // the URL only once the destination has rendered, so a re-render during that // window (the goToUrl reset below, a colour-scheme change) navigated back to // the screen the user was leaving and cancelled the click. const initialUrl = (Platform.OS === 'web' ? firstUrl.current : currentUrl) || initialDefaultUrl; const [goToUrl, setGoToUrl] = React.useState(initialUrl); React.useEffect(() => { setGoToUrl(initialUrl); }, [initialUrl]); React.useEffect(() => { if (goToUrl) { // reset goToUrl after it has been handled, so we can handle new deep links // because in the meantime user can navigate away from goToUrl const timerId = setTimeout(() => { setGoToUrl(undefined); }, 1500); return () => { clearTimeout(timerId); }; } return undefined; }, [goToUrl, setGoToUrl]); return { goToUrl: withoutBasePath(goToUrl, basePath), setGoToUrl, initialDefaultUrl: withoutBasePath(initialDefaultUrl, basePath)!, }; } function getPathFromRoot( root: RootValue, tab: BottomTabType | undefined, rootKey: string ) { if (root.type === 'bottomTabs') { return getScreenKey( rootKey, tab || root.children[0], Platform.OS === 'web' ? root.children[0]!.path : undefined ); } return getScreenKey(rootKey, undefined, root.child!.path); }