Something went wrong. Try again.
Bluesky app fork with some witchin' additions 💫 forked from jollywhoppers.com/witchsky.app
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import {useCallback, useState} from 'react'import {msg} from '@lingui/macro'import {useLingui} from '@lingui/react'
import {logger} from '#/logger'import {type SessionAccount, useSessionApi} from '#/state/session'import {useLoggedOutViewControls} from '#/state/shell/logged-out'import * as Toast from '#/view/com/util/Toast'import {useAnalytics} from '#/analytics'import {type Metrics} from '#/analytics/metrics'import {storeNavigationStateForAccountSwitch} from '#/Navigation'
export function useAccountSwitcher() { const ax = useAnalytics() const [pendingDid, setPendingDid] = useState<string | null>(null) const {_} = useLingui() const {resumeSession} = useSessionApi() const {requestSwitchToAccount} = useLoggedOutViewControls()
const onPressSwitchAccount = useCallback( async ( account: SessionAccount, logContext: Metrics['account:loggedIn']['logContext'], ) => { if (pendingDid) { // The session API isn't resilient to race conditions so let's just ignore this. return } try { setPendingDid(account.did) if (account.accessJwt) { // Store navigation state before switching so user stays on the same page storeNavigationStateForAccountSwitch() await resumeSession(account, true) ax.metric('account:loggedIn', {logContext, withPassword: false}) Toast.show(_(msg`Signed in as @${account.handle}`)) } else { requestSwitchToAccount({requestedAccount: account.did}) Toast.show( _(msg`Please sign in as @${account.handle}`), 'circle-exclamation', ) } } catch (e: any) { logger.error(`switch account: selectAccount failed`, { message: e.message, }) requestSwitchToAccount({requestedAccount: account.did}) Toast.show( _(msg`Please sign in as @${account.handle}`), 'circle-exclamation', ) } finally { setPendingDid(null) } }, [_, ax, resumeSession, requestSwitchToAccount, pendingDid], )
return {onPressSwitchAccount, pendingDid}}