diff --git a/packages/neuron-ui/src/components/ImportKeystore/index.tsx b/packages/neuron-ui/src/components/ImportKeystore/index.tsx index 2eb1fbf04e..8361bb9ff4 100644 --- a/packages/neuron-ui/src/components/ImportKeystore/index.tsx +++ b/packages/neuron-ui/src/components/ImportKeystore/index.tsx @@ -1,4 +1,4 @@ -import React, { useState, useCallback, useMemo } from 'react' +import React, { useState, useCallback, useMemo, useEffect } from 'react' import { RouteComponentProps } from 'react-router-dom' import { Stack, DefaultButton, PrimaryButton, TextField } from 'office-ui-fabric-react' import { useTranslation } from 'react-i18next' @@ -6,6 +6,7 @@ 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' const defaultFields = { path: '', @@ -23,6 +24,16 @@ const ImportKeystore = (props: React.PropsWithoutRef { + if (fields.name === '') { + 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]) @@ -39,11 +50,9 @@ const ImportKeystore = (props: React.PropsWithoutRef { - const genName = (baseNum: number = 0): string => { - const walletName = t('wizard.wallet-suffix', { suffix: baseNum }) - if (wallets.some(wallet => wallet.name === walletName)) { - return genName(baseNum + 1) - } - return walletName - } dispatch({ type: 'name', - payload: genName(wallets.length + 1), + payload: generateWalletName(wallets, wallets.length + 1, t), }) dispatch({ type: 'password', diff --git a/packages/neuron-ui/src/utils/generateWalletName.ts b/packages/neuron-ui/src/utils/generateWalletName.ts new file mode 100644 index 0000000000..f21cc582f2 --- /dev/null +++ b/packages/neuron-ui/src/utils/generateWalletName.ts @@ -0,0 +1,8 @@ +const generateWalletName = (wallets: State.WalletIdentity[], baseNum: number = 0, t: any): string => { + const walletName = t('wizard.wallet-suffix', { suffix: baseNum }) + if (wallets.some(wallet => wallet.name === walletName)) { + return generateWalletName(wallets, baseNum + 1, t) + } + return walletName +} +export default generateWalletName