forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {useCallback, useState} from 'react'
2import {View} from 'react-native'
3import {msg} from '@lingui/core/macro'
4import {useLingui} from '@lingui/react'
5import {Trans} from '@lingui/react/macro'
6
7import {logger} from '#/logger'
8import {type SessionAccount, useSession, useSessionApi} from '#/state/session'
9import {useLoggedOutViewControls} from '#/state/shell/logged-out'
10import * as Toast from '#/view/com/util/Toast'
11import {atoms as a, web} from '#/alf'
12import {AccountList} from '#/components/AccountList'
13import {Button, ButtonText} from '#/components/Button'
14import * as TextField from '#/components/forms/TextField'
15import {useAnalytics} from '#/analytics'
16import {IS_WEB} from '#/env'
17import {FormContainer} from './FormContainer'
18
19export const ChooseAccountForm = ({
20 onSelectAccount,
21 onPressBack,
22}: {
23 onSelectAccount: (account?: SessionAccount) => void
24 onPressBack: () => void
25}) => {
26 const [pendingDid, setPendingDid] = useState<string | null>(null)
27 const {_} = useLingui()
28 const ax = useAnalytics()
29 const {currentAccount} = useSession()
30 const {resumeSession} = useSessionApi()
31 const {setShowLoggedOut} = useLoggedOutViewControls()
32
33 const onSelect = useCallback(
34 async (account: SessionAccount) => {
35 if (pendingDid) {
36 // The session API isn't resilient to race conditions so let's just ignore this.
37 return
38 }
39 if (!account.accessJwt) {
40 // Move to login form.
41 onSelectAccount(account)
42 return
43 }
44 if (account.did === currentAccount?.did) {
45 setShowLoggedOut(false)
46 Toast.show(_(msg`Already signed in as @${account.handle}`))
47 return
48 }
49 try {
50 setPendingDid(account.did)
51 await resumeSession(account, true)
52 ax.metric('account:loggedIn', {
53 logContext: 'ChooseAccountForm',
54 withPassword: false,
55 })
56 Toast.show(_(msg`Signed in as @${account.handle}`))
57 } catch (e: any) {
58 logger.error('choose account: initSession failed', {
59 message: e instanceof Error ? e.message : 'Unknown error',
60 })
61 // Move to login form.
62 onSelectAccount(account)
63 } finally {
64 setPendingDid(null)
65 }
66 },
67 [
68 currentAccount,
69 resumeSession,
70 pendingDid,
71 onSelectAccount,
72 setShowLoggedOut,
73 _,
74 ax,
75 ],
76 )
77
78 return (
79 <FormContainer
80 testID="chooseAccountForm"
81 titleText={<Trans>Select account</Trans>}
82 style={web([a.py_2xl])}>
83 <View>
84 {IS_WEB && (
85 <TextField.LabelText>
86 <Trans>Sign in as...</Trans>
87 </TextField.LabelText>
88 )}
89 <AccountList
90 onSelectAccount={onSelect}
91 onSelectOther={() => onSelectAccount()}
92 pendingDid={pendingDid}
93 />
94 </View>
95 {IS_WEB && (
96 <View style={[a.flex_row]}>
97 <Button
98 label={_(msg`Back`)}
99 color="secondary"
100 size="large"
101 onPress={onPressBack}>
102 <ButtonText>{_(msg`Back`)}</ButtonText>
103 </Button>
104 <View style={[a.flex_1]} />
105 </View>
106 )}
107 </FormContainer>
108 )
109}