From 326f0ecb1a8c14d46c6afd0f7465654b0f37d412 Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 30 Mar 2022 13:00:34 +0200 Subject: [PATCH] Bags summary adjustment (#7272) * Filter for own/all bags * Initial Own/All depending on stashes * Adjust bags summaries --- packages/page-staking/src/Bags/Bag.tsx | 3 ++ packages/page-staking/src/Bags/Summary.tsx | 30 +++++++++++++++++-- packages/page-staking/src/Bags/index.tsx | 20 ++++++++----- packages/page-staking/src/Bags/types.ts | 1 + packages/page-staking/src/Bags/useBagsList.ts | 11 +++++-- 5 files changed, 52 insertions(+), 13 deletions(-) diff --git a/packages/page-staking/src/Bags/Bag.tsx b/packages/page-staking/src/Bags/Bag.tsx index 2bec55cffe..31bc3a65d7 100644 --- a/packages/page-staking/src/Bags/Bag.tsx +++ b/packages/page-staking/src/Bags/Bag.tsx @@ -17,6 +17,7 @@ import useBagEntries from './useBagEntries'; import useBonded from './useBonded'; interface Props { + index: number; info: PalletBagsListListBag; lower: BN; stashNodes?: StashNode[]; @@ -44,7 +45,9 @@ function Bag ({ info, lower, stashNodes, upper }: Props): React.ReactElement + + {info.head.isSome && } {info.tail.isSome && } diff --git a/packages/page-staking/src/Bags/Summary.tsx b/packages/page-staking/src/Bags/Summary.tsx index 6e26969701..e034b4dfe1 100644 --- a/packages/page-staking/src/Bags/Summary.tsx +++ b/packages/page-staking/src/Bags/Summary.tsx @@ -2,21 +2,31 @@ // SPDX-License-Identifier: Apache-2.0 import type { BN } from '@polkadot/util'; +import type { BagMap } from './types'; -import React from 'react'; +import React, { useMemo } from 'react'; import { CardSummary, Spinner, SummaryBox } from '@polkadot/react-components'; -import { formatNumber } from '@polkadot/util'; +import { useApi, useCall } from '@polkadot/react-hooks'; +import { formatNumber, isNumber } from '@polkadot/util'; import { useTranslation } from '../translate'; interface Props { className?: string; ids?: BN[]; + nodes?: BagMap; } -function Summary ({ className = '', ids }: Props): React.ReactElement { +function Summary ({ className = '', ids, nodes }: Props): React.ReactElement { const { t } = useTranslation(); + const { api } = useApi(); + const total = useCall(api.query.bagsList.counterForListNodes); + + const myCount = useMemo( + () => nodes && Object.values(nodes).reduce((count, n) => count + n.length, 0), + [nodes] + ); return ( @@ -26,6 +36,20 @@ function Summary ({ className = '', ids }: Props): React.ReactElement { : } +
+ ('total nodes')}> + {nodes + ? formatNumber(total) + : + } + + ('my nodes')}> + {isNumber(myCount) + ? formatNumber(myCount) + : '-' + } + +
); } diff --git a/packages/page-staking/src/Bags/index.tsx b/packages/page-staking/src/Bags/index.tsx index a3264efc1f..06d42d4a61 100644 --- a/packages/page-staking/src/Bags/index.tsx +++ b/packages/page-staking/src/Bags/index.tsx @@ -36,22 +36,22 @@ function Bags ({ ownStashes }: Props): React.ReactElement { const [filterIndex, setFilterIndex] = useState(() => stashIds.length ? 0 : 1); const ids = useBagsIds(); const list = useBagsList(ids); - const nodes = useBagsNodes(stashIds); const headerRef = useRef([ [t('bags')], - [t('head'), 'address'], - [t('tail'), 'address'], - [t('mine'), 'address'], - [] + [t('max'), 'number'], + [t('min'), 'number'], + [t('first'), 'address'], + [t('last'), 'address'], + [t('stashes'), 'address'], + [t('count'), 'number'] ]); const filterOptions = useRef([ { text: t('My bags'), value: 'mine' }, { text: t('All bags'), value: 'all' } ]); - const filtered = useMemo( () => list && nodes && sortNodes(list, nodes, !filterIndex), [filterIndex, list, nodes] @@ -59,7 +59,10 @@ function Bags ({ ownStashes }: Props): React.ReactElement { return ( <> - + { emptySpinner={t('Retrieving all available bags, this will take some time')} header={headerRef.current} > - {filtered?.map(([{ info, key, lower, upper }, nodes]) => ( + {filtered?.map(([{ index, info, key, lower, upper }, nodes]) => ( ] => [id, opts[index]]) .filter(([, o]) => o.isSome) - .map(([upper, o]): BagInfo => ({ info: o.unwrap(), key: upper.toString(), lower: BN_ZERO, upper })) - .sort((a, b) => b.upper.cmp(a.upper)); + .sort(([a], [b]) => b.cmp(a)) + .map(([upper, o], index): BagInfo => ({ + index, + info: o.unwrap(), + key: upper.toString(), + lower: BN_ZERO, + upper + })); return sorted.map((entry, index) => (index === (sorted.length - 1)) ? entry + // We could probably use a .add(BN_ONE) here : { ...entry, lower: sorted[index + 1].upper } ); },