diff options
Diffstat (limited to 'src/lib/hooks/useAccountSwitcher.ts')
-rw-r--r-- | src/lib/hooks/useAccountSwitcher.ts | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/src/lib/hooks/useAccountSwitcher.ts b/src/lib/hooks/useAccountSwitcher.ts index 6a1cea234..ad529f912 100644 --- a/src/lib/hooks/useAccountSwitcher.ts +++ b/src/lib/hooks/useAccountSwitcher.ts @@ -1,15 +1,21 @@ -import {useCallback} from 'react' +import {useCallback, useState} from 'react' +import {msg} from '@lingui/macro' +import {useLingui} from '@lingui/react' import {useAnalytics} from '#/lib/analytics/analytics' +import {logger} from '#/logger' import {isWeb} from '#/platform/detection' import {SessionAccount, useSessionApi} from '#/state/session' import {useLoggedOutViewControls} from '#/state/shell/logged-out' import * as Toast from '#/view/com/util/Toast' +import {logEvent} from '../statsig/statsig' import {LogEvents} from '../statsig/statsig' export function useAccountSwitcher() { + const [pendingDid, setPendingDid] = useState<string | null>(null) + const {_} = useLingui() const {track} = useAnalytics() - const {selectAccount, clearCurrentAccount} = useSessionApi() + const {initSession} = useSessionApi() const {requestSwitchToAccount} = useLoggedOutViewControls() const onPressSwitchAccount = useCallback( @@ -18,8 +24,12 @@ export function useAccountSwitcher() { logContext: LogEvents['account:loggedIn']['logContext'], ) => { track('Settings:SwitchAccountButtonClicked') - + if (pendingDid) { + // The session API isn't resilient to race conditions so let's just ignore this. + return + } try { + setPendingDid(account.did) if (account.accessJwt) { if (isWeb) { // We're switching accounts, which remounts the entire app. @@ -29,24 +39,26 @@ export function useAccountSwitcher() { // 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}`) - }, 100) + await initSession(account) + logEvent('account:loggedIn', {logContext, withPassword: false}) + Toast.show(_(msg`Signed in as @${account.handle}`)) } else { requestSwitchToAccount({requestedAccount: account.did}) Toast.show( - `Please sign in as @${account.handle}`, + _(msg`Please sign in as @${account.handle}`), 'circle-exclamation', ) } - } catch (e) { - Toast.show('Sorry! We need you to enter your password.') - clearCurrentAccount() // back user out to login + } catch (e: any) { + logger.error(`switch account: selectAccount failed`, { + message: e.message, + }) + } finally { + setPendingDid(null) } }, - [track, clearCurrentAccount, selectAccount, requestSwitchToAccount], + [_, track, initSession, requestSwitchToAccount, pendingDid], ) - return {onPressSwitchAccount} + return {onPressSwitchAccount, pendingDid} } |