Summary table header (#3392)

* Symmary table header

* Nominator count
This commit is contained in:
Jaco Greeff 2020-08-11 22:11:11 +02:00 committed by GitHub
parent 328f3e3b8e
commit 1e2fe08801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 146 additions and 82 deletions

View File

@ -5,7 +5,7 @@
import { Slash } from './types';
import React from 'react';
import { AddressMini, AddressSmall, Expander } from '@polkadot/react-components';
import { AddressMini, AddressSmall, Badge, Expander } from '@polkadot/react-components';
import { FormatBalance } from '@polkadot/react-query';
import { formatNumber } from '@polkadot/util';
@ -15,11 +15,19 @@ interface Props {
slash: Slash;
}
function Row ({ slash: { slash: { others, own, payout, reporters, validator }, total, totalOther } }: Props): React.ReactElement<Props> {
function Row ({ slash: { isMine, slash: { others, own, payout, reporters, validator }, total, totalOther } }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
return (
<tr>
<td className='badge'>
{isMine && (
<Badge
color='red'
icon='skull-crossbones'
/>
)}
</td>
<td className='address'>
<AddressSmall value={validator} />
</td>
@ -37,15 +45,6 @@ function Row ({ slash: { slash: { others, own, payout, reporters, validator }, t
</Expander>
)}
</td>
<td className='number together'>
<FormatBalance value={own} />
</td>
<td className='number together'>
<FormatBalance value={totalOther} />
</td>
<td className='number together'>
<FormatBalance value={total} />
</td>
<td className='address'>
{reporters.map((reporter, index): React.ReactNode => (
<AddressMini
@ -54,6 +53,15 @@ function Row ({ slash: { slash: { others, own, payout, reporters, validator }, t
/>
))}
</td>
<td className='number together'>
<FormatBalance value={own} />
</td>
<td className='number together'>
<FormatBalance value={totalOther} />
</td>
<td className='number together'>
<FormatBalance value={total} />
</td>
<td className='number together'>
<FormatBalance value={payout} />
</td>

View File

@ -2,12 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { StakerState } from '@polkadot/react-hooks/types';
import { UnappliedSlash } from '@polkadot/types/interfaces';
import { Slash } from './types';
import BN from 'bn.js';
import React, { useMemo } from 'react';
import { Table } from '@polkadot/react-components';
import { CardSummary, SummaryBox, Table } from '@polkadot/react-components';
import { FormatBalance } from '@polkadot/react-query';
import { formatNumber } from '@polkadot/util';
@ -15,6 +16,7 @@ import { useTranslation } from '../translate';
import Row from './Row';
interface Props {
ownStashes?: StakerState[];
slashes: [BN, UnappliedSlash[]][];
}
@ -22,93 +24,138 @@ interface SlashEra {
era: BN;
nominators: string[];
payout: BN;
reporters: string[];
slashes: Slash[];
validators: string[];
total: BN;
}
function Slashes ({ slashes }: Props): React.ReactElement<Props> | null {
function calcSlashEras (slashes: [BN, UnappliedSlash[]][], ownStashes: StakerState[]): SlashEra[] {
const slashEras: SlashEra[] = [];
slashes
.reduce((rows: Slash[], [era, slashes]): Slash[] => {
return slashes.reduce((rows: Slash[], slash): Slash[] => {
const totalOther = slash.others.reduce((total: BN, [, value]): BN => {
return total.add(value);
}, new BN(0));
const isMine = ownStashes.some(({ stashId }): boolean => {
return slash.validator.eq(stashId) || slash.others.some(([nominatorId]) => nominatorId.eq(stashId));
});
rows.push({ era, isMine, slash, total: slash.own.add(totalOther), totalOther });
return rows;
}, rows);
}, [])
.forEach((slash): void => {
let slashEra = slashEras.find(({ era }) => era.eq(slash.era));
if (!slashEra) {
slashEra = {
era: slash.era,
nominators: [],
payout: new BN(0),
reporters: [],
slashes: [],
total: new BN(0),
validators: []
};
slashEras.push(slashEra);
}
slashEra.payout.iadd(slash.slash.payout);
slashEra.total.iadd(slash.total);
slashEra.slashes.push(slash);
const validatorId = slash.slash.validator.toString();
if (!slashEra.validators.includes(validatorId)) {
slashEra.validators.push(validatorId);
}
slash.slash.others.forEach(([accountId]): void => {
const nominatorId = accountId.toString();
if (slashEra && !slashEra.nominators.includes(nominatorId)) {
slashEra.nominators.push(nominatorId);
}
});
slash.slash.reporters.forEach((accountId): void => {
const reporterId = accountId.toString();
if (slashEra && !slashEra.reporters.includes(reporterId)) {
slashEra.reporters.push(reporterId);
}
});
});
return slashEras.sort((a, b) => b.era.cmp(a.era));
}
function Slashes ({ ownStashes = [], slashes }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const header = useMemo(() => [
[],
const header = useMemo((): [string?, string?, number?][] => [
[undefined, 'start', 3],
[t('reporters'), 'address'],
[t('own')],
[t('other')],
[t('total')],
[t('reporters'), 'address'],
[t('payout')]
], [t]);
const rows = useMemo((): SlashEra[] => {
const slashEras: SlashEra[] = [];
slashes
.reduce((rows: Slash[], [era, slashes]): Slash[] => {
return slashes.reduce((rows: Slash[], slash): Slash[] => {
const totalOther = slash.others.reduce((total: BN, [, value]): BN => {
return total.add(value);
}, new BN(0));
rows.push({ era, slash, total: slash.own.add(totalOther), totalOther });
return rows;
}, rows);
}, [])
.forEach((slash): void => {
let slashEra = slashEras.find(({ era }) => era.eq(slash.era));
if (!slashEra) {
slashEra = {
era: slash.era,
nominators: [],
payout: new BN(0),
slashes: [],
total: new BN(0),
validators: []
};
slashEras.push(slashEra);
}
slashEra.payout.iadd(slash.slash.payout);
slashEra.total.iadd(slash.total);
slashEra.slashes.push(slash);
const validatorId = slash.slash.validator.toString();
if (!slashEra.validators.includes(validatorId)) {
slashEra.validators.push(validatorId);
}
slash.slash.others.forEach(([accountId]): void => {
const nominatorId = accountId.toString();
if (slashEra && !slashEra.nominators.includes(nominatorId)) {
slashEra.nominators.push(nominatorId);
}
});
});
return slashEras.sort((a, b) => b.era.cmp(a.era));
}, [slashes]);
const rows = useMemo(() => calcSlashEras(slashes, ownStashes), [ownStashes, slashes]);
if (!rows.length) {
return <article>{t('There are no unapplied/pending slashes')}</article>;
return (
<Table
empty={t<string>('There are no unapplied/pending slashes')}
header={[[t('unapplied'), 'start']]}
/>
);
}
return (
<>
{rows.map(({ era, payout, slashes, total, validators }): React.ReactNode => (
{rows.map(({ era, nominators, reporters, slashes, total, validators }): React.ReactNode => (
<Table
footer={
<tr>
<td>{formatNumber(validators.length)}</td>
<td colSpan={3} />
<td><FormatBalance value={total} /></td>
<td />
<td><FormatBalance value={payout} /></td>
</tr>
}
header={[[t('era {{era}}/unapplied', { replace: { era: era.toString() } }), 'start']].concat(header)}
header={[[t('era {{era}}/unapplied', { replace: { era: era.toString() } }), 'start', 6]]}
key={era.toString()}
>
<tr>
<td colSpan={8}>
<SummaryBox isSmall>
<section>
<CardSummary label={t<string>('validators')}>
{formatNumber(validators.length)}
</CardSummary>
<CardSummary label={t<string>('nominators')}>
{formatNumber(nominators.length)}
</CardSummary>
<CardSummary label={t<string>('reporters')}>
{formatNumber(reporters.length)}
</CardSummary>
</section>
<section>
<CardSummary label={t<string>('total')}>
<FormatBalance value={total} />
</CardSummary>
</section>
</SummaryBox>
</td>
</tr>
<tr>
{header.map(([label, className, colSpan = 1], index): React.ReactNode => (
<td
className={className}
colSpan={colSpan}
key={index}
>
<label>{label}</label>
</td>
))}
</tr>
{slashes.map((slash, index): React.ReactNode => (
<Row
key={index}

View File

@ -8,6 +8,7 @@ import BN from 'bn.js';
export interface Slash {
era: BN;
isMine: boolean;
slash: UnappliedSlash;
total: BN;
totalOther: BN;

View File

@ -139,7 +139,10 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement<Pr
<Query />
</Route>
<Route path={`${basePath}/slashes`}>
<Slashes slashes={slashes} />
<Slashes
ownStashes={ownStashes}
slashes={slashes}
/>
</Route>
<Route path={`${basePath}/targets`}>
<Targets

View File

@ -8,11 +8,12 @@ import styled from 'styled-components';
interface Props {
children?: React.ReactNode;
className?: string;
isSmall?: boolean;
}
function SummaryBox ({ children, className }: Props): React.ReactElement<Props> {
function SummaryBox ({ children, className = '', isSmall }: Props): React.ReactElement<Props> {
return (
<div className={className}>
<div className={`${className}${isSmall ? ' isSmall' : ''}`}>
{children}
</div>
);
@ -58,6 +59,10 @@ export default React.memo(styled(SummaryBox)`
margin-bottom: 1.5rem;
}
&.isSmall {
margin-bottom: 0;
}
.ui.label {
padding-left: 0;
padding-right: 0;