diff options
author | Hailey <me@haileyok.com> | 2024-08-20 15:43:40 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-20 15:43:40 -0700 |
commit | 723896a45f0fdf9612e5b6bb2a82ac7e894928ba (patch) | |
tree | 3822c9fa69da8d9ad771a2da49c69af9ca435669 /src/lib/hooks/useGoBack.ts | |
parent | e54298ec2c9a04aabe40ee7719962e2e33be23ec (diff) | |
download | voidsky-723896a45f0fdf9612e5b6bb2a82ac7e894928ba.tar.zst |
Add `list hidden` screen (#4958)
Co-authored-by: Hailey <me@haileyok.com> Co-authored-by: Eric Bailey <git@esb.lol>
Diffstat (limited to 'src/lib/hooks/useGoBack.ts')
-rw-r--r-- | src/lib/hooks/useGoBack.ts | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/lib/hooks/useGoBack.ts b/src/lib/hooks/useGoBack.ts new file mode 100644 index 000000000..59555bdac --- /dev/null +++ b/src/lib/hooks/useGoBack.ts @@ -0,0 +1,23 @@ +import {StackActions, useNavigation} from '@react-navigation/native' + +import {NavigationProp} from 'lib/routes/types' +import {router} from '#/routes' + +export function useGoBack(onGoBack?: () => unknown) { + const navigation = useNavigation<NavigationProp>() + return () => { + onGoBack?.() + if (navigation.canGoBack()) { + navigation.goBack() + } else { + navigation.navigate('HomeTab') + // Checking the state for routes ensures that web doesn't encounter errors while going back + if (navigation.getState()?.routes) { + navigation.dispatch(StackActions.push(...router.matchPath('/'))) + } else { + navigation.navigate('HomeTab') + navigation.dispatch(StackActions.popToTop()) + } + } + } +} |