import React 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 {useModalControls} from '#/state/modals' import {useAgent, useSession} from '#/state/session' import {ErrorMessage} from '#/view/com/util/error/ErrorMessage' import {atoms as a, useBreakpoints} from '#/alf' import {Button, ButtonText} from '#/components/Button' import * as Dialog from '#/components/Dialog' import * as TextField from '#/components/forms/TextField' import {InlineLinkText} from '#/components/Link' import {Loader} from '#/components/Loader' import {Text} from '#/components/Typography' export function VerifyEmailDialog({ control, onCloseWithoutVerifying, onCloseAfterVerifying, reasonText, }: { control: Dialog.DialogControlProps onCloseWithoutVerifying?: () => void onCloseAfterVerifying?: () => void reasonText?: string }) { const agent = useAgent() const [didVerify, setDidVerify] = React.useState(false) return ( { if (!didVerify) { onCloseWithoutVerifying?.() return } try { await agent.resumeSession(agent.session!) onCloseAfterVerifying?.() } catch (e: unknown) { logger.error(String(e)) return } }}> ) } export function Inner({ control, setDidVerify, reasonText, }: { control: Dialog.DialogControlProps setDidVerify: (value: boolean) => void reasonText?: string }) { const {_} = useLingui() const {currentAccount} = useSession() const agent = useAgent() const {openModal} = useModalControls() const {gtMobile} = useBreakpoints() const [currentStep, setCurrentStep] = React.useState< 'StepOne' | 'StepTwo' | 'StepThree' >('StepOne') const [confirmationCode, setConfirmationCode] = React.useState('') const [isProcessing, setIsProcessing] = React.useState(false) const [error, setError] = React.useState('') const uiStrings = { 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 ( {uiStrings[currentStep].title} {error ? ( ) : null} {currentStep === 'StepOne' ? ( {reasonText ? ( {reasonText} Don't have access to{' '} {currentAccount?.email} ?{' '} { e.preventDefault() control.close(() => { openModal({name: 'change-email'}) }) return false }}> Change your email address . ) : ( You'll receive an email at{' '} {currentAccount?.email} {' '} to verify it's you. {' '} { e.preventDefault() control.close(() => { openModal({name: 'change-email'}) }) return false }}> Need to change it? )} ) : ( {uiStrings[currentStep].message} )} {currentStep === 'StepTwo' ? ( Confirmation Code ) : null} {currentStep === 'StepOne' ? ( <> ) : currentStep === 'StepTwo' ? ( <> ) : currentStep === 'StepThree' ? ( ) : null} ) }