import React, {useState} from 'react' import {ActivityIndicator, SafeAreaView, StyleSheet, View} from 'react-native' import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import {useModalControls} from '#/state/modals' import {useAgent, useSession} from '#/state/session' import {usePalette} from 'lib/hooks/usePalette' import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries' import {cleanError} from 'lib/strings/errors' import {colors, s} from 'lib/styles' import {isWeb} from 'platform/detection' import {ErrorMessage} from '../util/error/ErrorMessage' import {Button} from '../util/forms/Button' import {Text} from '../util/text/Text' import * as Toast from '../util/Toast' import {ScrollView, TextInput} from './util' enum Stages { InputEmail, ConfirmCode, Done, } export const snapPoints = ['90%'] export function Component() { const pal = usePalette('default') const {currentAccount} = useSession() const agent = useAgent() const {_} = useLingui() const [stage, setStage] = useState(Stages.InputEmail) const [email, setEmail] = useState(currentAccount?.email || '') const [confirmationCode, setConfirmationCode] = useState('') const [isProcessing, setIsProcessing] = useState(false) const [error, setError] = useState('') const {isMobile} = useWebMediaQueries() const {openModal, closeModal} = useModalControls() const onRequestChange = async () => { if (email === currentAccount?.email) { setError(_(msg`Enter your new email above`)) return } setError('') setIsProcessing(true) try { const res = await agent.com.atproto.server.requestEmailUpdate() if (res.data.tokenRequired) { setStage(Stages.ConfirmCode) } else { await agent.com.atproto.server.updateEmail({email: email.trim()}) await agent.resumeSession(agent.session!) Toast.show(_(msg`Email updated`)) setStage(Stages.Done) } } catch (e) { let err = cleanError(String(e)) // TEMP // while rollout is occuring, we're giving a temporary error message // you can remove this any time after Oct2023 // -prf if (err === 'email must be confirmed (temporary)') { err = _( msg`Please confirm your email before changing it. This is a temporary requirement while email-updating tools are added, and it will soon be removed.`, ) } setError(err) } finally { setIsProcessing(false) } } const onConfirm = async () => { setError('') setIsProcessing(true) try { await agent.com.atproto.server.updateEmail({ email: email.trim(), token: confirmationCode.trim(), }) await agent.resumeSession(agent.session!) Toast.show(_(msg`Email updated`)) setStage(Stages.Done) } catch (e) { setError(cleanError(String(e))) } finally { setIsProcessing(false) } } const onVerify = async () => { closeModal() openModal({name: 'verify-email'}) } return ( {stage === Stages.InputEmail ? _(msg`Change Your Email`) : ''} {stage === Stages.ConfirmCode ? _(msg`Security Step Required`) : ''} {stage === Stages.Done ? _(msg`Email Updated`) : ''} {stage === Stages.InputEmail ? ( Enter your new email address below. ) : stage === Stages.ConfirmCode ? ( An email has been sent to your previous address,{' '} {currentAccount?.email || '(no email)'}. It includes a confirmation code which you can enter below. ) : ( Your email has been updated but not verified. As a next step, please verify your new email. )} {stage === Stages.InputEmail && ( )} {stage === Stages.ConfirmCode && ( )} {error ? ( ) : undefined} {isProcessing ? ( ) : ( {stage === Stages.InputEmail && (