From 1e2fe0880106f268d8304ef6cc7df6d9b052ad04 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 11 Aug 2020 22:11:11 +0200 Subject: [PATCH] Summary table header (#3392) * Symmary table header * Nominator count --- packages/page-staking/src/Slashes/Row.tsx | 30 +-- packages/page-staking/src/Slashes/index.tsx | 183 ++++++++++++------- packages/page-staking/src/Slashes/types.ts | 1 + packages/page-staking/src/index.tsx | 5 +- packages/react-components/src/SummaryBox.tsx | 9 +- 5 files changed, 146 insertions(+), 82 deletions(-) diff --git a/packages/page-staking/src/Slashes/Row.tsx b/packages/page-staking/src/Slashes/Row.tsx index 4086b16eec..a42d3fa474 100644 --- a/packages/page-staking/src/Slashes/Row.tsx +++ b/packages/page-staking/src/Slashes/Row.tsx @@ -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 { +function Row ({ slash: { isMine, slash: { others, own, payout, reporters, validator }, total, totalOther } }: Props): React.ReactElement { const { t } = useTranslation(); return ( + + {isMine && ( + + )} + @@ -37,15 +45,6 @@ function Row ({ slash: { slash: { others, own, payout, reporters, validator }, t )} - - - - - - - - - {reporters.map((reporter, index): React.ReactNode => ( ))} + + + + + + + + + diff --git a/packages/page-staking/src/Slashes/index.tsx b/packages/page-staking/src/Slashes/index.tsx index 3c3e9696e1..433e04b62f 100644 --- a/packages/page-staking/src/Slashes/index.tsx +++ b/packages/page-staking/src/Slashes/index.tsx @@ -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 | 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 | 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
{t('There are no unapplied/pending slashes')}
; + return ( + ('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 => (
- - - - - } - 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()} > + + + + + {header.map(([label, className, colSpan = 1], index): React.ReactNode => ( + + ))} + {slashes.map((slash, index): React.ReactNode => ( - + { +function SummaryBox ({ children, className = '', isSmall }: Props): React.ReactElement { return ( -
+
{children}
); @@ -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;
{formatNumber(validators.length)} - -
+ +
+ ('validators')}> + {formatNumber(validators.length)} + + ('nominators')}> + {formatNumber(nominators.length)} + + ('reporters')}> + {formatNumber(reporters.length)} + +
+
+ ('total')}> + + +
+
+
+ +