Skip to content

Commit 694ac98

Browse files
committed
feat(neuron-ui): use the same naming strategy as importing mnemonic words
1 parent a8c9543 commit 694ac98

3 files changed

Lines changed: 22 additions & 11 deletions

File tree

packages/neuron-ui/src/components/ImportKeystore/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import React, { useState, useCallback, useMemo } from 'react'
1+
import React, { useState, useCallback, useMemo, useEffect } from 'react'
22
import { RouteComponentProps } from 'react-router-dom'
33
import { Stack, DefaultButton, PrimaryButton, TextField } from 'office-ui-fabric-react'
44
import { useTranslation } from 'react-i18next'
55
import { showOpenDialog } from 'services/remote'
66
import { importWalletWithKeystore } from 'states/stateProvider/actionCreators'
77
import { StateWithDispatch } from 'states/stateProvider/reducer'
88
import { useGoBack } from 'utils/hooks'
9+
import generateWalletName from 'utils/generateWalletName'
910

1011
const defaultFields = {
1112
path: '',
@@ -23,6 +24,16 @@ const ImportKeystore = (props: React.PropsWithoutRef<StateWithDispatch & RouteCo
2324
const [fields, setFields] = useState(defaultFields)
2425
const goBack = useGoBack(history)
2526

27+
useEffect(() => {
28+
if (fields.name === '') {
29+
const name = generateWalletName(wallets, wallets.length + 1, t)
30+
setFields({
31+
...fields,
32+
name,
33+
})
34+
}
35+
}, [wallets, fields, setFields, t])
36+
2637
const exsitingNames = useMemo(() => {
2738
return wallets.map(w => w.name)
2839
}, [wallets])
@@ -39,11 +50,9 @@ const ImportKeystore = (props: React.PropsWithoutRef<StateWithDispatch & RouteCo
3950
return
4051
}
4152
const filePath = filePaths[0]
42-
const filename = filePath.split('/').pop() || 'Imported wallet'
4353
setFields({
4454
...fields,
4555
path: filePath,
46-
name: filename,
4756
})
4857
},
4958
})

packages/neuron-ui/src/components/WalletWizard/index.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { createWalletWithMnemonic, importWalletWithMnemonic } from 'states/state
1919
import { Routes, MnemonicAction } from 'utils/const'
2020
import { buttonGrommetIconStyles } from 'utils/icons'
2121
import { verifyPasswordComplexity } from 'utils/validators'
22+
import generateWalletName from 'utils/generateWalletName'
2223

2324
export enum WalletWizardPath {
2425
Welcome = '/welcome',
@@ -212,16 +213,9 @@ const Submission = ({
212213
const message = 'wizard.set-wallet-name-and-password'
213214

214215
useEffect(() => {
215-
const genName = (baseNum: number = 0): string => {
216-
const walletName = t('wizard.wallet-suffix', { suffix: baseNum })
217-
if (wallets.some(wallet => wallet.name === walletName)) {
218-
return genName(baseNum + 1)
219-
}
220-
return walletName
221-
}
222216
dispatch({
223217
type: 'name',
224-
payload: genName(wallets.length + 1),
218+
payload: generateWalletName(wallets, wallets.length + 1, t),
225219
})
226220
dispatch({
227221
type: 'password',
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const generateWalletName = (wallets: State.WalletIdentity[], baseNum: number = 0, t: any): string => {
2+
const walletName = t('wizard.wallet-suffix', { suffix: baseNum })
3+
if (wallets.some(wallet => wallet.name === walletName)) {
4+
return generateWalletName(wallets, baseNum + 1, t)
5+
}
6+
return walletName
7+
}
8+
export default generateWalletName

0 commit comments

Comments
 (0)