Bluesky app fork with some witchin' additions 馃挮
at main 60 lines 2.1 kB view raw
1import {useCallback, useState} from 'react' 2import {msg} from '@lingui/macro' 3import {useLingui} from '@lingui/react' 4 5import {logger} from '#/logger' 6import {type SessionAccount, useSessionApi} from '#/state/session' 7import {useLoggedOutViewControls} from '#/state/shell/logged-out' 8import * as Toast from '#/view/com/util/Toast' 9import {storeNavigationStateForAccountSwitch} from '#/Navigation' 10import {logEvent} from '../statsig/statsig' 11import {type LogEvents} from '../statsig/statsig' 12 13export function useAccountSwitcher() { 14 const [pendingDid, setPendingDid] = useState<string | null>(null) 15 const {_} = useLingui() 16 const {resumeSession} = useSessionApi() 17 const {requestSwitchToAccount} = useLoggedOutViewControls() 18 19 const onPressSwitchAccount = useCallback( 20 async ( 21 account: SessionAccount, 22 logContext: LogEvents['account:loggedIn']['logContext'], 23 ) => { 24 if (pendingDid) { 25 // The session API isn't resilient to race conditions so let's just ignore this. 26 return 27 } 28 try { 29 setPendingDid(account.did) 30 if (account.accessJwt) { 31 // Store navigation state before switching so user stays on the same page 32 storeNavigationStateForAccountSwitch() 33 await resumeSession(account, true) 34 logEvent('account:loggedIn', {logContext, withPassword: false}) 35 Toast.show(_(msg`Signed in as @${account.handle}`)) 36 } else { 37 requestSwitchToAccount({requestedAccount: account.did}) 38 Toast.show( 39 _(msg`Please sign in as @${account.handle}`), 40 'circle-exclamation', 41 ) 42 } 43 } catch (e: any) { 44 logger.error(`switch account: selectAccount failed`, { 45 message: e.message, 46 }) 47 requestSwitchToAccount({requestedAccount: account.did}) 48 Toast.show( 49 _(msg`Please sign in as @${account.handle}`), 50 'circle-exclamation', 51 ) 52 } finally { 53 setPendingDid(null) 54 } 55 }, 56 [_, resumeSession, requestSwitchToAccount, pendingDid], 57 ) 58 59 return {onPressSwitchAccount, pendingDid} 60}