From 9388f37ba8b5d98697765108e0894cf1b127f50b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 26 Jan 2021 14:38:11 +0100 Subject: [PATCH] Add Ledger account name when adding (#4496) --- packages/page-accounts/src/modals/Ledger.tsx | 38 ++++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/packages/page-accounts/src/modals/Ledger.tsx b/packages/page-accounts/src/modals/Ledger.tsx index 89cb8f6b67..c524ac9fc9 100644 --- a/packages/page-accounts/src/modals/Ledger.tsx +++ b/packages/page-accounts/src/modals/Ledger.tsx @@ -6,7 +6,7 @@ import type { Ledger } from '@polkadot/ui-keyring'; import React, { useCallback, useRef, useState } from 'react'; -import { Button, Dropdown, MarkError, Modal } from '@polkadot/react-components'; +import { Button, Dropdown, Input, MarkError, Modal } from '@polkadot/react-components'; import { useApi, useLedger } from '@polkadot/react-hooks'; import { keyring } from '@polkadot/ui-keyring'; @@ -25,14 +25,14 @@ interface Props { const AVAIL = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]; // query the ledger for the address, adding it to the keyring -async function queryLedger (api: ApiPromise, getLedger: () => Ledger, accountOffset: number, addressOffset: number): Promise { +async function queryLedger (api: ApiPromise, getLedger: () => Ledger, name: string, accountOffset: number, addressOffset: number): Promise { const { address } = await getLedger().getAddress(false, accountOffset, addressOffset); keyring.addHardware(address, 'ledger', { accountOffset, addressOffset, genesisHash: api.genesisHash.toHex(), - name: `ledger ${accountOffset}/${addressOffset}` + name: name || `ledger ${accountOffset}/${addressOffset}` }); } @@ -43,6 +43,7 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement const [accIndex, setAccIndex] = useState(0); const [addIndex, setAddIndex] = useState(0); const [error, setError] = useState(null); + const [{ isNameValid, name }, setName] = useState({ isNameValid: false, name: '' }); const [isBusy, setIsBusy] = useState(false); const accOps = useRef(AVAIL.map((value): Option => ({ @@ -55,12 +56,17 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement value }))); + const _onChangeName = useCallback( + (name: string) => setName({ isNameValid: !!name.trim(), name }), + [] + ); + const _onSave = useCallback( (): void => { setError(null); setIsBusy(true); - queryLedger(api, getLedger, accIndex, addIndex) + queryLedger(api, getLedger, name, accIndex, addIndex) .then(() => onClose()) .catch((error): void => { console.error(error); @@ -69,7 +75,7 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement setError(error); }); }, - [accIndex, addIndex, api, getLedger, onClose] + [accIndex, addIndex, api, getLedger, name, onClose] ); return ( @@ -79,6 +85,23 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement size='large' > + + + ('Name given to this account to uniquely identity the account to yourself.')} + isError={!isNameValid} + label={t('name')} + onChange={_onChangeName} + placeholder={t('account name')} + value={name} + /> + + + {t('The name for this account as it will appear under your accounts.')} + + /> -

{t('The account type that you wish to create. This is the top-level derivation.')}

+ {t('The account type that you wish to create. This is the top-level derivation.')}
@@ -107,7 +130,7 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement )} -

{t('The address index on the account that you wish to add. This is the second-level derivation.')}

+ {t('The address index on the account that you wish to add. This is the second-level derivation.')}
@@ -115,6 +138,7 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement