Display next action countdown (#4849)

* Display next action countdown

* Show parathreads
This commit is contained in:
Jaco Greeff 2021-03-14 19:45:02 +01:00 committed by GitHub
parent 97d2e65deb
commit 15c08c6364
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 378 additions and 48 deletions

View File

@ -18,7 +18,7 @@ function DueBlocks ({ dueBlocks, endBlock, label }: Props): React.ReactElement<P
<>
{dueBlocks.gtn(0) && (
<>
<BlockToTime blocks={dueBlocks}>
<BlockToTime value={dueBlocks}>
&nbsp;({label})
</BlockToTime>
#{formatNumber(endBlock)}

View File

@ -126,7 +126,7 @@ function Contract ({ className, contract, index, links, onCall }: Props): React.
evictAt
? (
<>
<BlockToTime blocks={evictAt.sub(bestNumber)} />
<BlockToTime value={evictAt.sub(bestNumber)} />
#{formatNumber(evictAt)}
</>
)

View File

@ -81,7 +81,7 @@ function Motion ({ className = '', isMember, members, motion: { hash, proposal,
<td className='number together'>
{remainingBlocks && end && (
<>
<BlockToTime blocks={remainingBlocks} />
<BlockToTime value={remainingBlocks} />
#{formatNumber(end)}
</>
)}

View File

@ -30,7 +30,7 @@ function DispatchEntry ({ value: { at, image, imageHash, index } }: Props): Reac
<td className='number together'>
{bestNumber && (
<>
<BlockToTime blocks={at.sub(bestNumber)} />
<BlockToTime value={at.sub(bestNumber)} />
#{formatNumber(at)}
</>
)}

View File

@ -33,7 +33,7 @@ function Scheduled ({ className = '', value: { blockNumber, call, maybeId, maybe
<td className='number together'>
{bestNumber && (
<>
<BlockToTime blocks={blockNumber.sub(bestNumber)} />
<BlockToTime value={blockNumber.sub(bestNumber)} />
#{formatNumber(blockNumber)}
</>
)}

View File

@ -92,11 +92,11 @@ function Referendum ({ className = '', value: { allAye, allNay, image, imageHash
proposal={image?.proposal}
/>
<td className='number together media--1200'>
<BlockToTime blocks={remainBlock} />
<BlockToTime value={remainBlock} />
{t<string>('{{blocks}} blocks', { replace: { blocks: formatNumber(remainBlock) } })}
</td>
<td className='number together media--1400'>
<BlockToTime blocks={enactBlock.sub(bestNumber)} />
<BlockToTime value={enactBlock.sub(bestNumber)} />
#{formatNumber(enactBlock)}
</td>
<td className='number together media--1400'>

View File

@ -25,7 +25,7 @@ function Summary (): React.ReactElement {
className='media--800'
label={t<string>('target')}
>
<BlockToTime blocks={BN_ONE} />
<BlockToTime value={BN_ONE} />
</CardSummary>
{api.query.balances && (
<CardSummary

View File

@ -59,7 +59,7 @@ function Fund ({ bestNumber, className, isOngoing, value: { info: { cap, deposit
<td className='address'><AddressMini value={depositor} /></td>
<td className='number together'>
{blocksLeft && (
<BlockToTime blocks={blocksLeft} />
<BlockToTime value={blocksLeft} />
)}
#{formatNumber(end)}
</td>

View File

@ -0,0 +1,27 @@
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ParaLifecycle } from '@polkadot/types/interfaces';
import type { QueuedAction } from './types';
import React from 'react';
import { SessionToTime } from '@polkadot/react-query';
interface Props {
lifecycle: ParaLifecycle | null;
nextAction?: QueuedAction;
}
function Lifecycle ({ lifecycle, nextAction }: Props): React.ReactElement<Props> | null {
return lifecycle && (
<>
{lifecycle.toString()}
{nextAction && (
<SessionToTime value={nextAction.sessionIndex} />
)}
</>
);
}
export default React.memo(Lifecycle);

View File

@ -4,6 +4,7 @@
import type { Option, Vec } from '@polkadot/types';
import type { AccountId, BlockNumber, HeadData, Header, ParaId, ParaLifecycle } from '@polkadot/types/interfaces';
import type { Codec } from '@polkadot/types/types';
import type { QueuedAction } from './types';
import BN from 'bn.js';
import React, { useCallback, useMemo } from 'react';
@ -15,6 +16,7 @@ import { formatNumber } from '@polkadot/util';
import { useTranslation } from '../translate';
import { sliceHex } from '../util';
import Lifecycle from './Lifecycle';
interface Props {
bestNumber?: BN;
@ -23,6 +25,7 @@ interface Props {
isScheduled?: boolean;
lastBacked?: [string, string, BN];
lastInclusion?: [string, string, BN];
nextAction?: QueuedAction;
validators?: AccountId[];
}
@ -68,7 +71,7 @@ const optionsMulti = {
})
};
function Parachain ({ bestNumber, className = '', id, isScheduled, lastBacked, lastInclusion, validators }: Props): React.ReactElement<Props> {
function Parachain ({ bestNumber, className = '', id, isScheduled, lastBacked, lastInclusion, nextAction, validators }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const { api: paraApi } = useParaApi(id);
@ -125,7 +128,10 @@ function Parachain ({ bestNumber, className = '', id, isScheduled, lastBacked, l
</td>
<td className='start together hash'>{paraInfo.headHex}</td>
<td className='start media--1100'>
{paraInfo.lifecycle && paraInfo.lifecycle.toString()}
<Lifecycle
lifecycle={paraInfo.lifecycle}
nextAction={nextAction}
/>
</td>
<td className='all' />
<td className='number'>{blockDelay && <BlockToTime blocks={blockDelay} />}</td>
@ -144,7 +150,7 @@ function Parachain ({ bestNumber, className = '', id, isScheduled, lastBacked, l
<td className='number media--1300'>
{paraInfo.updateAt && bestNumber && (
<>
<BlockToTime blocks={bestNumber.sub(paraInfo.updateAt)} />
<BlockToTime value={bestNumber.sub(paraInfo.updateAt)} />
#{formatNumber(paraInfo.updateAt)}
</>
)}

View File

@ -4,6 +4,7 @@
import type { SignedBlockExtended } from '@polkadot/api-derive/type';
import type { AccountId, CandidateReceipt, Event, ParaId, ParaValidatorIndex } from '@polkadot/types/interfaces';
import type { ScheduledProposals } from '../types';
import type { QueuedAction } from './types';
import BN from 'bn.js';
import React, { useEffect, useMemo, useRef, useState } from 'react';
@ -15,6 +16,7 @@ import { useTranslation } from '../translate';
import Parachain from './Parachain';
interface Props {
actionsQueue: QueuedAction[];
ids?: ParaId[];
scheduled?: ScheduledProposals[];
}
@ -42,7 +44,7 @@ function includeEntry (map: EventMap, event: Event, blockHash: string, blockNumb
}
}
function ParachainList ({ ids, scheduled }: Props): React.ReactElement<Props> {
function Parachains ({ actionsQueue, ids, scheduled }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const bestNumber = useBestNumber();
@ -131,6 +133,9 @@ function ParachainList ({ ids, scheduled }: Props): React.ReactElement<Props> {
key={id.toString()}
lastBacked={lastBacked[id.toString()]}
lastInclusion={lastIncluded[id.toString()]}
nextAction={actionsQueue.find(({ paraIds }) =>
paraIds.some((p) => p.eq(id))
)}
validators={validatorMap[index]}
/>
))}
@ -138,4 +143,4 @@ function ParachainList ({ ids, scheduled }: Props): React.ReactElement<Props> {
);
}
export default React.memo(ParachainList);
export default React.memo(Parachains);

View File

@ -0,0 +1,94 @@
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Option, Vec } from '@polkadot/types';
import type { AccountId, BalanceOf, HeadData, ParaId, ParaLifecycle } from '@polkadot/types/interfaces';
import type { ITuple } from '@polkadot/types/types';
import type { LeaseInfo, QueuedAction } from './types';
import BN from 'bn.js';
import React from 'react';
import { ParaLink } from '@polkadot/react-components';
import { useApi, useCallMulti } from '@polkadot/react-hooks';
import { formatNumber } from '@polkadot/util';
import { sliceHex } from '../util';
import Lifecycle from './Lifecycle';
interface Props {
className?: string;
currentPeriod: BN | null;
id: ParaId;
nextAction?: QueuedAction;
}
type QueryResult = [Option<HeadData>, Option<ParaLifecycle>, Vec<Option<ITuple<[AccountId, BalanceOf]>>>];
interface QueryState {
headHex: string | null;
leases: LeaseInfo[];
lifecycle: ParaLifecycle | null;
}
const optionsMulti = {
defaultValue: {
headHex: null,
leases: [],
lifecycle: null
},
transform: ([headData, optLifecycle, leases]: QueryResult): QueryState => ({
headHex: headData.isSome
? sliceHex(headData.unwrap())
: null,
leases: leases
? leases
.map((optLease, period): LeaseInfo | null => {
if (optLease.isNone) {
return null;
}
const [accountId, balance] = optLease.unwrap();
return {
accountId,
balance,
period
};
})
.filter((item): item is LeaseInfo => !!item)
: [],
lifecycle: optLifecycle.unwrapOr(null)
})
};
function Parachain ({ className = '', currentPeriod, id, nextAction }: Props): React.ReactElement<Props> {
const { api } = useApi();
const paraInfo = useCallMulti<QueryState>([
[api.query.paras.heads, id],
[api.query.paras.paraLifecycles, id],
[api.query.slots?.leases, id]
], optionsMulti);
return (
<tr className={className}>
<td className='number'><h1>{formatNumber(id)}</h1></td>
<td className='badge together'><ParaLink id={id} /></td>
<td className='start together hash'>{paraInfo.headHex}</td>
<td className='start media--1100'>
<Lifecycle
lifecycle={paraInfo.lifecycle}
nextAction={nextAction}
/>
</td>
<td className='all' />
<td className='start together'>
{currentPeriod &&
paraInfo.leases.map(({ period }) => formatNumber(currentPeriod.addn(period))).join(', ')
}
</td>
</tr>
);
}
export default React.memo(Parachain);

View File

@ -0,0 +1,51 @@
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type BN from 'bn.js';
import type { ParaId } from '@polkadot/types/interfaces';
import type { QueuedAction } from './types';
import React, { useRef } from 'react';
import { Table } from '@polkadot/react-components';
import { useTranslation } from '../translate';
import Parathread from './Parathread';
interface Props {
actionsQueue: QueuedAction[];
currentPeriod: BN | null;
ids?: ParaId[];
}
function Parathreads ({ actionsQueue, currentPeriod, ids }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const headerRef = useRef([
[t('waiting'), 'start', 2],
[t('genesis'), 'start'],
[t('lifecycle'), 'start'],
[],
[t('leases'), 'start']
]);
return (
<Table
empty={ids && t<string>('There are no waiting parathreads')}
header={headerRef.current}
>
{ids?.map((id): React.ReactNode => (
<Parathread
currentPeriod={currentPeriod}
id={id}
key={id.toString()}
nextAction={actionsQueue.find(({ paraIds }) =>
paraIds.some((p) => p.eq(id))
)}
/>
))}
</Table>
);
}
export default React.memo(Parathreads);

View File

@ -5,6 +5,7 @@ import type BN from 'bn.js';
import type { Option, Vec } from '@polkadot/types';
import type { AccountId, BalanceOf, ParaGenesisArgs, ParaId, ParaLifecycle } from '@polkadot/types/interfaces';
import type { ITuple } from '@polkadot/types/types';
import type { LeaseInfo, QueuedAction } from './types';
import React from 'react';
@ -14,16 +15,12 @@ import { formatNumber } from '@polkadot/util';
import { useTranslation } from '../translate';
import { sliceHex } from '../util';
import Lifecycle from './Lifecycle';
interface Props {
currentPeriod: BN | null;
id: ParaId;
}
interface LeaseInfo {
accountId: AccountId;
balance: BalanceOf;
period: number;
nextAction?: QueuedAction;
}
interface MultiState {
@ -61,7 +58,7 @@ const optMulti = {
})
};
function Upcoming ({ currentPeriod, id }: Props): React.ReactElement<Props> {
function Upcoming ({ currentPeriod, id, nextAction }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const { leases, lifecycle, upcomingGenesis } = useCallMulti<MultiState>([
@ -71,7 +68,7 @@ function Upcoming ({ currentPeriod, id }: Props): React.ReactElement<Props> {
], optMulti);
return (
<tr key={id.toString()}>
<tr>
<td className='number'><h1>{formatNumber(id)}</h1></td>
<td className='badge together'><ParaLink id={id} /></td>
<td className='start together hash'>
@ -80,7 +77,10 @@ function Upcoming ({ currentPeriod, id }: Props): React.ReactElement<Props> {
)}
</td>
<td className='start'>
{lifecycle && lifecycle.toString()}
<Lifecycle
lifecycle={lifecycle}
nextAction={nextAction}
/>
</td>
<td className='all' />
<td className='start together'>

View File

@ -3,6 +3,7 @@
import type BN from 'bn.js';
import type { ParaId } from '@polkadot/types/interfaces';
import type { QueuedAction } from './types';
import React, { useRef } from 'react';
@ -12,11 +13,12 @@ import { useTranslation } from '../translate';
import Upcoming from './Upcoming';
interface Props {
actionsQueue: QueuedAction[];
currentPeriod: BN | null;
ids?: ParaId[];
}
function UpcomingList ({ currentPeriod, ids }: Props): React.ReactElement<Props> {
function Upcomings ({ actionsQueue, currentPeriod, ids }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const headerRef = useRef([
@ -38,10 +40,13 @@ function UpcomingList ({ currentPeriod, ids }: Props): React.ReactElement<Props>
currentPeriod={currentPeriod}
id={id}
key={id.toString()}
nextAction={actionsQueue.find(({ paraIds }) =>
paraIds.some((p) => p.eq(id))
)}
/>
))}
</Table>
);
}
export default React.memo(UpcomingList);
export default React.memo(Upcomings);

View File

@ -9,10 +9,13 @@ import React, { useEffect, useState } from 'react';
import { useApi, useCall, useEventTrigger, useIsMountedRef } from '@polkadot/react-hooks';
import Actions from './Actions';
import Parachains from './ParachainList';
import Parachains from './Parachains';
import Parathreads from './Parathreads';
import Summary from './Summary';
import Upcoming from './UpcomingList';
import Upcomings from './Upcomings';
import useActionsQueue from './useActionsQueue';
import useLeasePeriod from './useLeasePeriod';
import useParathreads from './useParathreads';
interface Props {
className?: string;
@ -23,22 +26,24 @@ function Overview ({ className, proposals }: Props): React.ReactElement<Props> {
const { api } = useApi();
const paraIds = useCall<ParaId[]>(api.query.paras?.parachains);
const mountedRef = useIsMountedRef();
const actionsQueue = useActionsQueue();
const leasePeriod = useLeasePeriod();
const sessionTrigger = useEventTrigger([api.events.session.NewSession, api.events.registrar?.Registered]);
const threadIds = useParathreads();
const trigger = useEventTrigger([api.events.session.NewSession, api.events.registrar?.Registered]);
const [upcomingIds, setUpcomingIds] = useState<ParaId[]>([]);
useEffect((): void => {
sessionTrigger &&
trigger &&
api.query.paras?.upcomingParasGenesis
?.keys<[ParaId]>()
.then((keys): void => {
mountedRef.current &&
setUpcomingIds(
keys.map<ParaId>(({ args: [paraId] }) => paraId)
keys.map(({ args: [paraId] }) => paraId)
);
})
.catch(console.error);
}, [api, mountedRef, sessionTrigger]);
}, [api, mountedRef, trigger]);
return (
<div className={className}>
@ -52,10 +57,17 @@ function Overview ({ className, proposals }: Props): React.ReactElement<Props> {
{api.query.paras && (
<>
<Parachains
actionsQueue={actionsQueue}
ids={paraIds}
scheduled={proposals?.scheduled}
/>
<Upcoming
<Parathreads
actionsQueue={actionsQueue}
currentPeriod={leasePeriod && leasePeriod.currentPeriod}
ids={threadIds}
/>
<Upcomings
actionsQueue={actionsQueue}
currentPeriod={leasePeriod && leasePeriod.currentPeriod}
ids={upcomingIds}
/>

View File

@ -2,9 +2,21 @@
// SPDX-License-Identifier: Apache-2.0
import type BN from 'bn.js';
import type { AccountId, BalanceOf, ParaId } from '@polkadot/types/interfaces';
export interface LeaseInfo {
accountId: AccountId;
balance: BalanceOf;
period: number;
}
export interface LeasePeriod {
currentPeriod: BN;
length: BN;
remainder: BN;
}
export interface QueuedAction {
paraIds: ParaId[];
sessionIndex: BN;
}

View File

@ -0,0 +1,33 @@
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ParaId, SessionIndex } from '@polkadot/types/interfaces';
import type { QueuedAction } from './types';
import { useMemo } from 'react';
import { useApi, useCall, useCallMulti } from '@polkadot/react-hooks';
import { BN_FIVE, BN_FOUR, BN_ONE, BN_THREE, BN_TWO } from '@polkadot/util';
const INC = [BN_ONE, BN_TWO, BN_THREE, BN_FOUR, BN_FIVE];
export default function useActionsQueue (): QueuedAction[] {
const { api } = useApi();
const currentIndex = useCall<SessionIndex>(api.query.session.currentIndex);
const nextActions = useCallMulti<ParaId[][]>(
currentIndex
? INC.map((inc) => [api.query.paras.actionsQueue, currentIndex.add(inc)])
: []
);
return useMemo(
(): QueuedAction[] =>
currentIndex && nextActions
? nextActions.filter((paraIds) => paraIds.length).map((paraIds, index) => ({
paraIds,
sessionIndex: currentIndex.add(INC[index])
}))
: [],
[currentIndex, nextActions]
);
}

View File

@ -0,0 +1,36 @@
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Option } from '@polkadot/types';
import type { ParaId, ParaLifecycle } from '@polkadot/types/interfaces';
import { useEffect, useState } from 'react';
import { useApi, useEventTrigger } from '@polkadot/react-hooks';
export default function useParathreads (): ParaId[] | undefined {
const { api } = useApi();
const trigger = useEventTrigger([api.events.session.NewSession]);
const [result, setResult] = useState<ParaId[] | undefined>();
useEffect((): void => {
trigger &&
api.query.paras.paraLifecycles
.entries<Option<ParaLifecycle>, [ParaId]>()
.then((entries) => setResult(() =>
entries
.map(([{ args: [paraId] }, optValue]): ParaId | null => {
const value = optValue.unwrap();
return (value && (value.isParathread || value.isUpgradingToParachain || value.isOutgoingParathread))
? paraId
: null;
})
.filter((paraId): paraId is ParaId => !!paraId)
.sort((a, b) => a.cmp(b))
))
.catch(console.error);
}, [api, trigger]);
return result;
}

View File

@ -114,7 +114,7 @@ function PollApp ({ basePath, className }: Props): React.ReactElement<Props> {
)}
<div>
{canVote
? <BlockToTime blocks={blocksLeft} />
? <BlockToTime value={blocksLeft} />
: t<string>('Completed')
}
<div>#{formatNumber(api.consts.poll.end as BlockNumber)}</div>

View File

@ -73,7 +73,7 @@ function Unbond ({ className = '', controllerId, onClose, stakingLedger, stashId
help={t<string>('The bonding duration for any staked funds. After this period needs to be withdrawn.')}
label={t<string>('on-chain bonding duration')}
>
<BlockToTime blocks={bondedBlocks} />
<BlockToTime value={bondedBlocks} />
</Static>
)}
</Modal.Columns>

View File

@ -152,7 +152,7 @@ function Bond ({ className = '', isNominating, minNomination, onChange }: Props)
help={t<string>('The bonding duration for any staked funds. Needs to be unlocked and withdrawn to become available.')}
label={t<string>('on-chain bonding duration')}
>
<BlockToTime blocks={bondedBlocks} />
<BlockToTime value={bondedBlocks} />
</Static>
)}
</Modal.Columns>

View File

@ -86,7 +86,7 @@ function Stash ({ className = '', isDisabled, payout: { available, rewards, stas
<span className='payout-eras'>{eraStr}</span>
</td>
<td className='number'><FormatBalance value={available} /></td>
<td className='number'>{eraBlocks && <BlockToTime blocks={eraBlocks} />}</td>
<td className='number'>{eraBlocks && <BlockToTime value={eraBlocks} />}</td>
<td
className='button'
colSpan={3}

View File

@ -66,7 +66,7 @@ function Validator ({ className = '', isDisabled, payout }: Props): React.ReactE
<span className='payout-eras'>{eraStr}</span>
</td>
<td className='number'><FormatBalance value={payout.available} /></td>
<td className='number'>{eraBlocks && <BlockToTime blocks={eraBlocks} />}</td>
<td className='number'>{eraBlocks && <BlockToTime value={eraBlocks} />}</td>
<td
className='expand'
colSpan={2}

View File

@ -69,7 +69,7 @@ function Proposal ({ className = '', imageHash, members, prime }: Props): React.
<td className='number together'>
{remainingBlocks && end && (
<>
<BlockToTime blocks={remainingBlocks} />
<BlockToTime value={remainingBlocks} />
#{formatNumber(end)}
</>
)}

View File

@ -142,7 +142,7 @@ function Tip ({ bestNumber, className = '', defaultId, hash, isMember, members,
{closesAt
? (bestNumber && closesAt.gt(bestNumber)) && (
<div className='closingTimer'>
<BlockToTime blocks={closesAt.sub(bestNumber)} />
<BlockToTime value={closesAt.sub(bestNumber)} />
#{formatNumber(closesAt)}
</div>
)

View File

@ -263,7 +263,7 @@ function createBalanceItems (formatIndex: number, lookup: Record<string, string>
{bestNumber.lt((balancesAll as DeriveBalancesAll).vestingEndBlock) && (
<>
<div>
<BlockToTime blocks={(balancesAll as DeriveBalancesAll).vestingEndBlock.sub(bestNumber)} />
<BlockToTime value={(balancesAll as DeriveBalancesAll).vestingEndBlock.sub(bestNumber)} />
<div className='faded'>{t('until block')} {formatNumber((balancesAll as DeriveBalancesAll).vestingEndBlock)}</div>
</div>
<div>

View File

@ -66,7 +66,7 @@ function CardSummary ({ children, className = '', help, label, progress }: Props
progress && !progress.hideValue && (
<>
{isTimed && !children && (
<BlockToTime blocks={progress.total} />
<BlockToTime value={progress.total} />
)}
<div className={isTimed ? 'isSecondary' : 'isPrimary'}>
{!left || isUndefined(progress.total)
@ -79,8 +79,8 @@ function CardSummary ({ children, className = '', help, label, progress }: Props
}`
: (
<BlockToTime
blocks={progress.total.sub(progress.value)}
className='timer'
value={progress.total.sub(progress.value)}
/>
)
}

View File

@ -59,8 +59,8 @@ function groupLocks (t: TFunction, bestNumber: BN, locks: DeriveDemocracyLock[]
{isCountdown
? (
<BlockToTime
blocks={blocks}
label={`${t<string>('{{blocks}} blocks', { replace: { blocks: formatNumber(blocks) } })}, `}
value={blocks}
/>
)
: isFinished

View File

@ -75,8 +75,8 @@ function StakingUnbonding ({ className = '', stakingInfo }: Props): React.ReactE
{api.consts.babe?.epochDuration
? (
<BlockToTime
blocks={blocks}
label={`${t<string>('{{blocks}} blocks', { replace: { blocks: formatNumber(blocks) } })}, `}
value={blocks}
/>
)
: t<string>('{{eras}} eras remaining', { replace: { eras: formatNumber(eras) } })

View File

@ -11,17 +11,17 @@ import { useBlockTime } from '@polkadot/react-hooks';
interface Props {
api?: ApiPromise;
blocks?: BN;
children?: React.ReactNode;
className?: string;
isInline?: boolean;
label?: React.ReactNode;
value?: BN;
}
function BlockToTime ({ api, blocks, children, className = '', isInline, label }: Props): React.ReactElement<Props> | null {
const [, text] = useBlockTime(blocks, api);
function BlockToTime ({ api, children, className = '', isInline, label, value }: Props): React.ReactElement<Props> | null {
const [, text] = useBlockTime(value, api);
if (!blocks || blocks.isZero()) {
if (!value || value.isZero()) {
return null;
}

View File

@ -0,0 +1,48 @@
// Copyright 2017-2021 @polkadot/react-query authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { DeriveSessionProgress } from '@polkadot/api-derive/types';
import BN from 'bn.js';
import React, { useMemo } from 'react';
import { useApi, useCall } from '@polkadot/react-hooks';
import { BN_ZERO } from '@polkadot/util';
import BlockToTime from './BlockToTime';
interface Props {
children?: React.ReactNode;
className?: string;
isInline?: boolean;
label?: React.ReactNode;
value?: BN;
}
function SessionToTime ({ children, className, isInline, label, value }: Props): React.ReactElement<Props> | null {
const { api } = useApi();
const sessionInfo = useCall<DeriveSessionProgress>(api.derive.session.progress);
const blocks = useMemo(
() => sessionInfo && value && sessionInfo.currentIndex.lt(value)
? value
.sub(sessionInfo.currentIndex)
.imul(sessionInfo.sessionLength)
.isub(sessionInfo.sessionProgress)
: BN_ZERO,
[sessionInfo, value]
);
return (
<BlockToTime
className={className}
isInline={isInline}
label={label}
value={blocks}
>
{children}
</BlockToTime>
);
}
export default React.memo(SessionToTime);

View File

@ -18,5 +18,6 @@ export { default as LockedVote } from './LockedVote';
export { default as NodeName } from './NodeName';
export { default as NodeVersion } from './NodeVersion';
export { default as Nonce } from './Nonce';
export { default as SessionToTime } from './SessionToTime';
export { default as TimeNow } from './TimeNow';
export { default as TotalIssuance } from './TotalIssuance';