Referenda time displays (#8157)

* Referenda time displays

* Adjust layout
This commit is contained in:
Jaco 2022-09-22 15:46:58 +03:00 committed by GitHub
parent 0ac3a49b17
commit 62262e9aaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 67 deletions

View File

@ -0,0 +1,36 @@
// Copyright 2017-2022 @polkadot/app-referenda authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BN } from '@polkadot/util';
import React from 'react';
import { useBestNumber } from '@polkadot/react-hooks';
import { BlockToTime } from '@polkadot/react-query';
import { formatNumber } from '@polkadot/util';
interface Props {
className?: string;
label: string;
when: BN | null;
}
function RefEnd ({ className = '', label, when }: Props): React.ReactElement<Props> {
const bestNumber = useBestNumber();
return (
<td className={`${className} number`}>
{bestNumber && when && (
<>
<div>{label}</div>
{when.gt(bestNumber) && (
<BlockToTime value={when.sub(bestNumber)} />
)}
<div>#{formatNumber(when)}</div>
</>
)}
</td>
);
}
export default React.memo(RefEnd);

View File

@ -1,8 +1,7 @@
// Copyright 2017-2022 @polkadot/app-referenda authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Option } from '@polkadot/types';
import type { PalletReferendaDecidingStatus, PalletReferendaTrackInfo } from '@polkadot/types/lookup';
import type { PalletReferendaTrackInfo } from '@polkadot/types/lookup';
import type { BN } from '@polkadot/util';
import type { Referendum, ReferendumProps as Props } from '../types';
@ -10,71 +9,70 @@ import React, { useMemo } from 'react';
import usePreimage from '@polkadot/app-preimages/usePreimage';
import { CallExpander, Progress } from '@polkadot/react-components';
import { useBestNumber } from '@polkadot/react-hooks';
import { BlockToTime } from '@polkadot/react-query';
import { formatNumber } from '@polkadot/util';
import { useTranslation } from '../translate';
import Deposits from './Deposits';
import RefEnd from './RefEnd';
import Vote from './Vote';
import Votes from './Votes';
interface Expanded {
ongoing: Referendum['info']['asOngoing'];
tallyTotal: BN;
periods: {
periodEnd: BN | null;
prepareEnd: BN | null;
decideEnd: BN | null;
confirmEnd: BN | null;
};
shortHash: string;
tallyTotal: BN;
}
function expandOngoing (info: Referendum['info']): Expanded {
function expandOngoing (info: Referendum['info'], track?: PalletReferendaTrackInfo): Expanded {
const ongoing = info.asOngoing;
const hexHash = ongoing.proposalHash.toHex();
let prepareEnd: BN | null = null;
let decideEnd: BN | null = null;
let confirmEnd: BN | null = null;
if (track) {
const { deciding, submitted } = ongoing;
if (deciding.isSome) {
const d = deciding.unwrap();
if (d.confirming.isSome) {
// we are confirming
confirmEnd = d.confirming.unwrap().add(track.confirmPeriod);
} else {
// we are still deciding
decideEnd = d.since.add(track.decisionPeriod);
}
} else {
// we are still preparing
prepareEnd = submitted.add(track.preparePeriod);
}
}
return {
ongoing,
periods: {
confirmEnd,
decideEnd,
periodEnd: confirmEnd || decideEnd || prepareEnd,
prepareEnd
},
shortHash: `${hexHash.slice(0, 8)}…${hexHash.slice(-6)}`,
tallyTotal: ongoing.tally.ayes.add(ongoing.tally.nays)
};
}
function expandPeriods (submitted: BN, optDeciding: Option<PalletReferendaDecidingStatus>, track?: PalletReferendaTrackInfo): [BN | null, BN | null, BN | null, BN | null] {
if (!track) {
return [null, null, null, null];
}
let prepareEnd: BN | null = null;
let decideEnd: BN | null = null;
let confirmEnd: BN | null = null;
if (optDeciding.isSome) {
const deciding = optDeciding.unwrap();
if (deciding.confirming.isSome) {
// we are confirming
confirmEnd = deciding.confirming.unwrap().add(track.confirmPeriod);
} else {
// we are still deciding
decideEnd = deciding.since.add(track.decisionPeriod);
}
} else {
// we are still preparing
prepareEnd = submitted.add(track.preparePeriod);
}
return [prepareEnd, decideEnd, confirmEnd, confirmEnd || decideEnd || prepareEnd];
}
function Ongoing ({ isMember, members, palletReferenda, palletVote, value: { id, info, isConvictionVote, track } }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const bestNumber = useBestNumber();
const { ongoing: { deciding, decisionDeposit, proposalHash, submissionDeposit, submitted, tally }, shortHash, tallyTotal } = useMemo(
() => expandOngoing(info),
[info]
);
const [, decideEnd, confirmEnd, periodEnd] = useMemo(
() => expandPeriods(submitted, deciding, track),
[deciding, submitted, track]
const { ongoing: { decisionDeposit, proposalHash, submissionDeposit, tally }, periods: { confirmEnd, decideEnd, periodEnd }, shortHash, tallyTotal } = useMemo(
() => expandOngoing(info, track),
[info, track]
);
const preimage = usePreimage(proposalHash);
@ -93,21 +91,16 @@ function Ongoing ({ isMember, members, palletReferenda, palletVote, value: { id,
: t('preimage {{shortHash}}', { replace: { shortHash } })
}
</td>
<td className='number'>
{bestNumber && periodEnd && (
<>
{
confirmEnd
? t<string>('Confirming')
: decideEnd
? t<string>('Deciding')
: t<string>('Preparing')
}
<BlockToTime value={periodEnd.sub(bestNumber)} />
#{formatNumber(periodEnd)}
</>
)}
</td>
<RefEnd
label={
confirmEnd
? t<string>('Confirming')
: decideEnd
? t<string>('Deciding')
: t<string>('Preparing')
}
when={periodEnd}
/>
<Deposits
canDeposit
decision={decisionDeposit}

View File

@ -3,16 +3,18 @@
import type { Option } from '@polkadot/types';
import type { PalletReferendaDeposit } from '@polkadot/types/lookup';
import type { BN } from '@polkadot/util';
import type { Referendum, ReferendumProps as Props } from '../types';
import React, { useMemo } from 'react';
import Deposits from './Deposits';
import RefEnd from './RefEnd';
interface Expanded {
decision: Option<PalletReferendaDeposit> | null;
submit: PalletReferendaDeposit | null;
when: Date | null;
when: BN | null;
}
function expandTuple (info: Referendum['info']): Expanded {
@ -30,7 +32,7 @@ function expandTuple (info: Referendum['info']): Expanded {
? {
decision: data[2],
submit: data[1],
when: new Date(data[0].toNumber())
when: data[0]
}
: {
decision: null,
@ -48,6 +50,10 @@ function Tuple ({ palletReferenda, value: { id, info, track } }: Props): React.R
return (
<>
<td className='all' />
<RefEnd
label={info.type}
when={when}
/>
<Deposits
canRefund
decision={decision}
@ -58,14 +64,10 @@ function Tuple ({ palletReferenda, value: { id, info, track } }: Props): React.R
/>
<td
className='number'
colSpan={2}
>
{when && (
when.toUTCString()
)}
</td>
colSpan={3}
/>
<td className='number'>
{info.type}
</td>
</>
);