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
This commit is contained in:
dan 2024-05-02 21:55:50 +01:00 committed by GitHub
parent 8ba1b10ce0
commit b86c3b486f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 105 additions and 79 deletions

View file

@ -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}
}