From 3676afa17029fec769a2e293f95b9ba950f7d2d0 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 6 Apr 2022 10:37:37 +0800 Subject: [PATCH] My accounts display on-chain identify name (#91) * chore: display on chain identify name * chore: switch account remove confirm button * fix: keyring get address meta is empty * chore: account name component forward ref * chore: use account name component to display account name * chore: remove use account name hook * fix: darwinia account name --- public/locales/en/translation-apps.json | 1 - src/components/widget/Connection.tsx | 37 ++++++------ src/components/widget/account/AccountName.tsx | 15 +++-- .../widget/account/ActiveAccount.tsx | 59 ++++++++----------- .../widget/account/IdentAccountAddress.tsx | 8 +-- src/providers/api.tsx | 13 ++-- src/utils/network/connection.tsx | 3 +- 7 files changed, 70 insertions(+), 66 deletions(-) diff --git a/public/locales/en/translation-apps.json b/public/locales/en/translation-apps.json index 120f6d38..d29cad8b 100644 --- a/public/locales/en/translation-apps.json +++ b/public/locales/en/translation-apps.json @@ -37,7 +37,6 @@ "Check it to continue": "Check it to continue", "Claim Reward": "Claim Reward", "Claimed": "Claimed", - "Confirm": "Confirm", "Confirm to continue": "Confirm to continue", "Connect Wallet": "Connect Wallet", "Connect to Metamask": "Connect to Metamask", diff --git a/src/components/widget/Connection.tsx b/src/components/widget/Connection.tsx index 8f60f24c..e7f25bbd 100644 --- a/src/components/widget/Connection.tsx +++ b/src/components/widget/Connection.tsx @@ -1,12 +1,13 @@ import BaseIdentityIcon, { Identicon } from '@polkadot/react-identicon'; -import { Button, Card, Col, Modal, Row, Typography } from 'antd'; -import React, { CSSProperties, useMemo, useState } from 'react'; +import { Card, Col, Modal, Row, Typography } from 'antd'; +import React, { CSSProperties, useMemo, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useAccount, useApi } from '../../hooks'; import { convertToSS58 } from '../../utils'; import { ViewBrowserIcon } from '../icons'; import { ConnectPolkadot } from './ConnectPolkadot'; import { EllipsisMiddle } from './EllipsisMiddle'; +import { AccountName } from './account/AccountName'; function ActiveAccount({ children, @@ -25,8 +26,9 @@ function ActiveAccount({ textClassName?: string; onClick?: React.MouseEventHandler; }>) { + const ref = useRef(null); const { network } = useApi(); - const { accountWithMeta } = useAccount(); + const { account } = useAccount(); const containerCls = useMemo( () => `flex items-center justify-between leading-normal whitespace-nowrap p-1 overflow-hidden bg-${network.name} @@ -39,7 +41,8 @@ function ActiveAccount({
- {accountWithMeta.meta.name} + + {ref.current?.textContent} {children}
@@ -103,24 +106,24 @@ export function Connection() { - - {account} - + + - + + {t('View in Subscan')} + diff --git a/src/components/widget/account/AccountName.tsx b/src/components/widget/account/AccountName.tsx index a1753218..1d63ff6f 100644 --- a/src/components/widget/account/AccountName.tsx +++ b/src/components/widget/account/AccountName.tsx @@ -4,13 +4,14 @@ import { TypeRegistry } from '@polkadot/types/create'; import { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces'; import { stringToU8a } from '@polkadot/util'; import { isFunction } from 'lodash'; -import { ReactNode, useEffect, useState } from 'react'; +import { ReactNode, useEffect, useState, forwardRef } from 'react'; import { from } from 'rxjs'; import { useApi } from '../../../hooks'; import { getAddressName } from '../../../utils'; interface AccountNameProps { account: string; + className?: string; } const registry = new TypeRegistry(); @@ -122,8 +123,8 @@ function extractIdentity(address: string, identity: DeriveAccountRegistration): return elem; } -export function AccountName({ account }: AccountNameProps) { - const [name, setName] = useState(account); +export const AccountName = forwardRef(({ account, className }, ref) => { + const [name, setName] = useState(() => extractName(account)); const { api } = useApi(); useEffect(() => { @@ -149,5 +150,9 @@ export function AccountName({ account }: AccountNameProps) { return () => sub$$.unsubscribe(); }, [account, api]); - return {name}; -} + return ( + + {name} + + ); +}); diff --git a/src/components/widget/account/ActiveAccount.tsx b/src/components/widget/account/ActiveAccount.tsx index 370ccfaa..3f47b07b 100644 --- a/src/components/widget/account/ActiveAccount.tsx +++ b/src/components/widget/account/ActiveAccount.tsx @@ -6,9 +6,28 @@ import { useTranslation } from 'react-i18next'; import { useAccount, useApi } from '../../../hooks'; import { convertToSS58 } from '../../../utils'; import { EllipsisMiddle } from '../EllipsisMiddle'; +import { IAccountMeta } from '../../../model'; +import { AccountName } from './AccountName'; const iconSize = 36; +const AccountWithIdentify = ({ value }: { value: IAccountMeta }) => { + return ( + <> + + + + + + + ); +}; + // eslint-disable-next-line complexity export function ActiveAccount() { const { @@ -16,7 +35,6 @@ export function ActiveAccount() { network, } = useApi(); const { t } = useTranslation(); - const [selected, setSelected] = useState(); const { account, setAccount } = useAccount(); const [isVisible, setIsVisible] = useState(false); const displayAccounts = useMemo( @@ -58,36 +76,20 @@ export function ActiveAccount() { maskClosable={false} onCancel={() => setIsVisible(false)} bodyStyle={{ - maxHeight: '50vh', + maxHeight: '70vh', overflow: 'scroll', }} - footer={ - accounts?.length - ? [ - , - ] - : null - } + footer={null} > {accounts?.length ? ( { - setSelected(event.target.value); + if (event.target.value !== account) { + setAccount(event.target.value); + } + setIsVisible(false); }} > {displayAccounts.map((item) => ( @@ -96,16 +98,7 @@ export function ActiveAccount() { key={item.address} className={`radio-list account-select-btn-group-${network.name}`} > - - - {item.meta?.name} - - + ))} diff --git a/src/components/widget/account/IdentAccountAddress.tsx b/src/components/widget/account/IdentAccountAddress.tsx index 5ecc1af4..61a9cffd 100644 --- a/src/components/widget/account/IdentAccountAddress.tsx +++ b/src/components/widget/account/IdentAccountAddress.tsx @@ -1,6 +1,7 @@ import Identicon from '@polkadot/react-identicon'; import { IAccountMeta } from '../../../model'; import { EllipsisMiddle } from '../EllipsisMiddle'; +import { AccountName } from '../../../components/widget/account/AccountName'; interface IdentAccountProps { account: IAccountMeta | undefined; @@ -14,13 +15,12 @@ export function IdentAccountAddress({ account, className = '', iconSize = defaul if (!account) { return null; } - - const { address, meta } = account; + const { address } = account; return (
- - {!!meta?.name && {meta?.name}} + + -
diff --git a/src/providers/api.tsx b/src/providers/api.tsx index 4c67ed8b..548ce254 100644 --- a/src/providers/api.tsx +++ b/src/providers/api.tsx @@ -3,6 +3,7 @@ import { Alert } from 'antd'; import { createContext, useCallback, useEffect, useMemo, useReducer, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { EMPTY, Subscription } from 'rxjs'; +import keyring from '@polkadot/ui-keyring'; import { BallScalePulse } from '../components/widget/BallScalePulse'; import { crabConfig, THEME } from '../config'; import { @@ -53,10 +54,14 @@ function accountReducer(state: StoreState, action: Action): Sto ...state, connection: { ...rest, - accounts: accounts.map((item) => ({ - ...item, - address: convertToSS58(item.address, state.network.ss58Prefix), - })), + accounts: accounts.map((item) => { + const address = convertToSS58(item.address, state.network.ss58Prefix); + keyring.saveAddress(address, item.meta); + return { + ...item, + address, + }; + }), }, }; } diff --git a/src/utils/network/connection.tsx b/src/utils/network/connection.tsx index cc47ae21..7f7bcf0e 100644 --- a/src/utils/network/connection.tsx +++ b/src/utils/network/connection.tsx @@ -29,7 +29,6 @@ import { IAccountMeta, PolkadotConnection, } from '../../model'; -import { getAddressMeta } from '../helper'; import { entrance } from './entrance'; import { isMetamaskInstalled, isNetworkConsistent } from './network'; import { switchMetamaskNetwork } from './switch'; @@ -52,7 +51,7 @@ export const getPolkadotConnection: (network: ChainConfig) => Observable !injectedAddress.includes(key)); const local: IAccountMeta[] = source.map((address) => { - const meta = getAddressMeta(address); + const meta = injected.find((item) => item.address === address)?.meta || {}; return { address,