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
|
@ -22,7 +22,7 @@ export const ChooseAccountForm = ({
|
|||
onSelectAccount: (account?: SessionAccount) => void
|
||||
onPressBack: () => void
|
||||
}) => {
|
||||
const [isSwitchingAccounts, setIsSwitchingAccounts] = React.useState(false)
|
||||
const [pendingDid, setPendingDid] = React.useState<string | null>(null)
|
||||
const {track, screen} = useAnalytics()
|
||||
const {_} = useLingui()
|
||||
const {currentAccount} = useSession()
|
||||
|
@ -35,13 +35,17 @@ export const ChooseAccountForm = ({
|
|||
|
||||
const onSelect = React.useCallback(
|
||||
async (account: SessionAccount) => {
|
||||
if (pendingDid) {
|
||||
// The session API isn't resilient to race conditions so let's just ignore this.
|
||||
return
|
||||
}
|
||||
if (account.accessJwt) {
|
||||
if (account.did === currentAccount?.did) {
|
||||
setShowLoggedOut(false)
|
||||
Toast.show(_(msg`Already signed in as @${account.handle}`))
|
||||
} else {
|
||||
try {
|
||||
setIsSwitchingAccounts(true)
|
||||
setPendingDid(account.did)
|
||||
await initSession(account)
|
||||
logEvent('account:loggedIn', {
|
||||
logContext: 'ChooseAccountForm',
|
||||
|
@ -57,14 +61,22 @@ export const ChooseAccountForm = ({
|
|||
})
|
||||
onSelectAccount(account)
|
||||
} finally {
|
||||
setIsSwitchingAccounts(false)
|
||||
setPendingDid(null)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
onSelectAccount(account)
|
||||
}
|
||||
},
|
||||
[currentAccount, track, initSession, onSelectAccount, setShowLoggedOut, _],
|
||||
[
|
||||
currentAccount,
|
||||
track,
|
||||
initSession,
|
||||
pendingDid,
|
||||
onSelectAccount,
|
||||
setShowLoggedOut,
|
||||
_,
|
||||
],
|
||||
)
|
||||
|
||||
return (
|
||||
|
@ -78,7 +90,7 @@ export const ChooseAccountForm = ({
|
|||
<AccountList
|
||||
onSelectAccount={onSelect}
|
||||
onSelectOther={() => onSelectAccount()}
|
||||
isSwitchingAccounts={isSwitchingAccounts}
|
||||
pendingDid={pendingDid}
|
||||
/>
|
||||
</View>
|
||||
<View style={[a.flex_row]}>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue