this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at f4e14626aab6b95a89af86a0cade00b9d6bbf505 65 lines 2.4 kB view raw
1import {useCallback, useState} from 'react' 2import {msg} from '@lingui/core/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 '#/components/Toast' 9import {useAnalytics} from '#/analytics' 10import {type Metrics} from '#/analytics/metrics' 11import {IS_WEB} from '#/env' 12 13export function useAccountSwitcher() { 14 const ax = useAnalytics() 15 const [pendingDid, setPendingDid] = useState<string | null>(null) 16 const {_} = useLingui() 17 const {resumeSession} = useSessionApi() 18 const {requestSwitchToAccount} = useLoggedOutViewControls() 19 20 const onPressSwitchAccount = useCallback( 21 async ( 22 account: SessionAccount, 23 logContext: Metrics['account:loggedIn']['logContext'], 24 ) => { 25 if (pendingDid) { 26 // The session API isn't resilient to race conditions so let's just ignore this. 27 return 28 } 29 try { 30 setPendingDid(account.did) 31 if (account.accessJwt) { 32 if (IS_WEB) { 33 // We're switching accounts, which remounts the entire app. 34 // On mobile, this gets us Home, but on the web we also need reset the URL. 35 // We can't change the URL via a navigate() call because the navigator 36 // itself is about to unmount, and it calls pushState() too late. 37 // So we change the URL ourselves. The navigator will pick it up on remount. 38 history.pushState(null, '', '/') 39 } 40 await resumeSession(account, true) 41 ax.metric('account:loggedIn', {logContext, withPassword: false}) 42 Toast.show(_(msg`Signed in as @${account.handle}`)) 43 } else { 44 requestSwitchToAccount({requestedAccount: account.did}) 45 Toast.show(_(msg`Please sign in as @${account.handle}`), { 46 type: 'warning', 47 }) 48 } 49 } catch (e: any) { 50 logger.error(`switch account: selectAccount failed`, { 51 message: e.message, 52 }) 53 requestSwitchToAccount({requestedAccount: account.did}) 54 Toast.show(_(msg`Please sign in as @${account.handle}`), { 55 type: 'warning', 56 }) 57 } finally { 58 setPendingDid(null) 59 } 60 }, 61 [_, ax, resumeSession, requestSwitchToAccount, pendingDid], 62 ) 63 64 return {onPressSwitchAccount, pendingDid} 65}