import React, {useState} from 'react' import {ActivityIndicator, Keyboard, View} from 'react-native' import {type ComAtprotoServerDescribeServer} from '@atproto/api' import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import * as EmailValidator from 'email-validator' import {isNetworkError} from '#/lib/strings/errors' import {cleanError} from '#/lib/strings/errors' import {logger} from '#/logger' import {Agent} from '#/state/session/agent' import {atoms as a, useTheme} from '#/alf' import {Button, ButtonText} from '#/components/Button' import {FormError} from '#/components/forms/FormError' import {HostingProvider} from '#/components/forms/HostingProvider' import * as TextField from '#/components/forms/TextField' import {At_Stroke2_Corner0_Rounded as At} from '#/components/icons/At' import {Text} from '#/components/Typography' import {FormContainer} from './FormContainer' type ServiceDescription = ComAtprotoServerDescribeServer.OutputSchema export const ForgotPasswordForm = ({ error, serviceUrl, serviceDescription, setError, setServiceUrl, onPressBack, onEmailSent, }: { error: string serviceUrl: string serviceDescription: ServiceDescription | undefined setError: (v: string) => void setServiceUrl: (v: string) => void onPressBack: () => void onEmailSent: () => void }) => { const t = useTheme() const [isProcessing, setIsProcessing] = useState(false) const [email, setEmail] = useState('') const {_} = useLingui() const onPressSelectService = React.useCallback(() => { Keyboard.dismiss() }, []) const onPressNext = async () => { if (!EmailValidator.validate(email)) { return setError(_(msg`Your email appears to be invalid.`)) } setError('') setIsProcessing(true) try { const agent = new Agent(null, {service: serviceUrl}) await agent.com.atproto.server.requestPasswordReset({email}) onEmailSent() } catch (e: any) { const errMsg = e.toString() logger.warn('Failed to request password reset', {error: e}) setIsProcessing(false) if (isNetworkError(e)) { setError( _( msg`Unable to contact your service. Please check your Internet connection.`, ), ) } else { setError(cleanError(errMsg)) } } } return ( Reset password}> Hosting provider Email address Enter the email you used to create your account. We'll send you a "reset code" so you can set a new password. {!serviceDescription || isProcessing ? ( ) : ( )} {!serviceDescription || isProcessing ? ( Processing... ) : undefined} ) }