1 | {"version":3,"names":["nanoid","React","useLatestCallback","NavigationHelpersContext","NavigationRouteContext","PreventRemoveContext","transformPreventedRoutes","preventedRoutesMap","preventedRoutesToTransform","values","preventedRoutes","reduce","acc","routeKey","preventRemove","PreventRemoveProvider","children","parentId","useState","setPreventedRoutesMap","Map","navigation","useContext","route","preventRemoveContextValue","setParentPrevented","setPreventRemove","id","getState","routes","every","key","Error","prevPrevented","get","nextPrevented","set","delete","isPrevented","some","useEffect","undefined","value","useMemo"],"sources":["PreventRemoveProvider.tsx"],"sourcesContent":["import { nanoid } from 'nanoid/non-secure';\nimport * as React from 'react';\nimport useLatestCallback from 'use-latest-callback';\n\nimport NavigationHelpersContext from './NavigationHelpersContext';\nimport NavigationRouteContext from './NavigationRouteContext';\nimport PreventRemoveContext, { PreventedRoutes } from './PreventRemoveContext';\n\ntype Props = {\n children: React.ReactNode;\n};\n\ntype PreventedRoutesMap = Map<\n string,\n {\n routeKey: string;\n preventRemove: boolean;\n }\n>;\n\n/**\n * Util function to transform map of prevented routes to a simpler object.\n */\nconst transformPreventedRoutes = (\n preventedRoutesMap: PreventedRoutesMap\n): PreventedRoutes => {\n const preventedRoutesToTransform = [...preventedRoutesMap.values()];\n\n const preventedRoutes = preventedRoutesToTransform.reduce<PreventedRoutes>(\n (acc, { routeKey, preventRemove }) => {\n acc[routeKey] = {\n preventRemove: acc[routeKey]?.preventRemove || preventRemove,\n };\n return acc;\n },\n {}\n );\n\n return preventedRoutes;\n};\n\n/**\n * Component used for managing which routes have to be prevented from removal in native-stack.\n */\nexport default function PreventRemoveProvider({ children }: Props) {\n const [parentId] = React.useState(() => nanoid());\n const [preventedRoutesMap, setPreventedRoutesMap] =\n React.useState<PreventedRoutesMap>(new Map());\n\n const navigation = React.useContext(NavigationHelpersContext);\n const route = React.useContext(NavigationRouteContext);\n\n const preventRemoveContextValue = React.useContext(PreventRemoveContext);\n // take `setPreventRemove` from parent context - if exist it means we're in a nested context\n const setParentPrevented = preventRemoveContextValue?.setPreventRemove;\n\n const setPreventRemove = useLatestCallback(\n (id: string, routeKey: string, preventRemove: boolean): void => {\n if (\n preventRemove &&\n (navigation == null ||\n navigation\n ?.getState()\n .routes.every((route) => route.key !== routeKey))\n ) {\n throw new Error(\n `Couldn't find a route with the key ${routeKey}. Is your component inside NavigationContent?`\n );\n }\n\n setPreventedRoutesMap((prevPrevented) => {\n // values haven't changed - do nothing\n if (\n routeKey === prevPrevented.get(id)?.routeKey &&\n preventRemove === prevPrevented.get(id)?.preventRemove\n ) {\n return prevPrevented;\n }\n\n const nextPrevented = new Map(prevPrevented);\n\n if (preventRemove) {\n nextPrevented.set(id, {\n routeKey,\n preventRemove,\n });\n } else {\n nextPrevented.delete(id);\n }\n\n return nextPrevented;\n });\n }\n );\n\n const isPrevented = [...preventedRoutesMap.values()].some(\n ({ preventRemove }) => preventRemove\n );\n\n React.useEffect(() => {\n if (route?.key !== undefined && setParentPrevented !== undefined) {\n // when route is defined (and setParentPrevented) it means we're in a nested stack\n // route.key then will be the route key of parent\n setParentPrevented(parentId, route.key, isPrevented);\n return () => {\n setParentPrevented(parentId, route.key, false);\n };\n }\n\n return;\n }, [parentId, isPrevented, route?.key, setParentPrevented]);\n\n const value = React.useMemo(\n () => ({\n setPreventRemove,\n preventedRoutes: transformPreventedRoutes(preventedRoutesMap),\n }),\n [setPreventRemove, preventedRoutesMap]\n );\n\n return (\n <PreventRemoveContext.Provider value={value}>\n {children}\n </PreventRemoveContext.Provider>\n );\n}\n"],"mappings":"AAAA,SAASA,MAAT,QAAuB,mBAAvB;AACA,OAAO,KAAKC,KAAZ,MAAuB,OAAvB;AACA,OAAOC,iBAAP,MAA8B,qBAA9B;AAEA,OAAOC,wBAAP,MAAqC,4BAArC;AACA,OAAOC,sBAAP,MAAmC,0BAAnC;AACA,OAAOC,oBAAP,MAAsD,wBAAtD;;AAcA;AACA;AACA;AACA,MAAMC,wBAAwB,GAC5BC,kBAD+B,IAEX;EACpB,MAAMC,0BAA0B,GAAG,CAAC,G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