mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {View} from 'react-native'
3import {msg, Trans} from '@lingui/macro'
4import {useLingui} from '@lingui/react'
5
6import {isNative} from '#/platform/detection'
7import {useAgent, useSession} from '#/state/session'
8import {atoms as a, useBreakpoints, useTheme} from '#/alf'
9import {Button, ButtonIcon, ButtonText} from '#/components/Button'
10import * as Dialog from '#/components/Dialog'
11import {DialogControlProps} from '#/components/Dialog'
12import {Divider} from '#/components/Divider'
13import {ArrowRotateCounterClockwise_Stroke2_Corner0_Rounded as Resend} from '#/components/icons/ArrowRotateCounterClockwise'
14import {useIntentDialogs} from '#/components/intents/IntentDialogs'
15import {Loader} from '#/components/Loader'
16import {Text} from '#/components/Typography'
17
18export function VerifyEmailIntentDialog() {
19 const {verifyEmailDialogControl: control} = useIntentDialogs()
20
21 return (
22 <Dialog.Outer control={control}>
23 <Dialog.Handle />
24 <Inner control={control} />
25 </Dialog.Outer>
26 )
27}
28
29function Inner({}: {control: DialogControlProps}) {
30 const t = useTheme()
31 const {gtMobile} = useBreakpoints()
32 const {_} = useLingui()
33 const {verifyEmailState: state} = useIntentDialogs()
34 const [status, setStatus] = React.useState<
35 'loading' | 'success' | 'failure' | 'resent'
36 >('loading')
37 const [sending, setSending] = React.useState(false)
38 const agent = useAgent()
39 const {currentAccount} = useSession()
40
41 React.useEffect(() => {
42 ;(async () => {
43 if (!state?.code) {
44 return
45 }
46 try {
47 await agent.com.atproto.server.confirmEmail({
48 email: (currentAccount?.email || '').trim(),
49 token: state.code.trim(),
50 })
51 setStatus('success')
52 } catch (e) {
53 setStatus('failure')
54 }
55 })()
56 }, [agent.com.atproto.server, currentAccount?.email, state?.code])
57
58 const onPressResendEmail = async () => {
59 setSending(true)
60 await agent.com.atproto.server.requestEmailConfirmation()
61 setSending(false)
62 setStatus('resent')
63 }
64
65 return (
66 <Dialog.ScrollableInner
67 label={_(msg`Verify email dialog`)}
68 style={[
69 gtMobile ? {width: 'auto', maxWidth: 400, minWidth: 200} : a.w_full,
70 ]}>
71 <View style={[a.gap_xl]}>
72 {status === 'loading' ? (
73 <View style={[a.py_2xl, a.align_center, a.justify_center]}>
74 <Loader size="xl" fill={t.atoms.text_contrast_low.color} />
75 </View>
76 ) : status === 'success' ? (
77 <View style={[a.gap_sm, isNative && a.pb_xl]}>
78 <Text style={[a.font_heavy, a.text_2xl]}>
79 <Trans>Email Verified</Trans>
80 </Text>
81 <Text style={[a.text_md, a.leading_snug]}>
82 <Trans>
83 Thanks, you have successfully verified your email address. You
84 can close this dialog.
85 </Trans>
86 </Text>
87 </View>
88 ) : status === 'failure' ? (
89 <View style={[a.gap_sm]}>
90 <Text style={[a.font_heavy, a.text_2xl]}>
91 <Trans>Invalid Verification Code</Trans>
92 </Text>
93 <Text style={[a.text_md, a.leading_snug]}>
94 <Trans>
95 The verification code you have provided is invalid. Please make
96 sure that you have used the correct verification link or request
97 a new one.
98 </Trans>
99 </Text>
100 </View>
101 ) : (
102 <View style={[a.gap_sm, isNative && a.pb_xl]}>
103 <Text style={[a.font_heavy, a.text_2xl]}>
104 <Trans>Email Resent</Trans>
105 </Text>
106 <Text style={[a.text_md, a.leading_snug]}>
107 <Trans>
108 We have sent another verification email to{' '}
109 <Text style={[a.text_md, a.font_bold]}>
110 {currentAccount?.email}
111 </Text>
112 .
113 </Trans>
114 </Text>
115 </View>
116 )}
117
118 {status === 'failure' && (
119 <>
120 <Divider />
121 <Button
122 label={_(msg`Resend Verification Email`)}
123 onPress={onPressResendEmail}
124 variant="solid"
125 color="secondary_inverted"
126 size="large"
127 disabled={sending}>
128 <ButtonIcon icon={sending ? Loader : Resend} position="left" />
129 <ButtonText>
130 <Trans>Resend Email</Trans>
131 </ButtonText>
132 </Button>
133 </>
134 )}
135 </View>
136
137 <Dialog.Close />
138 </Dialog.ScrollableInner>
139 )
140}