Bags summary adjustment (#7272)

* Filter for own/all bags

* Initial Own/All depending on stashes

* Adjust bags summaries
This commit is contained in:
Jaco 2022-03-30 13:00:34 +02:00 committed by GitHub
parent 4f0db8d138
commit 326f0ecb1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 13 deletions

View File

@ -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<Pro
return (
<tr>
<td className='number' />
<td className='number'><FormatBalance value={upper} /></td>
<td className='number'><FormatBalance value={lower} /></td>
<td className='address'>{info.head.isSome && <AddressMini value={info.head} />}</td>
<td className='address'>{info.tail.isSome && <AddressMini value={info.tail} />}</td>
<td className='address'>

View File

@ -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<Props> {
function Summary ({ className = '', ids, nodes }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const total = useCall<BN>(api.query.bagsList.counterForListNodes);
const myCount = useMemo(
() => nodes && Object.values(nodes).reduce((count, n) => count + n.length, 0),
[nodes]
);
return (
<SummaryBox className={className}>
@ -26,6 +36,20 @@ function Summary ({ className = '', ids }: Props): React.ReactElement<Props> {
: <Spinner noLabel />
}
</CardSummary>
<section>
<CardSummary label={t<string>('total nodes')}>
{nodes
? formatNumber(total)
: <Spinner noLabel />
}
</CardSummary>
<CardSummary label={t<string>('my nodes')}>
{isNumber(myCount)
? formatNumber(myCount)
: '-'
}
</CardSummary>
</section>
</SummaryBox>
);
}

View File

@ -36,22 +36,22 @@ function Bags ({ ownStashes }: Props): React.ReactElement<Props> {
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<Props> {
return (
<>
<Summary ids={ids} />
<Summary
ids={ids}
nodes={nodes}
/>
<Button.Group>
<ToggleGroup
onChange={setFilterIndex}
@ -72,8 +75,9 @@ function Bags ({ ownStashes }: Props): React.ReactElement<Props> {
emptySpinner={t<string>('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]) => (
<Bag
index={index}
info={info}
key={key}
lower={lower}

View File

@ -17,6 +17,7 @@ export interface ListNode {
}
export interface BagInfo {
index: number;
info: PalletBagsListListBag;
key: string;
lower: BN;

View File

@ -16,12 +16,19 @@ const multiOptions = {
const sorted = ids
.map((id, index): [BN, Option<PalletBagsListListBag>] => [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 }
);
},