mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {KeyboardAvoidingView} from 'react-native'
3import {LayoutAnimationConfig} from 'react-native-reanimated'
4import {msg} from '@lingui/macro'
5import {useLingui} from '@lingui/react'
6
7import {useAnalytics} from '#/lib/analytics/analytics'
8import {DEFAULT_SERVICE} from '#/lib/constants'
9import {logger} from '#/logger'
10import {useServiceQuery} from '#/state/queries/service'
11import {SessionAccount, useSession} from '#/state/session'
12import {useLoggedOutView} from '#/state/shell/logged-out'
13import {LoggedOutLayout} from '#/view/com/util/layouts/LoggedOutLayout'
14import {ForgotPasswordForm} from '#/screens/Login/ForgotPasswordForm'
15import {LoginForm} from '#/screens/Login/LoginForm'
16import {PasswordUpdatedForm} from '#/screens/Login/PasswordUpdatedForm'
17import {SetNewPasswordForm} from '#/screens/Login/SetNewPasswordForm'
18import {atoms as a} from '#/alf'
19import {ChooseAccountForm} from './ChooseAccountForm'
20import {ScreenTransition} from './ScreenTransition'
21
22enum Forms {
23 Login,
24 ChooseAccount,
25 ForgotPassword,
26 SetNewPassword,
27 PasswordUpdated,
28}
29
30export const Login = ({onPressBack}: {onPressBack: () => void}) => {
31 const {_} = useLingui()
32
33 const {accounts} = useSession()
34 const {track} = useAnalytics()
35 const {requestedAccountSwitchTo} = useLoggedOutView()
36 const requestedAccount = accounts.find(
37 acc => acc.did === requestedAccountSwitchTo,
38 )
39
40 const [error, setError] = React.useState<string>('')
41 const [serviceUrl, setServiceUrl] = React.useState<string>(
42 requestedAccount?.service || DEFAULT_SERVICE,
43 )
44 const [initialHandle, setInitialHandle] = React.useState<string>(
45 requestedAccount?.handle || '',
46 )
47 const [currentForm, setCurrentForm] = React.useState<Forms>(
48 requestedAccount
49 ? Forms.Login
50 : accounts.length
51 ? Forms.ChooseAccount
52 : Forms.Login,
53 )
54
55 const {
56 data: serviceDescription,
57 error: serviceError,
58 refetch: refetchService,
59 } = useServiceQuery(serviceUrl)
60
61 const onSelectAccount = (account?: SessionAccount) => {
62 if (account?.service) {
63 setServiceUrl(account.service)
64 }
65 setInitialHandle(account?.handle || '')
66 setCurrentForm(Forms.Login)
67 }
68
69 const gotoForm = (form: Forms) => {
70 setError('')
71 setCurrentForm(form)
72 }
73
74 React.useEffect(() => {
75 if (serviceError) {
76 setError(
77 _(
78 msg`Unable to contact your service. Please check your Internet connection.`,
79 ),
80 )
81 logger.warn(`Failed to fetch service description for ${serviceUrl}`, {
82 error: String(serviceError),
83 })
84 } else {
85 setError('')
86 }
87 }, [serviceError, serviceUrl, _])
88
89 const onPressForgotPassword = () => {
90 track('Signin:PressedForgotPassword')
91 setCurrentForm(Forms.ForgotPassword)
92 }
93
94 let content = null
95 let title = ''
96 let description = ''
97
98 switch (currentForm) {
99 case Forms.Login:
100 title = _(msg`Sign in`)
101 description = _(msg`Enter your username and password`)
102 content = (
103 <LoginForm
104 error={error}
105 serviceUrl={serviceUrl}
106 serviceDescription={serviceDescription}
107 initialHandle={initialHandle}
108 setError={setError}
109 setServiceUrl={setServiceUrl}
110 onPressBack={() =>
111 accounts.length ? gotoForm(Forms.ChooseAccount) : onPressBack()
112 }
113 onPressForgotPassword={onPressForgotPassword}
114 onPressRetryConnect={refetchService}
115 />
116 )
117 break
118 case Forms.ChooseAccount:
119 title = _(msg`Sign in`)
120 description = _(msg`Select from an existing account`)
121 content = (
122 <ChooseAccountForm
123 onSelectAccount={onSelectAccount}
124 onPressBack={onPressBack}
125 />
126 )
127 break
128 case Forms.ForgotPassword:
129 title = _(msg`Forgot Password`)
130 description = _(msg`Let's get your password reset!`)
131 content = (
132 <ForgotPasswordForm
133 error={error}
134 serviceUrl={serviceUrl}
135 serviceDescription={serviceDescription}
136 setError={setError}
137 setServiceUrl={setServiceUrl}
138 onPressBack={() => gotoForm(Forms.Login)}
139 onEmailSent={() => gotoForm(Forms.SetNewPassword)}
140 />
141 )
142 break
143 case Forms.SetNewPassword:
144 title = _(msg`Forgot Password`)
145 description = _(msg`Let's get your password reset!`)
146 content = (
147 <SetNewPasswordForm
148 error={error}
149 serviceUrl={serviceUrl}
150 setError={setError}
151 onPressBack={() => gotoForm(Forms.ForgotPassword)}
152 onPasswordSet={() => gotoForm(Forms.PasswordUpdated)}
153 />
154 )
155 break
156 case Forms.PasswordUpdated:
157 title = _(msg`Password updated`)
158 description = _(msg`You can now sign in with your new password.`)
159 content = (
160 <PasswordUpdatedForm onPressNext={() => gotoForm(Forms.Login)} />
161 )
162 break
163 }
164
165 return (
166 <KeyboardAvoidingView testID="signIn" behavior="padding" style={a.flex_1}>
167 <LoggedOutLayout
168 leadin=""
169 title={title}
170 description={description}
171 scrollable>
172 <LayoutAnimationConfig skipEntering skipExiting>
173 <ScreenTransition key={currentForm}>{content}</ScreenTransition>
174 </LayoutAnimationConfig>
175 </LoggedOutLayout>
176 </KeyboardAvoidingView>
177 )
178}