/
index.tsx
148 lines (138 loc) · 4.84 KB
/
index.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
import React, { useState, useCallback, useMemo, useEffect } from 'react'
import { RouteComponentProps } from 'react-router-dom'
import { Stack, DefaultButton, PrimaryButton, TextField, Spinner } from 'office-ui-fabric-react'
import { useTranslation } from 'react-i18next'
import { showOpenDialog } from 'services/remote'
import { importWalletWithKeystore } from 'states/stateProvider/actionCreators'
import { StateWithDispatch } from 'states/stateProvider/reducer'
import { useGoBack } from 'utils/hooks'
import generateWalletName from 'utils/generateWalletName'
import { ErrorCode, MAX_WALLET_NAME_LENGTH, MAX_PASSWORD_LENGTH } from 'utils/const'
interface KeystoreFields {
path: string
name: string | undefined
password: string
}
const defaultFields: KeystoreFields = {
path: '',
name: undefined,
password: '',
}
const ImportKeystore = (props: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps>) => {
const [t] = useTranslation()
const {
history,
dispatch,
settings: { wallets },
} = props
const [fields, setFields] = useState(defaultFields)
const [loading, setLoading] = useState(false)
const goBack = useGoBack(history)
useEffect(() => {
if (fields.name === undefined) {
const name = generateWalletName(wallets, wallets.length + 1, t)
setFields({
...fields,
name,
})
}
}, [wallets, fields, setFields, t])
const exsitingNames = useMemo(() => {
return wallets.map(w => w.name)
}, [wallets])
const isNameUsed = useMemo(() => {
return exsitingNames.includes(fields.name || '')
}, [exsitingNames, fields.name])
const onFileClick = useCallback(() => {
showOpenDialog({
title: 'import keystore',
onUpload: (filePaths: string[]) => {
if (!filePaths || filePaths.length === 0) {
return
}
const filePath = filePaths[0]
setFields({
...fields,
path: filePath,
})
},
})
}, [fields])
const onSubmit = useCallback(() => {
if (loading) {
return
}
setLoading(true)
setTimeout(() => {
importWalletWithKeystore({
name: fields.name || '',
keystorePath: fields.path,
password: fields.password,
})(dispatch, history).finally(() => setLoading(false))
}, 200)
}, [fields.name, fields.password, fields.path, history, dispatch, loading])
return (
<Stack verticalFill verticalAlign="center" tokens={{ childrenGap: 15 }}>
<Stack tokens={{ childrenGap: 15 }}>
{Object.entries(fields).map(([key, value]) => {
let maxLength: number | undefined
if (key === 'name') {
maxLength = MAX_WALLET_NAME_LENGTH
} else if (key === 'password') {
maxLength = MAX_PASSWORD_LENGTH
}
return (
<TextField
key={key}
onClick={key === 'path' ? onFileClick : undefined}
label={t(`import-keystore.label.${key}`)}
placeholder={t(`import-keystore.placeholder.${key}`)}
type={key === 'password' ? 'password' : 'text'}
readOnly={key === 'path'}
value={value}
validateOnLoad={false}
onGetErrorMessage={(text?: string) => {
if (text === '') {
return t(`messages.codes.${ErrorCode.FieldRequired}`, { fieldName: `keystore-${key}` })
}
if (key === 'name' && isNameUsed) {
return t(`messages.codes.${ErrorCode.FieldUsed}`, { fieldName: `name`, fieldValue: text })
}
if (text && maxLength && text.length > maxLength) {
return t(`messages.codes.${ErrorCode.FieldTooLong}`, {
fieldName: key,
fieldValue: key === 'password' ? '' : text,
length: maxLength,
})
}
return ''
}}
onChange={(_e: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newValue?: string) => {
if (newValue !== undefined) {
setFields({
...fields,
[key]: newValue,
})
}
}}
/>
)
})}
</Stack>
<Stack horizontal horizontalAlign="end" tokens={{ childrenGap: 15 }}>
<DefaultButton onClick={goBack}>{t('import-keystore.button.back')}</DefaultButton>
{loading ? (
<PrimaryButton disabled>
<Spinner />
</PrimaryButton>
) : (
<PrimaryButton disabled={!(fields.name && fields.path && fields.password && !isNameUsed)} onClick={onSubmit}>
{t('import-keystore.button.submit')}
</PrimaryButton>
)}
</Stack>
</Stack>
)
}
ImportKeystore.displayName = 'ImportKeystore'
export default ImportKeystore