diff --git a/src/lib/hooks/useAccountSwitcher.ts b/src/lib/hooks/useAccountSwitcher.ts index 2b4f978e4f..3432c42c0c 100644 --- a/src/lib/hooks/useAccountSwitcher.ts +++ b/src/lib/hooks/useAccountSwitcher.ts @@ -1,10 +1,11 @@ import {useCallback} from 'react' import {useAnalytics} from '#/lib/analytics/analytics' -import {useSessionApi, SessionAccount} from '#/state/session' -import * as Toast from '#/view/com/util/Toast' -import {useCloseAllActiveElements} from '#/state/util' +import {isWeb} from '#/platform/detection' +import {SessionAccount, useSessionApi} from '#/state/session' import {useLoggedOutViewControls} from '#/state/shell/logged-out' +import {useCloseAllActiveElements} from '#/state/util' +import * as Toast from '#/view/com/util/Toast' import {LogEvents} from '../statsig/statsig' export function useAccountSwitcher() { @@ -23,6 +24,14 @@ export function useAccountSwitcher() { try { if (account.accessJwt) { closeAllActiveElements() + if (isWeb) { + // 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, logContext) setTimeout(() => { Toast.show(`Signed in as @${account.handle}`) diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index 644cadcdde..ff5cd9075b 100644 --- a/src/state/session/index.tsx +++ b/src/state/session/index.tsx @@ -211,7 +211,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { const clearCurrentAccount = React.useCallback(() => { logger.warn(`session: clear current account`) __globalAgent = PUBLIC_BSKY_AGENT - queryClient.clear() + queryClient.resetQueries() setStateAndPersist(s => ({ ...s, currentAccount: undefined, @@ -286,7 +286,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { ) __globalAgent = agent - queryClient.clear() + queryClient.resetQueries() upsertAccount(account) logger.debug(`session: created account`, {}, logger.DebugContext.session) @@ -334,7 +334,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { __globalAgent = agent // @ts-ignore if (IS_DEV && isWeb) window.agent = agent - queryClient.clear() + queryClient.resetQueries() upsertAccount(account) logger.debug(`session: logged in`, {}, logger.DebugContext.session) @@ -415,7 +415,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { */ agent.session = prevSession __globalAgent = agent - queryClient.clear() + queryClient.resetQueries() upsertAccount(account) if (prevSession.deactivated) { @@ -436,7 +436,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { }) __globalAgent = PUBLIC_BSKY_AGENT - queryClient.clear() + queryClient.resetQueries() }) } else { logger.debug(`session: attempting to resume using previous session`) @@ -457,7 +457,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { __globalAgent = PUBLIC_BSKY_AGENT } finally { - queryClient.clear() + queryClient.resetQueries() } } @@ -558,14 +558,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) { setState(s => ({...s, isSwitchingAccounts: true})) try { await initSession(account) - if (isWeb) { - // 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, '', '/') - } setState(s => ({...s, isSwitchingAccounts: false})) logEvent('account:loggedIn', {logContext, withPassword: false}) } catch (e) {