Small staking render adjustments (#1798)

* Small staking rendering improvements

* Only display once stash is available
This commit is contained in:
Jaco Greeff 2019-10-29 08:20:35 +01:00 committed by GitHub
parent f0cd4cbd3f
commit 3837ce1db6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 32 deletions

View File

@ -28,6 +28,7 @@ interface Props extends I18nProps {
lastBlock: string;
recentlyOnline?: Record<string, BlockNumber>;
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<Props> | null {
function Address ({ address, className, defaultName, filter, lastAuthor, lastBlock, recentlyOnline, stakingInfo, t, withNominations }: Props): React.ReactElement<Props> | null {
const { isSubstrateV2 } = useContext(ApiContext);
const [isNominatorMe, seIsNominatorMe] = useState(false);
const [{ hasOfflineWarnings, onlineStatus }, setOnlineStatus] = useState<OnlineState>({
@ -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 && (
<details>
<summary>
{t('Nominators ({{count}})', {

View File

@ -23,7 +23,7 @@ interface Props extends I18nProps {
function CurrentList ({ currentValidators, lastAuthor, lastBlock, next, recentlyOnline, t }: Props): React.ReactElement<Props> {
const [filter, setFilter] = useState<ValidatorFilter>('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 => (
<Address
address={address}
@ -33,6 +33,7 @@ function CurrentList ({ currentValidators, lastAuthor, lastBlock, next, recently
lastAuthor={lastAuthor}
lastBlock={lastBlock}
recentlyOnline={recentlyOnline}
withNominations={withNominations}
/>
));
};
@ -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)}
</Column>
<Column
emptyText={t('No addresses found')}
headerText={t('next up')}
>
{_renderColumn(next, t('intention'))}
{_renderColumn(next, t('intention'), false)}
</Column>
</Columar>
</div>

View File

@ -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<Props> {
const { isSubstrateV2 } = useContext(ApiContext);
const [{ lastAuthor, lastBlock }, setLast] = useState({ lastAuthor: '', lastBlock: '' });
const [nextSorted, setNextSorted] = useState<string[]>([]);
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 (
<div className='staking--Overview'>

View File

@ -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<Props> {
const [name, setName] = useState<React.ReactNode>(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<Props> {
const [name, setName] = useState<React.ReactNode>(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]);