From 794015aef88b4488e9803e76908c00fdb53b0df2 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 7 Dec 2023 22:28:49 +0000 Subject: [PATCH] Navigate Home on account switch (#2138) --- src/lib/hooks/useAccountSwitcher.ts | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/lib/hooks/useAccountSwitcher.ts b/src/lib/hooks/useAccountSwitcher.ts index f67a586e..2f2c110d 100644 --- a/src/lib/hooks/useAccountSwitcher.ts +++ b/src/lib/hooks/useAccountSwitcher.ts @@ -1,8 +1,6 @@ import {useCallback} from 'react' -import {useNavigation} from '@react-navigation/native' import {isWeb} from '#/platform/detection' -import {NavigationProp} from '#/lib/routes/types' import {useAnalytics} from '#/lib/analytics/analytics' import {useSessionApi, SessionAccount} from '#/state/session' import * as Toast from '#/view/com/util/Toast' @@ -13,7 +11,6 @@ export function useAccountSwitcher() { const {track} = useAnalytics() const {selectAccount, clearCurrentAccount} = useSessionApi() const closeAllActiveElements = useCloseAllActiveElements() - const navigation = useNavigation() const {setShowLoggedOut} = useLoggedOutViewControls() const onPressSwitchAccount = useCallback( @@ -23,16 +20,15 @@ export function useAccountSwitcher() { try { if (account.accessJwt) { closeAllActiveElements() - await selectAccount(account) - - /* - * Mobile resets to Home on its own, so no need to handle mobile - * specifically. - */ if (isWeb) { - navigation.navigate('Home') + // We're switching accounts, which remounts the entire app. + // On mobile, this gets us Home, but on the web we also need reset the URL. + // We can't change the URL via a navigate() call because the navigator + // itself is about to unmount, and it calls pushState() too late. + // So we change the URL ourselves. The navigator will pick it up on remount. + history.pushState(null, '', '/') } - + await selectAccount(account) setTimeout(() => { Toast.show(`Signed in as @${account.handle}`) }, 100) @@ -54,7 +50,6 @@ export function useAccountSwitcher() { clearCurrentAccount, selectAccount, closeAllActiveElements, - navigation, setShowLoggedOut, ], )