From ce36724c254f11b9dfeff54721bccb14bd16fe0c Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 21 Jan 2020 14:21:10 +0100 Subject: [PATCH] Account create options, sort by created (#2161) --- packages/app-accounts/src/Account.tsx | 10 +++++----- packages/app-accounts/src/Overview.tsx | 7 +++++++ packages/app-accounts/src/modals/Create.tsx | 15 +++++++++++---- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/packages/app-accounts/src/Account.tsx b/packages/app-accounts/src/Account.tsx index ec994c1691..8c6917f1fb 100644 --- a/packages/app-accounts/src/Account.tsx +++ b/packages/app-accounts/src/Account.tsx @@ -63,14 +63,14 @@ function Account ({ address, className, filter, isFavorite, toggleFavorite }: Pr useEffect((): void => { const account = keyring.getAccount(address); - setGenesisHash((account && account.meta.genesisHash) || null); + setGenesisHash(account?.meta.genesisHash || null); setFlags({ - isDevelopment: (account && account.meta.isTesting) || false, + isDevelopment: account?.meta.isTesting || false, isEditable: (account && !(account.meta.isInjected || account.meta.isHardware)) || false, - isExternal: (account && account.meta.isExternal) || false + isExternal: account?.meta.isExternal || false }); - _setTags(account?.meta?.tags || []); - setAccName(account?.meta?.name || ''); + _setTags(account?.meta.tags || []); + setAccName(account?.meta.name || ''); }, [address]); useEffect((): void => { diff --git a/packages/app-accounts/src/Overview.tsx b/packages/app-accounts/src/Overview.tsx index 5d7b659efe..1fc9405c39 100644 --- a/packages/app-accounts/src/Overview.tsx +++ b/packages/app-accounts/src/Overview.tsx @@ -2,6 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { KeyringAddress } from '@polkadot/ui-keyring/types'; import { ComponentProps as Props } from './types'; import React, { useEffect, useState } from 'react'; @@ -49,6 +50,12 @@ function Overview ({ className, onStatusChange }: Props): React.ReactElement ({ address, isFavorite: favorites.includes(address) })) + .sort((a, b): number => { + const accA = keyring.getAccount(a.address) as KeyringAddress; + const accB = keyring.getAccount(b.address) as KeyringAddress; + + return (accA.meta.whenCreated || 0) - (accB.meta.whenCreated || 0); + }) .sort((a, b): number => a.isFavorite === b.isFavorite ? 0 diff --git a/packages/app-accounts/src/modals/Create.tsx b/packages/app-accounts/src/modals/Create.tsx index 16b2ed9176..f785652381 100644 --- a/packages/app-accounts/src/modals/Create.tsx +++ b/packages/app-accounts/src/modals/Create.tsx @@ -39,6 +39,12 @@ interface AddressState { seedType: SeedType; } +interface CreateOptions { + genesisHash?: string; + name: string; + tags?: string[]; +} + const DEFAULT_PAIR_TYPE = 'sr25519'; function deriveValidate (seed: string, derivePath: string, pairType: KeypairType): string | null { @@ -129,12 +135,12 @@ export function downloadAccount ({ json, pair }: CreateResult): void { InputAddress.setLastValue('account', pair.address); } -function createAccount (suri: string, pairType: KeypairType, name: string, password: string, success: string): ActionStatus { +function createAccount (suri: string, pairType: KeypairType, { genesisHash, name, tags = [] }: CreateOptions, password: string, success: string): ActionStatus { // we will fill in all the details below const status = { action: 'create' } as ActionStatus; try { - const result = keyring.addUri(suri, password, { name: name.trim(), tags: [] }, pairType); + const result = keyring.addUri(suri, password, { genesisHash, name, tags }, pairType); const { address } = result.pair; status.account = address; @@ -152,7 +158,7 @@ function createAccount (suri: string, pairType: KeypairType, name: string, passw function Create ({ className, onClose, onStatusChange, seed: propsSeed, type: propsType }: Props): React.ReactElement { const { t } = useTranslation(); - const { isDevelopment } = useApi(); + const { api, isDevelopment } = useApi(); const [{ address, deriveError, derivePath, isSeedValid, pairType, seed, seedType }, setAddress] = useState(generateSeed(propsSeed, '', propsSeed ? 'raw' : 'bip', propsType)); const [isConfirmationOpen, setIsConfirmationOpen] = useState(false); const [{ isNameValid, name }, setName] = useState({ isNameValid: false, name: '' }); @@ -180,7 +186,8 @@ function Create ({ className, onClose, onStatusChange, seed: propsSeed, type: pr return; } - const status = createAccount(`${seed}${derivePath}`, pairType, name, password, t('created account')); + const options = { genesisHash: isDevelopment ? undefined : api.genesisHash.toString(), name: name.trim() }; + const status = createAccount(`${seed}${derivePath}`, pairType, options, password, t('created account')); _toggleConfirmation(); onStatusChange(status);