diff options
author | dan <dan.abramov@gmail.com> | 2024-05-02 21:55:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-02 21:55:50 +0100 |
commit | b86c3b486f2e126fe887733c9e8ef6856cd67e91 (patch) | |
tree | 3d96f1decb6649af910ec146fb73ce88538cb9fe /src/lib | |
parent | 8ba1b10ce0d278a88e37d6b6c277a41673392877 (diff) | |
download | voidsky-b86c3b486f2e126fe887733c9e8ef6856cd67e91.tar.zst |
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
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/hooks/useAccountSwitcher.ts | 22 |
1 files changed, 16 insertions, 6 deletions
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<string | null>(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} } |