4
4
Dialog ,
5
5
H4 ,
6
6
Icon ,
7
- InputGroup ,
8
7
Menu ,
9
8
MenuDivider ,
10
9
MenuItem ,
@@ -15,20 +14,13 @@ import {
15
14
} from '@blueprintjs/core'
16
15
import { Popover2 } from '@blueprintjs/popover2'
17
16
18
- import { requestActivation , requestActivationCode } from 'apis/auth'
19
- import { useAtom } from 'jotai'
20
- import { ComponentType , FC , useMemo , useRef , useState } from 'react'
21
- import { FieldValues , useController , useForm } from 'react-hook-form'
17
+ import { useAtom , useSetAtom } from 'jotai'
18
+ import { ComponentType , FC , useState } from 'react'
22
19
23
20
import { LoginPanel } from 'components/account/LoginPanel'
24
- import { EditorFieldProps } from 'components/editor/EditorFieldProps'
25
21
import { authAtom } from 'store/auth'
26
- import { NetworkError } from 'utils/error'
27
22
import { useCurrentSize } from 'utils/useCurrenSize'
28
- import { useNetworkState } from 'utils/useNetworkState'
29
- import { wrapErrorMessage } from 'utils/wrapErrorMessage'
30
23
31
- import { FormField2 } from './FormField'
32
24
import {
33
25
GlobalErrorBoundary ,
34
26
withGlobalErrorBoundary ,
@@ -37,139 +29,9 @@ import { AppToaster } from './Toaster'
37
29
import { EditDialog } from './account/EditDialog'
38
30
import { RegisterPanel } from './account/RegisterPanel'
39
31
40
- interface ActivationFormValues {
41
- code : string
42
- }
43
-
44
- const ActivationDialog : FC < {
45
- isOpen ?: boolean
46
- onClose : ( ) => void
47
- } > = ( { isOpen, onClose } ) => {
48
- const {
49
- control,
50
- handleSubmit,
51
- formState : { errors, isValid, isDirty, isSubmitting } ,
52
- } = useForm < ActivationFormValues > ( )
53
-
54
- const [ authState , setAuthState ] = useAtom ( authAtom )
55
- const latestAuthState = useRef ( authState )
56
- latestAuthState . current = authState
57
-
58
- const onSubmit = async ( { code } ) => {
59
- await wrapErrorMessage (
60
- ( e : NetworkError ) => `激活失败:${ e . message } ` ,
61
- requestActivation ( code ) ,
62
- )
63
-
64
- setAuthState ( {
65
- ...latestAuthState . current ,
66
- activated : true ,
67
- } )
68
-
69
- AppToaster . show ( {
70
- message : '激活成功' ,
71
- intent : 'success' ,
72
- } )
73
-
74
- onClose ( )
75
- }
76
-
77
- return (
78
- < Dialog
79
- className = "w-full max-w-xl"
80
- isOpen = { isOpen }
81
- title = "激活 MAA Copilot 账户"
82
- icon = "key"
83
- onClose = { onClose }
84
- >
85
- < form onSubmit = { handleSubmit ( onSubmit ) } >
86
- < div className = "flex flex-col px-4 pt-4" >
87
- < FormField2
88
- field = "code"
89
- label = "激活码"
90
- description = "激活码可在您注册时使用的邮箱中找到"
91
- error = { errors . code }
92
- >
93
- < ActivationInputGroup name = "code" control = { control } />
94
- </ FormField2 >
95
-
96
- < Button
97
- disabled = { ( ! isValid && ! isDirty ) || isSubmitting }
98
- intent = "primary"
99
- loading = { isSubmitting }
100
- type = "submit"
101
- icon = "envelope"
102
- large
103
- >
104
- 激活
105
- </ Button >
106
- </ div >
107
- </ form >
108
- </ Dialog >
109
- )
110
- }
111
-
112
- const ActivationInputGroup = < T extends FieldValues > ( {
113
- name,
114
- control,
115
- } : EditorFieldProps < T > ) => {
116
- const {
117
- field : { onChange, onBlur, ref } ,
118
- } = useController ( {
119
- name,
120
- control,
121
- rules : { required : '请输入激活码' } ,
122
- } )
123
-
124
- return (
125
- < div className = "flex" >
126
- < InputGroup
127
- large
128
- leftIcon = "lock"
129
- onChange = { onChange }
130
- onBlur = { onBlur }
131
- placeholder = "请输入您的激活码"
132
- ref = { ref }
133
- className = "flex-grow font-mono"
134
- autoComplete = "off"
135
- // eslint-disable-next-line jsx-a11y/no-autofocus
136
- autoFocus
137
- />
138
- < ActivationCodeRequestButton />
139
- </ div >
140
- )
141
- }
142
-
143
- const ActivationCodeRequestButton : FC = ( ) => {
144
- const { networkState, start, finish } = useNetworkState ( )
145
-
146
- const handleClick = ( ) => {
147
- start ( )
148
- wrapErrorMessage (
149
- ( e : NetworkError ) => `获取激活码失败:${ e . message } ` ,
150
- requestActivationCode ( ) ,
151
- )
152
- . then ( ( ) => {
153
- finish ( null )
154
- AppToaster . show ( {
155
- message : '激活码已发送至您的邮箱' ,
156
- intent : 'success' ,
157
- } )
158
- } )
159
- . catch ( ( e ) => finish ( e ) )
160
- }
161
-
162
- return (
163
- < Button icon = "reset" onClick = { handleClick } loading = { networkState . loading } >
164
- 重新发送
165
- </ Button >
166
- )
167
- }
168
-
169
32
const AccountMenu : FC = ( ) => {
170
- const [ authState , setAuthState ] = useAtom ( authAtom )
33
+ const setAuthState = useSetAtom ( authAtom )
171
34
const [ logoutDialogOpen , setLogoutDialogOpen ] = useState ( false )
172
- const [ activationDialogOpen , setActivationDialogOpen ] = useState ( false )
173
35
const [ editDialogOpen , setEditDialogOpen ] = useState ( false )
174
36
175
37
const handleLogout = ( ) => {
@@ -180,21 +42,6 @@ const AccountMenu: FC = () => {
180
42
} )
181
43
}
182
44
183
- const menuItems = useMemo ( ( ) => {
184
- const items : JSX . Element [ ] = [ ]
185
- if ( ! authState . activated ) {
186
- items . push (
187
- < MenuItem
188
- shouldDismissPopover = { false }
189
- icon = "key"
190
- text = "激活账户..."
191
- onClick = { ( ) => setActivationDialogOpen ( true ) }
192
- /> ,
193
- )
194
- }
195
- return items
196
- } , [ authState ] )
197
-
198
45
return (
199
46
< >
200
47
< Alert
@@ -211,11 +58,6 @@ const AccountMenu: FC = () => {
211
58
< p > 确定要退出登录吗?</ p >
212
59
</ Alert >
213
60
214
- < ActivationDialog
215
- isOpen = { activationDialogOpen }
216
- onClose = { ( ) => setActivationDialogOpen ( false ) }
217
- />
218
-
219
61
< EditDialog
220
62
isOpen = { editDialogOpen }
221
63
onClose = { ( ) => setEditDialogOpen ( false ) }
@@ -229,9 +71,6 @@ const AccountMenu: FC = () => {
229
71
onClick = { ( ) => setEditDialogOpen ( true ) }
230
72
/>
231
73
< MenuDivider />
232
- { menuItems }
233
-
234
- { menuItems . length > 0 && < MenuDivider /> }
235
74
236
75
< MenuItem
237
76
shouldDismissPopover = { false }
0 commit comments