From 3837ce1db60309133852e085388e7993e8bfd83c Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 29 Oct 2019 08:20:35 +0100 Subject: [PATCH] Small staking render adjustments (#1798) * Small staking rendering improvements * Only display once stash is available --- packages/app-staking/src/Overview/Address.tsx | 11 +++--- .../app-staking/src/Overview/CurrentList.tsx | 7 ++-- packages/app-staking/src/Overview/index.tsx | 39 +++++++++---------- packages/react-query/src/AccountName.tsx | 12 ++++-- 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/packages/app-staking/src/Overview/Address.tsx b/packages/app-staking/src/Overview/Address.tsx index 9838ecb754..9aa71b1605 100644 --- a/packages/app-staking/src/Overview/Address.tsx +++ b/packages/app-staking/src/Overview/Address.tsx @@ -28,6 +28,7 @@ interface Props extends I18nProps { lastBlock: string; recentlyOnline?: Record; stakingInfo?: DerivedStaking; + withNominations?: boolean; } interface StakingState { @@ -48,7 +49,7 @@ interface OnlineState { const WITH_VALIDATOR_PREFS = { validatorPayment: true }; -function Address ({ address, className, defaultName, filter, lastAuthor, lastBlock, recentlyOnline, stakingInfo, t }: Props): React.ReactElement | null { +function Address ({ address, className, defaultName, filter, lastAuthor, lastBlock, recentlyOnline, stakingInfo, t, withNominations }: Props): React.ReactElement | null { const { isSubstrateV2 } = useContext(ApiContext); const [isNominatorMe, seIsNominatorMe] = useState(false); const [{ hasOfflineWarnings, onlineStatus }, setOnlineStatus] = useState({ @@ -107,7 +108,7 @@ function Address ({ address, className, defaultName, filter, lastAuthor, lastBlo } }, [recentlyOnline, stakingInfo]); - if ((filter === 'hasNominators' && !hasNominators) || + if (!stashId || (filter === 'hasNominators' && !hasNominators) || (filter === 'noNominators' && hasNominators) || (filter === 'hasWarnings' && !hasOfflineWarnings) || (filter === 'noWarnings' && hasOfflineWarnings) || @@ -147,12 +148,12 @@ function Address ({ address, className, defaultName, filter, lastAuthor, lastBlo tooltip /> )} - key={stashId || controllerId || undefined} - value={stashId || address} + key={stashId} + value={stashId} withBalance={balanceOpts} withValidatorPrefs={WITH_VALIDATOR_PREFS} > - {hasNominators && ( + {withNominations && hasNominators && (
{t('Nominators ({{count}})', { diff --git a/packages/app-staking/src/Overview/CurrentList.tsx b/packages/app-staking/src/Overview/CurrentList.tsx index f4cf535f1d..6aee82d9b4 100644 --- a/packages/app-staking/src/Overview/CurrentList.tsx +++ b/packages/app-staking/src/Overview/CurrentList.tsx @@ -23,7 +23,7 @@ interface Props extends I18nProps { function CurrentList ({ currentValidators, lastAuthor, lastBlock, next, recentlyOnline, t }: Props): React.ReactElement { const [filter, setFilter] = useState('all'); - const _renderColumn = (addresses: string[], defaultName: string): React.ReactNode => { + const _renderColumn = (addresses: string[], defaultName: string, withNominations: boolean): React.ReactNode => { return addresses.map((address): React.ReactNode => (
)); }; @@ -59,13 +60,13 @@ function CurrentList ({ currentValidators, lastAuthor, lastBlock, next, recently emptyText={t('No addresses found')} headerText={t('validators')} > - {_renderColumn(currentValidators, t('validator'))} + {_renderColumn(currentValidators, t('validator'), true)} - {_renderColumn(next, t('intention'))} + {_renderColumn(next, t('intention'), false)} diff --git a/packages/app-staking/src/Overview/index.tsx b/packages/app-staking/src/Overview/index.tsx index b562b5bebf..bd5335a87e 100644 --- a/packages/app-staking/src/Overview/index.tsx +++ b/packages/app-staking/src/Overview/index.tsx @@ -6,7 +6,7 @@ import { BareProps } from '@polkadot/react-components/types'; import { ComponentProps } from '../types'; -import React, { useContext } from 'react'; +import React, { useContext, useEffect, useState } from 'react'; import { HeaderExtended } from '@polkadot/api-derive'; import { ApiContext } from '@polkadot/react-api'; import { withCalls, withMulti } from '@polkadot/react-api/with'; @@ -19,31 +19,28 @@ interface Props extends BareProps, ComponentProps { chain_subscribeNewHeads?: HeaderExtended; } -// TODO: Switch to useState function Overview (props: Props): React.ReactElement { const { isSubstrateV2 } = useContext(ApiContext); + const [{ lastAuthor, lastBlock }, setLast] = useState({ lastAuthor: '', lastBlock: '' }); + const [nextSorted, setNextSorted] = useState([]); const { chain_subscribeNewHeads, allControllers, allStashes, currentValidators, recentlyOnline } = props; - let nextSorted: string[]; - if (isSubstrateV2) { - // this is a V2 node currentValidators is a list of stashes - nextSorted = allStashes.filter((address): boolean => - !currentValidators.includes(address) + useEffect((): void => { + chain_subscribeNewHeads && setLast({ + lastAuthor: (chain_subscribeNewHeads.author || '').toString(), + lastBlock: formatNumber(chain_subscribeNewHeads.number) + }); + }, [chain_subscribeNewHeads]); + + useEffect((): void => { + setNextSorted( + isSubstrateV2 + // this is a V2 node currentValidators is a list of stashes + ? allStashes.filter((address): boolean => !currentValidators.includes(address)) + // this is a V1 node currentValidators is a list of controllers + : allControllers.filter((address): boolean => !currentValidators.includes(address)) ); - } else { - // this is a V1 node currentValidators is a list of controllers - nextSorted = allControllers.filter((address): boolean => - !currentValidators.includes(address) - ); - } - - let lastBlock = ''; - let lastAuthor: string | undefined; - - if (chain_subscribeNewHeads) { - lastBlock = formatNumber(chain_subscribeNewHeads.number); - lastAuthor = (chain_subscribeNewHeads.author || '').toString(); - } + }, [allControllers, allStashes, currentValidators]); return (
diff --git a/packages/react-query/src/AccountName.tsx b/packages/react-query/src/AccountName.tsx index a4add48bcc..d72f9f1497 100644 --- a/packages/react-query/src/AccountName.tsx +++ b/packages/react-query/src/AccountName.tsx @@ -20,8 +20,14 @@ interface Props extends BareProps, CallProps { withShort?: boolean; } -export function AccountName ({ children, className, defaultName = '', idAndIndex, info, label = '', params, style }: Props): React.ReactElement { - const [name, setName] = useState(defaultName.toUpperCase()); +function defaultOrAddr (defaultName = '', address?: string | null): string { + return defaultName + ? defaultName.toUpperCase() + : toShortAddress(address); +} + +export function AccountName ({ children, className, defaultName, idAndIndex, info, label = '', params, style }: Props): React.ReactElement { + const [name, setName] = useState(defaultOrAddr(defaultName, params)); useEffect((): void => { const [accountId, accountIndex] = idAndIndex || []; @@ -37,7 +43,7 @@ export function AccountName ({ children, className, defaultName = '', idAndIndex : extracted ); } else { - setName(defaultName.toUpperCase() || toShortAddress(params)); + setName(defaultOrAddr(defaultName, params)); } }, [idAndIndex, info]);