From 62262e9aafd1486e6eaa712d141b28bc731700c2 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 22 Sep 2022 15:46:58 +0300 Subject: [PATCH] Referenda time displays (#8157) * Referenda time displays * Adjust layout --- .../page-referenda/src/Referenda/RefEnd.tsx | 36 ++++++ .../src/Referenda/RefOngoing.tsx | 109 ++++++++---------- .../page-referenda/src/Referenda/RefTuple.tsx | 20 ++-- 3 files changed, 98 insertions(+), 67 deletions(-) create mode 100644 packages/page-referenda/src/Referenda/RefEnd.tsx diff --git a/packages/page-referenda/src/Referenda/RefEnd.tsx b/packages/page-referenda/src/Referenda/RefEnd.tsx new file mode 100644 index 0000000000..7ec6dbe8ad --- /dev/null +++ b/packages/page-referenda/src/Referenda/RefEnd.tsx @@ -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 { + const bestNumber = useBestNumber(); + + return ( + + {bestNumber && when && ( + <> +
{label}
+ {when.gt(bestNumber) && ( + + )} +
#{formatNumber(when)}
+ + )} + + ); +} + +export default React.memo(RefEnd); diff --git a/packages/page-referenda/src/Referenda/RefOngoing.tsx b/packages/page-referenda/src/Referenda/RefOngoing.tsx index 7c6c09c65e..5914a9f1e4 100644 --- a/packages/page-referenda/src/Referenda/RefOngoing.tsx +++ b/packages/page-referenda/src/Referenda/RefOngoing.tsx @@ -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, 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 { 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 } }) } - - {bestNumber && periodEnd && ( - <> - { - confirmEnd - ? t('Confirming') - : decideEnd - ? t('Deciding') - : t('Preparing') - } - - #{formatNumber(periodEnd)} - - )} - + ('Confirming') + : decideEnd + ? t('Deciding') + : t('Preparing') + } + when={periodEnd} + /> | 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 ( <> + - {when && ( - when.toUTCString() - )} - + colSpan={3} + /> - {info.type} + );