From b86c3b486f2e126fe887733c9e8ef6856cd67e91 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 2 May 2024 21:55:50 +0100 Subject: Improve account switcher pending state (#3827) * Protect against races * Reduce UI jank when switching accounts * Add pending state to selected account * Disable presses while pending --- src/lib/hooks/useAccountSwitcher.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'src/lib/hooks/useAccountSwitcher.ts') diff --git a/src/lib/hooks/useAccountSwitcher.ts b/src/lib/hooks/useAccountSwitcher.ts index d4e026958..de50e5336 100644 --- a/src/lib/hooks/useAccountSwitcher.ts +++ b/src/lib/hooks/useAccountSwitcher.ts @@ -12,7 +12,7 @@ import {logEvent} from '../statsig/statsig' import {LogEvents} from '../statsig/statsig' export function useAccountSwitcher() { - const [isSwitchingAccounts, setIsSwitchingAccounts] = useState(false) + const [pendingDid, setPendingDid] = useState(null) const {_} = useLingui() const {track} = useAnalytics() const {initSession, clearCurrentAccount} = useSessionApi() @@ -24,9 +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 { - setIsSwitchingAccounts(true) + setPendingDid(account.did) if (account.accessJwt) { if (isWeb) { // We're switching accounts, which remounts the entire app. @@ -57,11 +60,18 @@ export function useAccountSwitcher() { Toast.show(_(msg`Sorry! We need you to enter your password.`)) }, 100) } finally { - setIsSwitchingAccounts(false) + setPendingDid(null) } }, - [_, track, clearCurrentAccount, initSession, requestSwitchToAccount], + [ + _, + track, + clearCurrentAccount, + initSession, + requestSwitchToAccount, + pendingDid, + ], ) - return {onPressSwitchAccount, isSwitchingAccounts} + return {onPressSwitchAccount, pendingDid} } -- cgit 1.4.1