Optimize paras rendering (#4874)
* Parachain voters * | null * Optimize paras rendering * Adjust * linting * Adjust * Show timeout events * Remove useless realy: yes * Tuple to interface
This commit is contained in:
parent
06903ab0c2
commit
7ae7dfa08b
@ -25,6 +25,7 @@ interface Props {
|
||||
function Fund ({ bestNumber, className, isOngoing, value: { info: { cap, depositor, end, firstSlot, lastSlot, raised, retiring }, paraId } }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
|
||||
const blocksLeft = useMemo(
|
||||
() => bestNumber && end.gt(bestNumber) && end.sub(bestNumber),
|
||||
[bestNumber, end]
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
import type BN from 'bn.js';
|
||||
import type { Campaign } from './types';
|
||||
|
||||
import React, { useRef } from 'react';
|
||||
import React, { useMemo, useRef } from 'react';
|
||||
|
||||
import { Table } from '@polkadot/react-components';
|
||||
|
||||
@ -17,6 +17,12 @@ interface Props {
|
||||
value: Campaign[] | null;
|
||||
}
|
||||
|
||||
function extractLists (value: Campaign[] | null): [Campaign[] | null, Campaign[] | null] {
|
||||
return value
|
||||
? [value.filter(({ isEnded }) => !isEnded), value.filter(({ isEnded }) => isEnded)]
|
||||
: [null, null];
|
||||
}
|
||||
|
||||
function Funds ({ bestNumber, className, value }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
|
||||
@ -35,31 +41,36 @@ function Funds ({ bestNumber, className, value }: Props): React.ReactElement<Pro
|
||||
[t('raised')]
|
||||
]);
|
||||
|
||||
const [active, ended] = useMemo(
|
||||
() => extractLists(value),
|
||||
[value]
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Table
|
||||
className={className}
|
||||
empty={value && t<string>('No active campaigns found')}
|
||||
empty={active && t<string>('No active campaigns found')}
|
||||
header={headerActiveRef.current}
|
||||
>
|
||||
{value?.filter(({ isEnded }) => !isEnded).map((fund) => (
|
||||
{active?.map((fund) => (
|
||||
<Fund
|
||||
bestNumber={bestNumber}
|
||||
isOngoing
|
||||
key={fund.paraId.toString()}
|
||||
key={fund.key}
|
||||
value={fund}
|
||||
/>
|
||||
))}
|
||||
</Table>
|
||||
<Table
|
||||
className={className}
|
||||
empty={value && t<string>('No completed campaigns found')}
|
||||
empty={ended && t<string>('No completed campaigns found')}
|
||||
header={headedEndedRef.current}
|
||||
>
|
||||
{value?.filter(({ isEnded }) => isEnded).map((fund) => (
|
||||
{ended?.map((fund) => (
|
||||
<Fund
|
||||
bestNumber={bestNumber}
|
||||
key={fund.paraId.toString()}
|
||||
key={fund.key}
|
||||
value={fund}
|
||||
/>
|
||||
))}
|
||||
|
||||
@ -6,5 +6,6 @@ import type { FundInfo, ParaId } from '@polkadot/types/interfaces';
|
||||
export interface Campaign {
|
||||
info: FundInfo;
|
||||
isEnded?: boolean;
|
||||
key: string;
|
||||
paraId: ParaId;
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ const EMPTY: Result = {
|
||||
function extractCampaigns (optFunds: Option<FundInfo>[], paraIds: ParaId[]): Campaign[] | null {
|
||||
return optFunds && paraIds.length === optFunds.length
|
||||
? paraIds
|
||||
.map((paraId, i) => ({ info: optFunds[i].unwrapOr(null), paraId }))
|
||||
.map((paraId, i) => ({ info: optFunds[i].unwrapOr(null), key: paraId.toString(), paraId }))
|
||||
.filter((fund): fund is Campaign => !!fund.info)
|
||||
: null;
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
import type { Option, Vec } from '@polkadot/types';
|
||||
import type { AccountId, BlockNumber, CandidatePendingAvailability, HeadData, Header, ParaId, ParaLifecycle } from '@polkadot/types/interfaces';
|
||||
import type { Codec } from '@polkadot/types/types';
|
||||
import type { QueuedAction } from './types';
|
||||
import type { EventMapInfo, QueuedAction } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
@ -23,8 +23,9 @@ interface Props {
|
||||
className?: string;
|
||||
id: ParaId;
|
||||
isScheduled?: boolean;
|
||||
lastBacked?: [string, string, BN];
|
||||
lastInclusion?: [string, string, BN];
|
||||
lastBacked?: EventMapInfo;
|
||||
lastInclusion?: EventMapInfo;
|
||||
lastTimeout?: EventMapInfo;
|
||||
nextAction?: QueuedAction;
|
||||
sessionValidators?: AccountId[] | null;
|
||||
validators?: AccountId[];
|
||||
@ -75,7 +76,16 @@ const optionsMulti = {
|
||||
})
|
||||
};
|
||||
|
||||
function Parachain ({ bestNumber, className = '', id, isScheduled, lastBacked, lastInclusion, nextAction, sessionValidators, validators }: Props): React.ReactElement<Props> {
|
||||
function renderAddresses (list?: AccountId[]): JSX.Element[] | undefined {
|
||||
return list?.map((id) => (
|
||||
<AddressMini
|
||||
key={id.toString()}
|
||||
value={id}
|
||||
/>
|
||||
));
|
||||
}
|
||||
|
||||
function Parachain ({ bestNumber, className = '', id, isScheduled, lastBacked, lastInclusion, lastTimeout, nextAction, sessionValidators, validators }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { api: paraApi } = useParaApi(id);
|
||||
@ -96,7 +106,7 @@ function Parachain ({ bestNumber, className = '', id, isScheduled, lastBacked, l
|
||||
const blockDelay = useMemo(
|
||||
() => bestNumber && (
|
||||
lastInclusion
|
||||
? bestNumber.sub(lastInclusion[2])
|
||||
? bestNumber.sub(lastInclusion.blockNumber)
|
||||
: paraInfo.watermark
|
||||
? bestNumber.sub(paraInfo.watermark)
|
||||
: undefined
|
||||
@ -105,22 +115,12 @@ function Parachain ({ bestNumber, className = '', id, isScheduled, lastBacked, l
|
||||
);
|
||||
|
||||
const valRender = useCallback(
|
||||
() => validators?.map((id) => (
|
||||
<AddressMini
|
||||
key={id.toString()}
|
||||
value={id}
|
||||
/>
|
||||
)),
|
||||
() => renderAddresses(validators),
|
||||
[validators]
|
||||
);
|
||||
|
||||
const bckRender = useCallback(
|
||||
() => nonBacked.map((id) => (
|
||||
<AddressMini
|
||||
key={id.toString()}
|
||||
value={id}
|
||||
/>
|
||||
)),
|
||||
() => renderAddresses(nonBacked),
|
||||
[nonBacked]
|
||||
);
|
||||
|
||||
@ -144,12 +144,14 @@ function Parachain ({ bestNumber, className = '', id, isScheduled, lastBacked, l
|
||||
return (
|
||||
<tr className={className}>
|
||||
<td className='number'><h1>{formatNumber(id)}</h1></td>
|
||||
<td className='badge'>{isScheduled && (
|
||||
<Badge
|
||||
color='green'
|
||||
icon='clock'
|
||||
/>
|
||||
)}</td>
|
||||
<td className='badge'>
|
||||
{isScheduled && (
|
||||
<Badge
|
||||
color='green'
|
||||
icon='clock'
|
||||
/>
|
||||
)}
|
||||
</td>
|
||||
<td className='badge together'><ParaLink id={id} /></td>
|
||||
<td className='number media--1500'>
|
||||
{validators && validators.length !== 0 && (
|
||||
@ -174,18 +176,23 @@ function Parachain ({ bestNumber, className = '', id, isScheduled, lastBacked, l
|
||||
</td>
|
||||
<td className='all' />
|
||||
<td className='number'>{blockDelay && <BlockToTime value={blockDelay} />}</td>
|
||||
<td className='number'>
|
||||
<td className='number no-pad-left'>
|
||||
{lastInclusion
|
||||
? <a href={`#/explorer/query/${lastInclusion[0]}`}>{formatNumber(lastInclusion[2])}</a>
|
||||
? <a href={`#/explorer/query/${lastInclusion.blockHash}`}>{formatNumber(lastInclusion.blockNumber)}</a>
|
||||
: paraInfo.watermark && formatNumber(paraInfo.watermark)
|
||||
}
|
||||
</td>
|
||||
<td className='number'>
|
||||
<td className='number no-pad-left'>
|
||||
{lastBacked &&
|
||||
<a href={`#/explorer/query/${lastBacked[0]}`}>{formatNumber(lastBacked[2])}</a>
|
||||
<a href={`#/explorer/query/${lastBacked.blockHash}`}>{formatNumber(lastBacked.blockNumber)}</a>
|
||||
}
|
||||
</td>
|
||||
<td className='number media--900'>{paraBest && <>{formatNumber(paraBest)}</>}</td>
|
||||
<td className='number no-pad-left'>
|
||||
{lastTimeout &&
|
||||
<a href={`#/explorer/query/${lastTimeout.blockHash}`}>{formatNumber(lastTimeout.blockNumber)}</a>
|
||||
}
|
||||
</td>
|
||||
<td className='number media--900 no-pad-left'>{paraBest && <>{formatNumber(paraBest)}</>}</td>
|
||||
<td className='number media--1300'>
|
||||
{paraInfo.updateAt && bestNumber && (
|
||||
<>
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ApiPromise } from '@polkadot/api';
|
||||
import type { SignedBlockExtended } from '@polkadot/api-derive/types';
|
||||
import type { AccountId, CandidateReceipt, Event, ParaId, ParaValidatorIndex } from '@polkadot/types/interfaces';
|
||||
import type { IEvent } from '@polkadot/types/types';
|
||||
import type { ScheduledProposals } from '../types';
|
||||
import type { QueuedAction } from './types';
|
||||
import type { EventMapInfo, QueuedAction } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
||||
@ -21,92 +23,110 @@ interface Props {
|
||||
scheduled?: ScheduledProposals[];
|
||||
}
|
||||
|
||||
type EventMap = Record<string, [string, string, BN]>;
|
||||
type EventMap = Record<string, EventMapInfo>;
|
||||
|
||||
interface LastEvents {
|
||||
lastBacked: EventMap;
|
||||
lastIncluded: EventMap;
|
||||
lastTimeout: EventMap;
|
||||
}
|
||||
|
||||
const EMPTY_EVENTS: LastEvents = { lastBacked: {}, lastIncluded: {}, lastTimeout: {} };
|
||||
|
||||
const optionsMulti = {
|
||||
defaultValue: [null, null] as [AccountId[] | null, ParaValidatorIndex[][] | null]
|
||||
};
|
||||
|
||||
function includeEntry (map: EventMap, event: Event, blockHash: string, blockNumber: BN): void {
|
||||
const { descriptor } = event.data[0] as CandidateReceipt;
|
||||
const { descriptor } = (event as unknown as IEvent<[CandidateReceipt]>).data[0];
|
||||
|
||||
if (descriptor) {
|
||||
map[descriptor.paraId.toString()] = [
|
||||
map[descriptor.paraId.toString()] = {
|
||||
blockHash,
|
||||
descriptor.relayParent.toHex(),
|
||||
blockNumber
|
||||
];
|
||||
blockNumber,
|
||||
relayParent: descriptor.relayParent.toHex()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function extractScheduledIds (scheduled: ScheduledProposals[] = []): Record<string, boolean> {
|
||||
return scheduled.reduce((all: Record<string, boolean>, { scheduledIds }: ScheduledProposals): Record<string, boolean> =>
|
||||
scheduledIds.reduce((all: Record<string, boolean>, id) => ({
|
||||
...all,
|
||||
[id.toString()]: true
|
||||
}), all), {});
|
||||
}
|
||||
|
||||
function mapValidators (ids?: ParaId[], validators?: AccountId[] | null, validatorGroups?: ParaValidatorIndex[][] | null): AccountId[][] {
|
||||
return validators && validatorGroups && ids && ids.length === validatorGroups.length
|
||||
? validatorGroups.map((ids) =>
|
||||
ids
|
||||
.map((id) => validators[id.toNumber()])
|
||||
.filter((a) => a)
|
||||
)
|
||||
: [];
|
||||
}
|
||||
|
||||
function extractEvents (api: ApiPromise, lastBlock: SignedBlockExtended, prev: LastEvents): LastEvents {
|
||||
const backed: EventMap = {};
|
||||
const included: EventMap = {};
|
||||
const timeout: EventMap = {};
|
||||
const blockNumber = lastBlock.block.header.number.unwrap();
|
||||
const blockHash = lastBlock.block.header.hash.toHex();
|
||||
let wasBacked = false;
|
||||
let wasIncluded = false;
|
||||
let wasTimeout = false;
|
||||
|
||||
lastBlock.events.forEach(({ event, phase }) => {
|
||||
if (phase.isApplyExtrinsic) {
|
||||
if (api.events.inclusion.CandidateBacked.is(event)) {
|
||||
includeEntry(backed, event, blockHash, blockNumber);
|
||||
wasBacked = true;
|
||||
} else if (api.events.inclusion.CandidateIncluded.is(event)) {
|
||||
includeEntry(included, event, blockHash, blockNumber);
|
||||
wasIncluded = true;
|
||||
} else if (api.events.inclusion.CandidateTimedOut.is(event)) {
|
||||
includeEntry(timeout, event, blockHash, blockNumber);
|
||||
wasTimeout = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return wasBacked || wasIncluded || wasTimeout
|
||||
? {
|
||||
lastBacked: wasBacked
|
||||
? { ...prev.lastBacked, ...backed }
|
||||
: prev.lastBacked,
|
||||
lastIncluded: wasIncluded
|
||||
? { ...prev.lastIncluded, ...included }
|
||||
: prev.lastIncluded,
|
||||
lastTimeout: wasTimeout
|
||||
? { ...prev.lastTimeout, ...timeout }
|
||||
: prev.lastTimeout
|
||||
}
|
||||
: prev;
|
||||
}
|
||||
|
||||
function extractActions (actionsQueue: QueuedAction[], knownIds?: [ParaId, string][]): Record<string, QueuedAction | undefined> {
|
||||
return actionsQueue && knownIds
|
||||
? knownIds.reduce((all: Record<string, QueuedAction | undefined>, [id, key]) => ({
|
||||
...all,
|
||||
[key]: actionsQueue.find(({ paraIds }) => paraIds.some((p) => p.eq(id)))
|
||||
}), {})
|
||||
: {};
|
||||
}
|
||||
|
||||
function Parachains ({ actionsQueue, ids, scheduled }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const bestNumber = useBestNumber();
|
||||
const lastBlock = useCall<SignedBlockExtended>(api.derive.chain.subscribeNewBlocks);
|
||||
const [{ lastBacked, lastIncluded }, setLastEvents] = useState<LastEvents>({ lastBacked: {}, lastIncluded: {} });
|
||||
const [{ lastBacked, lastIncluded, lastTimeout }, setLastEvents] = useState<LastEvents>(EMPTY_EVENTS);
|
||||
const [validators, validatorGroups] = useCallMulti<[AccountId[] | null, ParaValidatorIndex[][] | null]>([
|
||||
api.query.session?.validators,
|
||||
api.query.paraScheduler?.validatorGroups || api.query.scheduler?.validatorGroups
|
||||
], optionsMulti);
|
||||
|
||||
const scheduledIds = useMemo(
|
||||
() => (scheduled || []).reduce((all: Record<string, boolean>, { scheduledIds }: ScheduledProposals): Record<string, boolean> => {
|
||||
return scheduledIds.reduce((all: Record<string, boolean>, id) => ({ ...all, [id.toString()]: true }), all);
|
||||
}, {}),
|
||||
[scheduled]
|
||||
);
|
||||
|
||||
const validatorMap = useMemo(
|
||||
() => validators && validatorGroups && ids && ids.length === validatorGroups.length
|
||||
? validatorGroups.map((ids) =>
|
||||
ids
|
||||
.map((id) => validators[id.toNumber()])
|
||||
.filter((a) => a)
|
||||
)
|
||||
: [],
|
||||
[ids, validators, validatorGroups]
|
||||
);
|
||||
|
||||
useEffect((): void => {
|
||||
lastBlock && setLastEvents((prev) => {
|
||||
const backed: Record<string, [string, string, BN]> = {};
|
||||
const included: Record<string, [string, string, BN]> = {};
|
||||
const blockNumber = lastBlock.block.header.number.unwrap();
|
||||
const blockHash = lastBlock.block.header.hash.toHex();
|
||||
let wasIncluded = false;
|
||||
let wasBacked = false;
|
||||
|
||||
lastBlock.events.forEach(({ event, phase }) => {
|
||||
if (phase.isApplyExtrinsic) {
|
||||
if (api.events.inclusion.CandidateBacked.is(event)) {
|
||||
includeEntry(backed, event, blockHash, blockNumber);
|
||||
wasBacked = true;
|
||||
} else if (api.events.inclusion.CandidateIncluded.is(event)) {
|
||||
includeEntry(included, event, blockHash, blockNumber);
|
||||
wasIncluded = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return wasBacked || wasIncluded
|
||||
? {
|
||||
lastBacked: wasBacked
|
||||
? { ...prev.lastBacked, ...backed }
|
||||
: prev.lastBacked,
|
||||
lastIncluded: wasIncluded
|
||||
? { ...prev.lastIncluded, ...included }
|
||||
: prev.lastIncluded
|
||||
}
|
||||
: prev;
|
||||
});
|
||||
}, [api, lastBlock]);
|
||||
|
||||
const headerRef = useRef([
|
||||
[t('parachains'), 'start', 3],
|
||||
['', 'media--1500'],
|
||||
@ -114,28 +134,54 @@ function Parachains ({ actionsQueue, ids, scheduled }: Props): React.ReactElemen
|
||||
[t('lifecycle'), 'start media--1100'],
|
||||
[],
|
||||
[t('included'), undefined, 2],
|
||||
[t('backed')],
|
||||
[t('chain best'), 'media--900'],
|
||||
[t('backed'), 'no-pad-left'],
|
||||
[t('timeout'), 'no-pad-left'],
|
||||
[t('chain best'), 'media--900 no-pad-left'],
|
||||
[t('upgrade'), 'media--1300'],
|
||||
[t('ump/dmp/hrmp'), 'media--1200']
|
||||
]);
|
||||
|
||||
const scheduledIds = useMemo(
|
||||
() => extractScheduledIds(scheduled),
|
||||
[scheduled]
|
||||
);
|
||||
|
||||
const validatorMap = useMemo(
|
||||
() => mapValidators(ids, validators, validatorGroups),
|
||||
[ids, validators, validatorGroups]
|
||||
);
|
||||
|
||||
const knownIds = useMemo(
|
||||
() => ids?.map((id): [ParaId, string] => [id, id.toString()]),
|
||||
[ids]
|
||||
);
|
||||
|
||||
const nextActions = useMemo(
|
||||
() => extractActions(actionsQueue, knownIds),
|
||||
[actionsQueue, knownIds]
|
||||
);
|
||||
|
||||
useEffect((): void => {
|
||||
lastBlock && setLastEvents((prev) =>
|
||||
extractEvents(api, lastBlock, prev)
|
||||
);
|
||||
}, [api, lastBlock]);
|
||||
|
||||
return (
|
||||
<Table
|
||||
empty={ids && t<string>('There are no registered parachains')}
|
||||
empty={knownIds && t<string>('There are no registered parachains')}
|
||||
header={headerRef.current}
|
||||
>
|
||||
{ids?.map((id, index): React.ReactNode => (
|
||||
{knownIds?.map(([id, key], index): React.ReactNode => (
|
||||
<Parachain
|
||||
bestNumber={bestNumber}
|
||||
id={id}
|
||||
isScheduled={scheduledIds[id.toString()]}
|
||||
key={id.toString()}
|
||||
lastBacked={lastBacked[id.toString()]}
|
||||
lastInclusion={lastIncluded[id.toString()]}
|
||||
nextAction={actionsQueue.find(({ paraIds }) =>
|
||||
paraIds.some((p) => p.eq(id))
|
||||
)}
|
||||
isScheduled={scheduledIds[key]}
|
||||
key={key}
|
||||
lastBacked={lastBacked[key]}
|
||||
lastInclusion={lastIncluded[key]}
|
||||
lastTimeout={lastTimeout[key]}
|
||||
nextAction={nextActions[key]}
|
||||
sessionValidators={validators}
|
||||
validators={validatorMap[index]}
|
||||
/>
|
||||
|
||||
@ -7,7 +7,6 @@ import React from 'react';
|
||||
|
||||
import SummarySession from '@polkadot/app-explorer/SummarySession';
|
||||
import { CardSummary, SummaryBox } from '@polkadot/react-components';
|
||||
import { useApi } from '@polkadot/react-hooks';
|
||||
import { BestNumber } from '@polkadot/react-query';
|
||||
import { formatNumber, isNumber } from '@polkadot/util';
|
||||
|
||||
@ -22,17 +21,10 @@ interface Props {
|
||||
|
||||
function Summary ({ leasePeriod, parachainCount, proposalCount, upcomingCount }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
|
||||
return (
|
||||
<SummaryBox>
|
||||
<section>
|
||||
<CardSummary label={t<string>('relay')}>
|
||||
{api.query.paras
|
||||
? t<string>('yes')
|
||||
: t<string>('no')
|
||||
}
|
||||
</CardSummary>
|
||||
{isNumber(parachainCount) && (
|
||||
<CardSummary label={t<string>('parachains')}>
|
||||
{formatNumber(parachainCount)}
|
||||
|
||||
@ -20,3 +20,9 @@ export interface QueuedAction {
|
||||
paraIds: ParaId[];
|
||||
sessionIndex: BN;
|
||||
}
|
||||
|
||||
export interface EventMapInfo {
|
||||
blockHash: string;
|
||||
blockNumber: BN;
|
||||
relayParent: string;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user