import {useState} from 'react' import {View} from 'react-native' import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import {cleanError} from '#/lib/strings/errors' import {logger} from '#/logger' import {useAgent, useSession} from '#/state/session' import {ErrorMessage} from '#/view/com/util/error/ErrorMessage' import {atoms as a, useBreakpoints, useTheme, web} from '#/alf' import {Button, ButtonText} from '#/components/Button' import * as Dialog from '#/components/Dialog' import * as TextField from '#/components/forms/TextField' import {Envelope_Filled_Stroke2_Corner0_Rounded as EnvelopeIcon} from '#/components/icons/Envelope' import {InlineLinkText} from '#/components/Link' import {Loader} from '#/components/Loader' import {Text} from '#/components/Typography' import {ChangeEmailDialog} from './ChangeEmailDialog' export function VerifyEmailDialog({ control, onCloseWithoutVerifying, onCloseAfterVerifying, reasonText, changeEmailControl, reminder, }: { control: Dialog.DialogControlProps onCloseWithoutVerifying?: () => void onCloseAfterVerifying?: () => void reasonText?: string /** * if a changeEmailControl for a ChangeEmailDialog is not provided, * this component will create one for you. Using this prop * helps reduce duplication, since these dialogs are often used together. */ changeEmailControl?: Dialog.DialogControlProps reminder?: boolean }) { const agent = useAgent() const fallbackChangeEmailControl = Dialog.useDialogControl() const [didVerify, setDidVerify] = useState(false) return ( <> { if (!didVerify) { onCloseWithoutVerifying?.() return } try { await agent.resumeSession(agent.session!) onCloseAfterVerifying?.() } catch (e: unknown) { logger.error(String(e)) return } }}> {!changeEmailControl && ( )} ) } export function Inner({ setDidVerify, reasonText, changeEmailControl, reminder, }: { setDidVerify: (value: boolean) => void reasonText?: string changeEmailControl: Dialog.DialogControlProps reminder?: boolean }) { const control = Dialog.useDialogContext() const {_} = useLingui() const {currentAccount} = useSession() const agent = useAgent() const {gtMobile} = useBreakpoints() const t = useTheme() const [currentStep, setCurrentStep] = useState< 'Reminder' | 'StepOne' | 'StepTwo' | 'StepThree' >(reminder ? 'Reminder' : 'StepOne') const [confirmationCode, setConfirmationCode] = useState('') const [isProcessing, setIsProcessing] = useState(false) const [error, setError] = useState('') const uiStrings = { Reminder: { title: _(msg`Please Verify Your Email`), message: _( msg`Your email has not yet been verified. This is an important security step which we recommend.`, ), }, StepOne: { title: _(msg`Verify Your Email`), message: '', }, StepTwo: { title: _(msg`Enter Code`), message: _( msg`An email has been sent! Please enter the confirmation code included in the email below.`, ), }, StepThree: { title: _(msg`Success!`), message: _(msg`Thank you! Your email has been successfully verified.`), }, } const onSendEmail = async () => { setError('') setIsProcessing(true) try { await agent.com.atproto.server.requestEmailConfirmation() setCurrentStep('StepTwo') } catch (e: unknown) { setError(cleanError(e)) } finally { setIsProcessing(false) } } const onVerifyEmail = async () => { setError('') setIsProcessing(true) try { await agent.com.atproto.server.confirmEmail({ email: (currentAccount?.email || '').trim(), token: confirmationCode.trim(), }) } catch (e: unknown) { setError(cleanError(String(e))) setIsProcessing(false) return } setIsProcessing(false) setDidVerify(true) setCurrentStep('StepThree') } return ( {currentStep === 'Reminder' && ( )} {uiStrings[currentStep].title} {error ? ( ) : null} {currentStep === 'StepOne' ? ( {reasonText ? ( {reasonText} Don't have access to{' '} {currentAccount?.email} ?{' '} { e.preventDefault() control.close(() => { changeEmailControl.open() }) return false }}> Change your email address . ) : ( You'll receive an email at{' '} {currentAccount?.email} {' '} to verify it's you. {' '} { e.preventDefault() control.close(() => { changeEmailControl.open() }) return false }}> Need to change it? )} ) : ( {uiStrings[currentStep].message} )} {currentStep === 'StepTwo' ? ( Confirmation Code ) : null} {currentStep === 'Reminder' ? ( <> ) : currentStep === 'StepOne' ? ( <> ) : currentStep === 'StepTwo' ? ( <> ) : currentStep === 'StepThree' ? ( ) : null} ) }