Show period timestamps (#8156)
This commit is contained in:
parent
505d7593cd
commit
7c80f52df5
@ -1,6 +1,8 @@
|
||||
// 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 { BN } from '@polkadot/util';
|
||||
import type { Referendum, ReferendumProps as Props } from '../types';
|
||||
|
||||
@ -8,6 +10,9 @@ 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';
|
||||
@ -31,14 +36,47 @@ function expandOngoing (info: Referendum['info']): Expanded {
|
||||
};
|
||||
}
|
||||
|
||||
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: { decisionDeposit, proposalHash, submissionDeposit, tally }, shortHash, tallyTotal } = useMemo(
|
||||
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 preimage = usePreimage(proposalHash);
|
||||
|
||||
return (
|
||||
@ -55,6 +93,21 @@ 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>
|
||||
<Deposits
|
||||
canDeposit
|
||||
decision={decisionDeposit}
|
||||
|
||||
@ -34,6 +34,11 @@ interface HashState {
|
||||
isHashValid: boolean;
|
||||
}
|
||||
|
||||
interface DefaultAtAfter {
|
||||
defaults: [{ isValid: boolean, value: { After: BN } }];
|
||||
trackId: number;
|
||||
}
|
||||
|
||||
function getOrigin (api: ApiPromise, specName: string, palletReferenda: string, tracks?: [BN, PalletReferendaTrackInfo][], trackId?: number): Record<string, string> | undefined {
|
||||
let origin: Record<string, string> | undefined;
|
||||
|
||||
@ -55,26 +60,47 @@ function getOrigin (api: ApiPromise, specName: string, palletReferenda: string,
|
||||
return origin;
|
||||
}
|
||||
|
||||
function getDefaultEnactment (prev: DefaultAtAfter | null, bestNumber: BN, trackId: number, tracks: [BN, PalletReferendaTrackInfo][]): DefaultAtAfter {
|
||||
if (prev && prev.trackId === trackId) {
|
||||
return prev;
|
||||
}
|
||||
|
||||
const track = tracks.find(([id]) => id.eqn(trackId));
|
||||
|
||||
return {
|
||||
defaults: [{
|
||||
isValid: true,
|
||||
value: {
|
||||
After: track
|
||||
? bestNumber
|
||||
.add(track[1].preparePeriod)
|
||||
.add(track[1].decisionPeriod)
|
||||
.add(track[1].confirmPeriod)
|
||||
.add(track[1].minEnactmentPeriod)
|
||||
: bestNumber.addn(1000)
|
||||
}
|
||||
}],
|
||||
trackId
|
||||
};
|
||||
}
|
||||
|
||||
function Submit ({ className = '', isMember, members, palletReferenda, tracks }: Props): React.ReactElement<Props> | null {
|
||||
const { t } = useTranslation();
|
||||
const { api, specName } = useApi();
|
||||
const bestNumber = useBestNumber();
|
||||
const [isSubmitOpen, toggleSubmit] = useToggle();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
const [trackId, setTrack] = useState<number | undefined>();
|
||||
const [trackId, setTrack] = useState<number | undefined>(undefined);
|
||||
const [origin, setOrigin] = useState<RawParam['value'] | null>(null);
|
||||
const [atAfter, setAtAfter] = useState<RawParam['value'] | null>(null);
|
||||
const [defaultAtAfter, setDefaultAtAfter] = useState<RawParam[] | null>(null);
|
||||
const [defaultAtAfter, setDefaultAtAfter] = useState<DefaultAtAfter | null>(null);
|
||||
const [{ hash, isHashValid }, setHash] = useState<HashState>({ hash: '', isHashValid: false });
|
||||
|
||||
useEffect((): void => {
|
||||
bestNumber && setDefaultAtAfter((prev) =>
|
||||
prev || [{
|
||||
isValid: true,
|
||||
value: { After: bestNumber.addn(1000) }
|
||||
}]
|
||||
tracks && trackId !== undefined && bestNumber && setDefaultAtAfter((prev) =>
|
||||
getDefaultEnactment(prev, bestNumber, trackId, tracks)
|
||||
);
|
||||
}, [api, bestNumber]);
|
||||
}, [api, bestNumber, trackId, tracks]);
|
||||
|
||||
const originParam = useMemo(
|
||||
() => getOrigin(api, specName, palletReferenda, tracks, trackId),
|
||||
@ -185,9 +211,10 @@ function Submit ({ className = '', isMember, members, palletReferenda, tracks }:
|
||||
<Params
|
||||
className='timeSelect'
|
||||
isError={isInvalidAt}
|
||||
key={`after:${defaultAtAfter.trackId.toString()}`}
|
||||
onChange={_onChangeAtAfter}
|
||||
params={atAfterType}
|
||||
values={defaultAtAfter}
|
||||
values={defaultAtAfter.defaults}
|
||||
/>
|
||||
</Modal.Columns>
|
||||
)}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user