import React, {useState, useEffect} from 'react' import {KeyboardAvoidingView} from 'react-native' import {useAnalytics} from 'lib/analytics/analytics' import {LoggedOutLayout} from 'view/com/util/layouts/LoggedOutLayout' import {useStores, DEFAULT_SERVICE} from 'state/index' import {ServiceDescription} from 'state/models/session' import {usePalette} from 'lib/hooks/usePalette' import {logger} from '#/logger' import {ChooseAccountForm} from './ChooseAccountForm' import {LoginForm} from './LoginForm' import {ForgotPasswordForm} from './ForgotPasswordForm' import {SetNewPasswordForm} from './SetNewPasswordForm' import {PasswordUpdatedForm} from './PasswordUpdatedForm' import {useLingui} from '@lingui/react' import {msg} from '@lingui/macro' import {useSession, SessionAccount} from '#/state/session' enum Forms { Login, ChooseAccount, ForgotPassword, SetNewPassword, PasswordUpdated, } export const Login = ({onPressBack}: {onPressBack: () => void}) => { const pal = usePalette('default') const store = useStores() const {accounts} = useSession() const {track} = useAnalytics() const {_} = useLingui() const [error, setError] = useState('') const [retryDescribeTrigger, setRetryDescribeTrigger] = useState({}) const [serviceUrl, setServiceUrl] = useState(DEFAULT_SERVICE) const [serviceDescription, setServiceDescription] = useState< ServiceDescription | undefined >(undefined) const [initialHandle, setInitialHandle] = useState('') const [currentForm, setCurrentForm] = useState( accounts.length ? Forms.ChooseAccount : Forms.Login, ) const onSelectAccount = (account?: SessionAccount) => { if (account?.service) { setServiceUrl(account.service) } setInitialHandle(account?.handle || '') setCurrentForm(Forms.Login) } const gotoForm = (form: Forms) => () => { setError('') setCurrentForm(form) } useEffect(() => { let aborted = false setError('') store.session.describeService(serviceUrl).then( desc => { if (aborted) { return } setServiceDescription(desc) }, err => { if (aborted) { return } logger.warn(`Failed to fetch service description for ${serviceUrl}`, { error: err, }) setError( _( msg`Unable to contact your service. Please check your Internet connection.`, ), ) }, ) return () => { aborted = true } }, [store.session, serviceUrl, retryDescribeTrigger, _]) const onPressRetryConnect = () => setRetryDescribeTrigger({}) const onPressForgotPassword = () => { track('Signin:PressedForgotPassword') setCurrentForm(Forms.ForgotPassword) } return ( {currentForm === Forms.Login ? ( ) : undefined} {currentForm === Forms.ChooseAccount ? ( ) : undefined} {currentForm === Forms.ForgotPassword ? ( ) : undefined} {currentForm === Forms.SetNewPassword ? ( ) : undefined} {currentForm === Forms.PasswordUpdated ? ( ) : undefined} ) }