From 8d812116b700a592c4e3df4513caa478bdc8a437 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 25 Feb 2020 17:18:10 +0100 Subject: [PATCH] Minor AddressName cleanups (#2302) * Minor AddressName cleanups * Split Judgement modal * Re-add undefined --- packages/react-components/src/AccountName.tsx | 129 +++++------------- .../src/AccountNameJudgement.tsx | 93 +++++++++++++ 2 files changed, 126 insertions(+), 96 deletions(-) create mode 100644 packages/react-components/src/AccountNameJudgement.tsx diff --git a/packages/react-components/src/AccountName.tsx b/packages/react-components/src/AccountName.tsx index b2b97cfe58..27d636c3d2 100644 --- a/packages/react-components/src/AccountName.tsx +++ b/packages/react-components/src/AccountName.tsx @@ -13,14 +13,10 @@ import { Option } from '@polkadot/types'; import { useTranslation } from './translate'; import { getAddressName } from './util'; +import AccountNameJudgement from './AccountNameJudgement'; import AddressMini from './AddressMini'; import Badge from './Badge'; -import Dropdown from './Dropdown'; import Icon from './Icon'; -import Input from './Input'; -import InputAddress from './InputAddress'; -import Modal from './Modal'; -import TxButton from './TxButton'; interface Props extends BareProps { children?: React.ReactNode; @@ -29,24 +25,20 @@ interface Props extends BareProps { onClick?: () => void; override?: React.ReactNode; toggle?: any; - value?: AccountId | AccountIndex | Address | string | null | Uint8Array; - withShort?: boolean; + value: AccountId | AccountIndex | Address | string | Uint8Array | null | undefined; } -const JUDGEMENT_ENUM = [ - { value: 0, text: 'Unknown' }, - { value: 1, text: 'Fee paid' }, - { value: 2, text: 'Reasonable' }, - { value: 3, text: 'Known good' }, - { value: 4, text: 'Out of date' }, - { value: 5, text: 'Low quality' } -]; const DISPLAY_KEYS = ['display', 'legal', 'email', 'web', 'twitter', 'riot']; const nameCache: Map = new Map(); function defaultOrAddr (defaultName = '', _address: AccountId | AccountIndex | Address | string | Uint8Array, _accountIndex?: AccountIndex | null): [[React.ReactNode, React.ReactNode | null], boolean, boolean] { const accountId = _address.toString(); const accountIndex = (_accountIndex || '').toString(); + + if (!accountId) { + return [[defaultName, null], false, false]; + } + const [isAddressExtracted,, extracted] = getAddressName(accountId, null, defaultName); const [isAddressCached, nameCached] = nameCache.get(accountId) || [false, [null, null]]; @@ -63,35 +55,30 @@ function defaultOrAddr (defaultName = '', _address: AccountId | AccountIndex | A return [[extracted, null], !isAddressExtracted, isAddressExtracted]; } -function AccountName ({ children, className, defaultName, label, onClick, override, style, toggle, value, withShort }: Props): React.ReactElement { +function extractName (address: AccountId | string, accountIndex?: AccountIndex, defaultName?: string): React.ReactNode { + const [[displayFirst, displaySecond], isLocal, isAddress] = defaultOrAddr(defaultName, address, accountIndex); + + return ( +
+ { + displaySecond + ? <>{displayFirst}/{displaySecond} + : displayFirst + } +
+ ); +} + +function AccountName ({ children, className, defaultName, label, onClick, override, style, toggle, value }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const { allAccounts } = useAccounts(); const [isJudgementOpen, toggleJudgement] = useToggle(); const registrars = useCall[]>(api.query.identity?.registrars, []); const info = useCall(api.derive.accounts.info as any, [value]); - const [accountId, setAccountId] = useState(null); const [isRegistrar, setIsRegistrar] = useState(false); - const [judgementAccountId, setJudgementAccountId] = useState(null); - const [judgementEnum, setJudgementEnum] = useState(2); // Reasonable - const [registrarIndex, setRegistrarIndex] = useState(-1); const address = useMemo((): string => (value || '').toString(), [value]); - - const _extractName = (accountId?: AccountId, accountIndex?: AccountIndex): React.ReactNode => { - const [[displayFirst, displaySecond], isLocal, isAddress] = defaultOrAddr(defaultName, accountId || address, withShort ? null : accountIndex); - - return ( -
- { - displaySecond - ? <>{displayFirst}/{displaySecond} - : displayFirst - } -
- ); - }; - - const [name, setName] = useState((): React.ReactNode => _extractName()); + const [name, setName] = useState((): React.ReactNode => extractName((value || '').toString(), undefined, defaultName)); // determine if we have a registrar or not - registrars are allowed to approve useEffect((): void => { @@ -108,31 +95,10 @@ function AccountName ({ children, className, defaultName, label, onClick, overri } }, [allAccounts, registrars]); - // find the id of our registrar in the list - useEffect((): void => { - if (registrars && judgementAccountId) { - setRegistrarIndex( - registrars - .map((registrar): string| null => - registrar.isSome - ? registrar.unwrap().account.toString() - : null - ) - .indexOf(judgementAccountId) - ); - } else { - setRegistrarIndex(-1); - } - }, [judgementAccountId, registrars]); - - // set the actual nickname, localname, accountIndex, accountId + // set the actual nickname, local name, accountIndex, accountId useEffect((): void => { const { accountId, accountIndex, identity, nickname } = info || {}; - if (accountId) { - setAccountId(accountId.toString()); - } - if (api.query.identity?.identityOf) { if (identity?.display) { const judgements = identity.judgements.filter(([, judgement]): boolean => !judgement.isFeePaid); @@ -213,55 +179,26 @@ function AccountName ({ children, className, defaultName, label, onClick, overri ); - nameCache.set(address, [false, displayParent ? [displayParent, displayName] : [displayName, null]]); + nameCache.set((accountId || address).toString(), [false, displayParent ? [displayParent, displayName] : [displayName, null]]); setName((): React.ReactNode => name); } else { - setName((): React.ReactNode => _extractName(accountId, accountIndex)); + setName((): React.ReactNode => extractName(accountId || address, accountIndex)); } } else if (nickname) { - nameCache.set(address, [false, [nickname, null]]); + nameCache.set((accountId || address).toString(), [false, [nickname, null]]); setName(nickname); } else { - setName(defaultOrAddr(defaultName, accountId || address, withShort ? null : accountIndex)); + setName(defaultOrAddr(defaultName, accountId || address, accountIndex)); } - }, [info, toggle]); + }, [address, info, toggle]); return ( <> {isJudgementOpen && ( - - - - - - - - - - + )}
void; +} + +const JUDGEMENT_ENUM = [ + { value: 0, text: 'Unknown' }, + { value: 1, text: 'Fee paid' }, + { value: 2, text: 'Reasonable' }, + { value: 3, text: 'Known good' }, + { value: 4, text: 'Out of date' }, + { value: 5, text: 'Low quality' } +]; + +export default function AccountNameJudgement ({ address, toggleJudgement }: Props): React.ReactElement { + const { t } = useTranslation(); + const { api } = useApi(); + const registrars = useCall[]>(api.query.identity?.registrars, []); + const [judgementAccountId, setJudgementAccountId] = useState(null); + const [judgementEnum, setJudgementEnum] = useState(2); // Reasonable + const [registrarIndex, setRegistrarIndex] = useState(-1); + + // find the id of our registrar in the list + useEffect((): void => { + if (registrars && judgementAccountId) { + setRegistrarIndex( + registrars + .map((registrar): string| null => + registrar.isSome + ? registrar.unwrap().account.toString() + : null + ) + .indexOf(judgementAccountId) + ); + } else { + setRegistrarIndex(-1); + } + }, [judgementAccountId, registrars]); + + return ( + + + + + + + + + + + ); +}