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:
parent
8ba1b10ce0
commit
b86c3b486f
5 changed files with 105 additions and 79 deletions
|
@ -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}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue