1 | {"version":3,"sources":["useLinkTo.tsx"],"names":["useLinkTo","navigation","React","useContext","NavigationContainerRefContext","linking","LinkingContext","linkTo","useCallback","to","undefined","Error","navigate","screen","params","startsWith","options","state","getStateFromPath","config","action","dispatch","reset"],"mappings":";;;;;;;AAAA;;AAKA;;AAEA;;;;;;;;AAiBe,SAASA,SAAT,GAEX;AACF,QAAMC,UAAU,GAAGC,KAAK,CAACC,UAAN,CAAiBC,mCAAjB,CAAnB;AACA,QAAMC,OAAO,GAAGH,KAAK,CAACC,UAAN,CAAiBG,uBAAjB,CAAhB;AAEA,QAAMC,MAAM,GAAGL,KAAK,CAACM,WAAN,CACZC,EAAD,IAAuB;AACrB,QAAIR,UAAU,KAAKS,SAAnB,EAA8B;AAC5B,YAAM,IAAIC,KAAJ,CACJ,kFADI,CAAN;AAGD;;AAED,QAAI,OAAOF,EAAP,KAAc,QAAlB,EAA4B;AAC1B;AACAR,MAAAA,UAAU,CAACW,QAAX,CAAoBH,EAAE,CAACI,MAAvB,EAA+BJ,EAAE,CAACK,MAAlC;AACA;AACD;;AAED,QAAI,CAACL,EAAE,CAACM,UAAH,CAAc,GAAd,CAAL,EAAyB;AACvB,YAAM,IAAIJ,KAAJ,CAAW,iCAAgCF,EAAG,IAA9C,CAAN;AACD;;AAED,UAAM;AAAEO,MAAAA;AAAF,QAAcX,OAApB;AAEA,UAAMY,KAAK,GAAGD,OAAO,SAAP,IAAAA,OAAO,WAAP,IAAAA,OAAO,CAAEE,gBAAT,GACVF,OAAO,CAACE,gBAAR,CAAyBT,EAAzB,EAA6BO,OAAO,CAACG,MAArC,CADU,GAEV,4BAAiBV,EAAjB,EAAqBO,OAArB,aAAqBA,OAArB,uBAAqBA,OAAO,CAAEG,MAA9B,CAFJ;;AAIA,QAAIF,KAAJ,EAAW;AACT,YAAMG,MAAM,GAAG,8BAAmBH,KAAnB,EAA0BD,OAA1B,aAA0BA,OAA1B,uBAA0BA,OAAO,CAAEG,MAAnC,CAAf;;AAEA,UAAIC,MAAM,KAAKV,SAAf,EAA0B;AACxBT,QAAAA,UAAU,CAACoB,QAAX,CAAoBD,MAApB;AACD,OAFD,MAEO;AACLnB,QAAAA,UAAU,CAACqB,KAAX,CAAiBL,KAAjB;AACD;AACF,KARD,MAQO;AACL,YAAM,IAAIN,KAAJ,CAAU,iDAAV,CAAN;AACD;AACF,GAnCY,EAoCb,CAACN,OAAD,EAAUJ,UAAV,CApCa,CAAf;AAuCA,SAAOM,MAAP;AACD","sourcesContent":["import {\n getActionFromState,\n getStateFromPath,\n NavigationContainerRefContext,\n} from '@react-navigation/core';\nimport * as React from 'react';\n\nimport LinkingContext from './LinkingContext';\n\nexport type To<\n ParamList extends ReactNavigation.RootParamList = ReactNavigation.RootParamList,\n RouteName extends keyof ParamList = keyof ParamList\n> =\n | string\n | (undefined extends ParamList[RouteName]\n ? {\n screen: RouteName;\n params?: ParamList[RouteName];\n }\n : {\n screen: RouteName;\n params: ParamList[RouteName];\n });\n\nexport default function useLinkTo<\n ParamList extends ReactNavigation.RootParamList\n>() {\n const navigation = React.useContext(NavigationContainerRefContext);\n const linking = React.useContext(LinkingContext);\n\n const linkTo = React.useCallback(\n (to: To<ParamList>) => {\n if (navigation === undefined) {\n throw new Error(\n \"Couldn't find a navigation object. Is your component inside NavigationContainer?\"\n );\n }\n\n if (typeof to !== 'string') {\n // @ts-expect-error: This is fine\n navigation.navigate(to.screen, to.params);\n return;\n }\n\n if (!to.startsWith('/')) {\n throw new Error(`The path must start with '/' (${to}).`);\n }\n\n const { options } = linking;\n\n const state = options?.getStateFromPath\n ? options.getStateFromPath(to, options.config)\n : getStateFromPath(to, options?.config);\n\n if (state) {\n const action = getActionFromState(state, options?.config);\n\n if (action !== undefined) {\n navigation.dispatch(action);\n } else {\n navigation.reset(state);\n }\n } else {\n throw new Error('Failed to parse the path to a navigation state.');\n }\n },\n [linking, navigation]\n );\n\n return linkTo;\n}\n"]} |