-
Notifications
You must be signed in to change notification settings - Fork 4
/
popup.tsx
280 lines (255 loc) · 6.69 KB
/
popup.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { IconCopy, IconGithubLogo, IconLink } from "@douyinfe/semi-icons"
import { IllustrationConstruction } from "@douyinfe/semi-illustrations"
import {
Banner,
Empty,
Progress,
Radio,
RadioGroup,
Tag,
Typography
} from "@douyinfe/semi-ui"
import React, { useState } from "react"
import i18n from "./i18n"
import {
authenticator,
authenticatorOptions,
copyTextToClipboard,
dataSource,
goSettingSecurity,
type DataProps
} from "./util"
const { Title, Text } = Typography
const containerStyle: React.CSSProperties = {
width: 300,
padding: 6,
margin: 0
}
function IndexPopup() {
const [store, setStore] = useState<Record<string, DataProps>>(null)
const [groupState, setGroupState] = React.useState({})
React.useEffect(() => {
// 获取初始化数据
dataSource.get().then((res) => {
setStore(res || {})
})
}, [])
const onRadioGroupChange = (e, account) => {
setGroupState({
...groupState,
[account]: e.target.value
})
}
const handleCopy = (account, item) => {
copyTextToClipboard(item.value)
const params = store[account]
const codes = params.recoveryCodes.map((itemc) => {
if (itemc.value === item.value) {
return {
...itemc,
copyed: true
}
}
return itemc
})
const newStore: Record<string, DataProps> = {
...store,
[account]: {
...store[account],
recoveryCodes: codes
}
}
setStore(newStore)
dataSource.save(newStore)
}
if (!store) return null
if (!Object.keys(store).length) {
return (
<div style={containerStyle}>
<Empty
title={i18n("nodata")}
image={
<IllustrationConstruction style={{ width: 150, height: 150 }} />
}
description={
<Text
underline
icon={<IconLink />}
onClick={goSettingSecurity}
link={{
href: "javascript:void(0)"
}}>
{i18n("startTip")}
</Text>
}
/>
</div>
)
}
return (
<div style={containerStyle}>
{/* <button onClick={() => dataSource.save({})}>clear</button> */}
{Object.keys(store).map((account) => {
const { recoveryCodes = [], secret } = store[account]
const value = groupState[account] || "2FA"
const tfaVisible = value === "2FA"
const recoveryVisible = value === "Recovery"
return (
<div key={account}>
<RadioGroup
type="button"
value={value}
style={{ display: "flex", alignItems: "center" }}
onChange={(e) => onRadioGroupChange(e, account)}>
<Radio value="2FA">
<Tag
size="small"
type="light"
shape="circle"
style={{
maxWidth: 160,
color: tfaVisible && "var(--semi-color-primary)"
}}
prefixIcon={<IconGithubLogo />}>
{account}
</Tag>
</Radio>
<Radio value="Recovery">{i18n("recoveryCodes")}</Radio>
</RadioGroup>
<div style={{ margin: "12px 0 8px 0" }}>
{tfaVisible && <TFACode secret={secret} />}
{recoveryVisible && (
<div>
<WarningBanner data={recoveryCodes} />
<NoRecoveryCodes data={recoveryCodes} />
<RecoveryCodes
data={recoveryCodes}
account={account}
handleCopy={handleCopy}
/>
</div>
)}
</div>
</div>
)
})}
</div>
)
}
const RecoveryCodes = (props) => {
const { account, data, handleCopy } = props
return (
<div>
{data.map((item, index) => {
const { value, copyed } = item
const style = {
width: 120,
margin: "0 8px 8px 0",
textDecoration: copyed ? "line-through" : "auto"
}
return (
<Tag
key={value}
size="large"
type="light"
shape="circle"
color={`${copyed ? "grey" : "light-blue"}`}
style={style}
suffixIcon={
!copyed && (
<IconCopy
onClick={() => handleCopy(account, item)}
style={{ cursor: "pointer" }}
/>
)
}>
{value}
</Tag>
)
})}
</div>
)
}
const NoRecoveryCodes = (props) => {
const { data } = props
if (data.length > 0) return null
return (
<Empty
title={i18n("nodata")}
image={<IllustrationConstruction style={{ width: 150, height: 150 }} />}
description={
<a
onClick={goSettingSecurity}
style={{ textDecoration: "underline" }}
href="javascript:void(0)">
{i18n("noRecoveryCodes")}
</a>
}
/>
)
}
const WarningBanner = (props) => {
const { data } = props
/** 尚未使用的找回码 */
const restCodes = data.filter((item) => !item.copyed)
if (restCodes.length > 3) return null
if (!restCodes.length) return null
return (
<Banner
style={{ marginBottom: 8 }}
type="warning"
description={
<a
style={{ textDecoration: "underline" }}
href="https://github.com/settings/security?type=app#two-factor-summary">
{i18n("recoveryCodesLessTip", restCodes.length)}
</a>
}
/>
)
}
const TFACode = (props) => {
const { secret } = props
const [percent, setPercent] = React.useState(0)
React.useEffect(() => {
if (!secret) return
const timer = setInterval(() => {
const timeUsed = authenticator.timeUsed()
const percent = Math.ceil((timeUsed / authenticatorOptions.step) * 100)
setPercent(percent)
}, 1000)
return () => {
clearInterval(timer)
}
}, [secret])
if (!secret) {
return (
<Text style={{ fontWeight: 900 }}>
<span>{i18n("noSectet")}</span>
<Text
underline
icon={<IconLink />}
onClick={goSettingSecurity}
link={{
href: "javascript:void(0)"
}}>
{i18n("goToGen")}
</Text>
</Text>
)
}
const type = percent > 85 ? "warning" : "primary"
return (
<div>
<Progress
size="small"
stroke={`var(--semi-color-${type})`}
percent={percent}
/>
<Title copyable style={{ padding: "8px 0" }}>
{authenticator.generate(secret)}
</Title>
</div>
)
}
export default IndexPopup