forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {useCallback, useState} from 'react'
2import {msg} from '@lingui/macro'
3import {useLingui} from '@lingui/react'
4
5import {web} from '#/alf'
6import * as Dialog from '#/components/Dialog'
7import {type StatefulControl} from '#/components/dialogs/Context'
8import {useGlobalDialogsControlContext} from '#/components/dialogs/Context'
9import {useAccountEmailState} from '#/components/dialogs/EmailDialog/data/useAccountEmailState'
10import {Manage2FA} from '#/components/dialogs/EmailDialog/screens/Manage2FA'
11import {Update} from '#/components/dialogs/EmailDialog/screens/Update'
12import {VerificationReminder} from '#/components/dialogs/EmailDialog/screens/VerificationReminder'
13import {Verify} from '#/components/dialogs/EmailDialog/screens/Verify'
14import {type Screen, ScreenID} from '#/components/dialogs/EmailDialog/types'
15
16export type {Screen} from '#/components/dialogs/EmailDialog/types'
17export {ScreenID as EmailDialogScreenID} from '#/components/dialogs/EmailDialog/types'
18
19export function useEmailDialogControl() {
20 return useGlobalDialogsControlContext().emailDialogControl
21}
22
23export function EmailDialog() {
24 const {_} = useLingui()
25 const emailDialogControl = useEmailDialogControl()
26 const {isEmailVerified} = useAccountEmailState()
27 const onClose = useCallback(() => {
28 if (!isEmailVerified) {
29 if (emailDialogControl.value?.id === ScreenID.Verify) {
30 emailDialogControl.value.onCloseWithoutVerifying?.()
31 }
32 }
33 emailDialogControl.clear()
34 }, [isEmailVerified, emailDialogControl])
35
36 return (
37 <Dialog.Outer control={emailDialogControl.control} onClose={onClose}>
38 <Dialog.Handle />
39
40 <Dialog.ScrollableInner
41 label={_(msg`Make adjustments to email settings for your account`)}
42 style={web({maxWidth: 400})}>
43 <Inner control={emailDialogControl} />
44 <Dialog.Close />
45 </Dialog.ScrollableInner>
46 </Dialog.Outer>
47 )
48}
49
50function Inner({control}: {control: StatefulControl<Screen>}) {
51 const [screen, showScreen] = useState(() => control.value)
52
53 if (!screen) return null
54
55 switch (screen.id) {
56 case ScreenID.Update: {
57 return <Update config={screen} showScreen={showScreen} />
58 }
59 case ScreenID.Verify: {
60 return <Verify config={screen} showScreen={showScreen} />
61 }
62 case ScreenID.VerificationReminder: {
63 return <VerificationReminder config={screen} showScreen={showScreen} />
64 }
65 case ScreenID.Manage2FA: {
66 return <Manage2FA config={screen} showScreen={showScreen} />
67 }
68 default: {
69 return null
70 }
71 }
72}