Group democracy locks (#3093)
* Group democracy locks * Update packages/react-components/src/DemocracyLocks.tsx Co-authored-by: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com> * Check for state changes before setting Co-authored-by: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com>
This commit is contained in:
parent
0b44d184c6
commit
7c68d68339
@ -5,7 +5,7 @@
|
||||
import { DeriveDemocracyLock } from '@polkadot/api-derive/types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React, { useState } from 'react';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { useApi, useCall } from '@polkadot/react-hooks';
|
||||
import { BlockToTime, FormatBalance } from '@polkadot/react-query';
|
||||
@ -20,50 +20,108 @@ interface Props {
|
||||
value?: DeriveDemocracyLock[];
|
||||
}
|
||||
|
||||
interface Entry {
|
||||
details: React.ReactNode;
|
||||
headers: React.ReactNode[];
|
||||
isCountdown: boolean;
|
||||
isFinished: boolean;
|
||||
}
|
||||
|
||||
interface State {
|
||||
maxBalance: BN;
|
||||
sorted: Entry[];
|
||||
}
|
||||
|
||||
let id = 0;
|
||||
|
||||
// group by header & details
|
||||
// - all unlockable together
|
||||
// - all ongoing together
|
||||
// - unlocks are displayed individually
|
||||
function groupLocks (t: <T = string> (key: string, options?: { replace?: Record<string, string> }) => T, bestNumber: BN, locks: DeriveDemocracyLock[] = []): State {
|
||||
return {
|
||||
maxBalance: bnMax(...locks.map(({ balance }) => balance)),
|
||||
sorted: locks
|
||||
.map((info): [DeriveDemocracyLock, BN] => [info, info.unlockAt.gt(bestNumber) ? info.unlockAt.sub(bestNumber) : BN_ZERO])
|
||||
.sort((a, b) => a[0].referendumId.cmp(b[0].referendumId))
|
||||
.sort((a, b) => a[1].cmp(b[1]))
|
||||
.sort((a, b) => a[0].isFinished === b[0].isFinished ? 0 : (a[0].isFinished ? -1 : 1))
|
||||
.reduce((sorted: Entry[], [{ balance, isFinished, referendumId, vote }, blocks]): Entry[] => {
|
||||
const isCountdown = blocks.gt(BN_ZERO);
|
||||
const header = <div>#{referendumId.toString()} {formatBalance(balance, { forceUnit: '-' })} {vote.conviction.toString()}</div>;
|
||||
const prev = sorted.length ? sorted[sorted.length - 1] : null;
|
||||
|
||||
if (!prev || (isCountdown || (isFinished !== prev.isFinished))) {
|
||||
sorted.push({
|
||||
details: (
|
||||
<div className='faded'>
|
||||
{isCountdown
|
||||
? (
|
||||
<BlockToTime
|
||||
blocks={blocks}
|
||||
label={`${t<string>('{{blocks}} blocks', { replace: { blocks: formatNumber(blocks) } })}, `}
|
||||
/>
|
||||
)
|
||||
: isFinished
|
||||
? t<string>('lock expired')
|
||||
: t<string>('ongoing referendum')
|
||||
}
|
||||
</div>
|
||||
),
|
||||
headers: [header],
|
||||
isCountdown,
|
||||
isFinished
|
||||
});
|
||||
} else {
|
||||
prev.headers.push(header);
|
||||
}
|
||||
|
||||
return sorted;
|
||||
}, [])
|
||||
};
|
||||
}
|
||||
|
||||
function DemocracyLocks ({ className = '', value }: Props): React.ReactElement<Props> | null {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const bestNumber = useCall<BN>(api.derive.chain.bestNumber, []);
|
||||
const [trigger] = useState(`${Date.now()}-democracy-locks-${++id}`);
|
||||
const [{ maxBalance, sorted }, setState] = useState<State>({ maxBalance: BN_ZERO, sorted: [] });
|
||||
|
||||
if (!value || !value.length || !bestNumber) {
|
||||
useEffect((): void => {
|
||||
bestNumber && setState((state): State => {
|
||||
const newState = groupLocks(t, bestNumber, value);
|
||||
|
||||
// only update when the structure of new is different
|
||||
// - it has a new overall breakdown with sections
|
||||
// - one of the sections has a different number of headers
|
||||
return state.sorted.length !== newState.sorted.length || state.sorted.some((s, i) => s.headers.length !== newState.sorted[i].headers.length)
|
||||
? newState
|
||||
: state;
|
||||
});
|
||||
}, [bestNumber, t, value]);
|
||||
|
||||
if (!sorted.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const max = bnMax(...value.map(({ balance }) => balance));
|
||||
const mapped = value
|
||||
.map((info): [DeriveDemocracyLock, BN] => [info, info.unlockAt.gt(bestNumber) ? info.unlockAt.sub(bestNumber) : BN_ZERO])
|
||||
.sort((a, b) => a[1].cmp(b[1]));
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
<Icon
|
||||
icon='clock'
|
||||
tooltip={trigger}
|
||||
/>
|
||||
<FormatBalance value={max} />
|
||||
<FormatBalance value={maxBalance} />
|
||||
<Tooltip
|
||||
text={mapped.map(([{ balance, isFinished, referendumId, vote }, blocks], index): React.ReactNode => (
|
||||
text={sorted.map(({ details, headers }, index): React.ReactNode => (
|
||||
<div
|
||||
className='row'
|
||||
key={index}
|
||||
>
|
||||
<div>#{referendumId.toString()} {formatBalance(balance, { forceUnit: '-' })} {vote.conviction.toString()}</div>
|
||||
<div className='faded'>
|
||||
{blocks.gt(BN_ZERO)
|
||||
? (
|
||||
<BlockToTime
|
||||
blocks={blocks}
|
||||
label={`${t<string>('{{blocks}} blocks', { replace: { blocks: formatNumber(blocks) } })}, `}
|
||||
/>
|
||||
)
|
||||
: isFinished
|
||||
? t<string>('lock expired')
|
||||
: t<string>('ongoing referendum')
|
||||
}
|
||||
</div>
|
||||
{headers.map((header, index) => (
|
||||
<div key={index}>{header}</div>
|
||||
))}
|
||||
<div className='faded'>{details}</div>
|
||||
</div>
|
||||
))}
|
||||
trigger={trigger}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user