diff --git a/packages/page-parachains/src/Overview/LeaseBlocks.tsx b/packages/page-parachains/src/Overview/LeaseBlocks.tsx new file mode 100644 index 0000000000..ec1bdc0dc7 --- /dev/null +++ b/packages/page-parachains/src/Overview/LeaseBlocks.tsx @@ -0,0 +1,45 @@ +// Copyright 2017-2021 @polkadot/app-parachains authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type BN from 'bn.js'; +import type { LeasePeriod } from '../types'; + +import React, { useMemo } from 'react'; + +import { BlockToTime } from '@polkadot/react-query'; +import { BN_ONE, bnToBn } from '@polkadot/util'; + +interface Props { + children?: React.ReactNode; + className?: string; + leasePeriod?: LeasePeriod | null; + value?: number | BN | null; +} + +function LeaseBlocks ({ children, className, leasePeriod, value }: Props): React.ReactElement | null { + console.log(JSON.stringify({ leasePeriod, value })); + + const blocks = useMemo( + () => leasePeriod && value && + bnToBn(value) + .sub(BN_ONE) + .imul(leasePeriod.length) + .iadd(leasePeriod.remainder), + [leasePeriod, value] + ); + + if (!leasePeriod || !blocks) { + return null; + } + + return ( + + {children} + + ); +} + +export default React.memo(LeaseBlocks); diff --git a/packages/page-parachains/src/Overview/Summary.tsx b/packages/page-parachains/src/Overview/Summary.tsx index 1f93478c90..c3dc9b1cca 100644 --- a/packages/page-parachains/src/Overview/Summary.tsx +++ b/packages/page-parachains/src/Overview/Summary.tsx @@ -53,7 +53,7 @@ function Summary ({ leasePeriod, parachainCount, proposalCount, upcomingCount }: label={t('lease period')} progress={{ total: leasePeriod.length, - value: leasePeriod.remainder, + value: leasePeriod.progress, withTime: true }} /> diff --git a/packages/page-parachains/src/Overview/Upcoming.tsx b/packages/page-parachains/src/Overview/Upcoming.tsx index a32e669c68..643706e4d6 100644 --- a/packages/page-parachains/src/Overview/Upcoming.tsx +++ b/packages/page-parachains/src/Overview/Upcoming.tsx @@ -1,10 +1,10 @@ // Copyright 2017-2021 @polkadot/app-parachains authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type BN from 'bn.js'; import type { Option, Vec } from '@polkadot/types'; import type { AccountId, BalanceOf, HeadData, ParaGenesisArgs, ParaId, ParaInfo, ParaLifecycle } from '@polkadot/types/interfaces'; import type { ITuple } from '@polkadot/types/types'; +import type { LeasePeriod } from '../types'; import type { LeaseInfo, QueuedAction } from './types'; import React from 'react'; @@ -15,11 +15,12 @@ import { formatNumber } from '@polkadot/util'; import { useTranslation } from '../translate'; import { sliceHex } from '../util'; +import LeaseBlocks from './LeaseBlocks'; import Lifecycle from './Lifecycle'; interface Props { - currentPeriod: BN | null; id: ParaId; + leasePeriod: LeasePeriod | null; nextAction?: QueuedAction; } @@ -67,7 +68,7 @@ const optMulti = { }) }; -function Upcoming ({ currentPeriod, id, nextAction }: Props): React.ReactElement { +function Upcoming ({ id, leasePeriod, nextAction }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const { headHex, leases, lifecycle, manager } = useCallMulti([ @@ -90,11 +91,18 @@ function Upcoming ({ currentPeriod, id, nextAction }: Props): React.ReactElement nextAction={nextAction} /> - - - {currentPeriod && leases && ( + + {leasePeriod && leasePeriod.currentPeriod && leases && ( leases.length - ? leases.map(({ period }) => formatNumber(currentPeriod.addn(period))).join(', ') + ? ( + <> +
{leases.map(({ period }) => formatNumber(leasePeriod.currentPeriod.addn(period))).join(', ')}
+ + + ) : t('None') )} diff --git a/packages/page-parachains/src/Overview/Upcomings.tsx b/packages/page-parachains/src/Overview/Upcomings.tsx index f27fb82631..07226df7f3 100644 --- a/packages/page-parachains/src/Overview/Upcomings.tsx +++ b/packages/page-parachains/src/Overview/Upcomings.tsx @@ -1,8 +1,8 @@ // 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 { LeasePeriod } from '../types'; import type { QueuedAction } from './types'; import React, { useRef } from 'react'; @@ -14,19 +14,18 @@ import Upcoming from './Upcoming'; interface Props { actionsQueue: QueuedAction[]; - currentPeriod: BN | null; ids?: ParaId[]; + leasePeriod: LeasePeriod | null; } -function Upcomings ({ actionsQueue, currentPeriod, ids }: Props): React.ReactElement { +function Upcomings ({ actionsQueue, ids, leasePeriod }: Props): React.ReactElement { const { t } = useTranslation(); const headerRef = useRef([ [t('upcoming'), 'start', 3], [t('head'), 'start'], [t('lifecycle'), 'start'], - [], - [t('leases'), 'start'] + [t('leases')] ]); return ( @@ -36,9 +35,9 @@ function Upcomings ({ actionsQueue, currentPeriod, ids }: Props): React.ReactEle > {ids?.map((id): React.ReactNode => ( paraIds.some((p) => p.eq(id)) )} diff --git a/packages/page-parachains/src/Overview/index.tsx b/packages/page-parachains/src/Overview/index.tsx index 87c38f4617..edb406b0d0 100644 --- a/packages/page-parachains/src/Overview/index.tsx +++ b/packages/page-parachains/src/Overview/index.tsx @@ -45,8 +45,8 @@ function Overview ({ className, leasePeriod, proposals }: Props): React.ReactEle /> )} diff --git a/packages/page-parachains/src/types.ts b/packages/page-parachains/src/types.ts index 83ee68f5a5..b42cb60345 100644 --- a/packages/page-parachains/src/types.ts +++ b/packages/page-parachains/src/types.ts @@ -25,6 +25,7 @@ export interface ScheduledProposals { export interface LeasePeriod { currentPeriod: BN; length: BN; + progress: BN; remainder: BN; } diff --git a/packages/page-parachains/src/useLeasePeriod.ts b/packages/page-parachains/src/useLeasePeriod.ts index a6e75577ab..982022a4e4 100644 --- a/packages/page-parachains/src/useLeasePeriod.ts +++ b/packages/page-parachains/src/useLeasePeriod.ts @@ -15,11 +15,13 @@ export default function useLeasePeriod (): LeasePeriod | null { return useMemo((): LeasePeriod | null => { if (api.consts.slots?.leasePeriod && bestNumber) { const length = api.consts.slots.leasePeriod as BlockNumber; + const progress = bestNumber.mod(length); return { currentPeriod: bestNumber.div(length), length, - remainder: bestNumber.mod(length) + progress, + remainder: length.sub(progress) }; }