Shared rows between next-gen active/waiting (#8901)

* Shared rows between next-gene active/waiting

* elected map
This commit is contained in:
Jaco 2023-01-31 15:13:39 +02:00 committed by GitHub
parent cf3a04071b
commit 614d090aa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 199 additions and 124 deletions

View File

@ -0,0 +1,27 @@
// Copyright 2017-2023 @polkadot/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import React from 'react';
interface Props {
children: React.ReactNode;
className?: string;
isExpanded: boolean;
}
function Bottom ({ children, className = '', isExpanded }: Props): React.ReactElement<Props> | null {
if (!isExpanded) {
return null;
}
return (
<tr className={`${className} isExpanded isLast`}>
<td />
<td />
{children}
<td />
</tr>
);
}
export default React.memo(Bottom);

View File

@ -0,0 +1,22 @@
// Copyright 2017-2023 @polkadot/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import React from 'react';
interface Props {
children: React.ReactNode;
className?: string;
isExpanded: boolean;
}
function Middle ({ children, className = '', isExpanded }: Props): React.ReactElement<Props> {
return (
<tr className={`${className} isExpanded ${isExpanded ? '' : 'isLast'} packedTop`}>
<td />
{children}
<td />
</tr>
);
}
export default React.memo(Middle);

View File

@ -0,0 +1,54 @@
// Copyright 2017-2023 @polkadot/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Validator } from '../../../types';
import type { UseHeartbeat } from '../../types';
import React from 'react';
import { AddressSmall, Table } from '@polkadot/react-components';
import Status from '../Status';
interface Props {
children?: React.ReactNode;
className?: string;
heartbeat?: UseHeartbeat;
isExpanded: boolean;
isRelay?: boolean;
toggleExpanded: () => void;
toggleFavorite: (stashId: string) => void;
validator: Validator;
}
function Top ({ children, className = '', heartbeat, isExpanded, isRelay, toggleExpanded, toggleFavorite, validator }: Props): React.ReactElement<Props> {
return (
<tr className={`${className} isExpanded isFirst packedBottom`}>
<Table.Column.Favorite
address={validator.stashId}
isFavorite={validator.isFavorite}
toggle={toggleFavorite}
/>
<td
className='statusInfo'
rowSpan={2}
>
<Status
heartbeat={heartbeat}
isRelay={isRelay}
validator={validator}
/>
</td>
<td className='address relative all'>
<AddressSmall value={validator.stashId} />
{children}
</td>
<Table.Column.Expand
isExpanded={isExpanded}
toggle={toggleExpanded}
/>
</tr>
);
}
export default React.memo(Top);

View File

@ -5,12 +5,14 @@ import type { SessionInfo, Validator } from '../../types';
import React, { useMemo, useRef } from 'react';
import { AddressSmall, Table, Tag } from '@polkadot/react-components';
import { Table, Tag } from '@polkadot/react-components';
import { useToggle } from '@polkadot/react-hooks';
import { FormatBalance } from '@polkadot/react-query';
import { formatNumber } from '@polkadot/util';
import Status from './Status';
import Bottom from './Row/Bottom';
import Middle from './Row/Middle';
import Top from './Row/Top';
import useExposure from './useExposure';
import useHeartbeat from './useHeartbeat';
@ -28,18 +30,11 @@ interface PropsExpanded {
validator: Validator;
}
function ValidatorExpanded ({ className = '' }: PropsExpanded): React.ReactElement<PropsExpanded> {
return (
<tr className={`${className} isExpanded isLast`}>
<td />
<td />
<td />
<td />
</tr>
);
function ActiveExpanded ({ className = '' }: PropsExpanded): React.ReactElement<PropsExpanded> {
return <td className={className} />;
}
function Validator ({ className = '', isRelay, points, sessionInfo, toggleFavorite, validator }: Props): React.ReactElement<Props> {
function Active ({ className = '', isRelay, points, sessionInfo, toggleFavorite, validator }: Props): React.ReactElement<Props> {
const [isExpanded, toggleExpanded] = useToggle();
const pointsRef = useRef<{ counter: number, points: number }>({ counter: 0, points: 0 });
const exposure = useExposure(validator, sessionInfo);
@ -61,39 +56,27 @@ function Validator ({ className = '', isRelay, points, sessionInfo, toggleFavori
return (
<>
<tr className={`${className} isExpanded isFirst packedBottom`}>
<Table.Column.Favorite
address={validator.stashId}
isFavorite={validator.isFavorite}
toggle={toggleFavorite}
/>
<td
className='statusInfo'
rowSpan={2}
>
<Status
heartbeat={heartbeat}
isRelay={isRelay}
validator={validator}
<Top
className={className}
heartbeat={heartbeat}
isExpanded={isExpanded}
isRelay={isRelay}
toggleExpanded={toggleExpanded}
toggleFavorite={toggleFavorite}
validator={validator}
>
{points && (
<Tag
className={`${pointsAnimClass} absolute`}
color='lightgrey'
label={formatNumber(points)}
/>
</td>
<td className='address relative all'>
<AddressSmall value={validator.stashId} />
{points && (
<Tag
className={`${pointsAnimClass} absolute`}
color='lightgrey'
label={formatNumber(points)}
/>
)}
</td>
<Table.Column.Expand
isExpanded={isExpanded}
toggle={toggleExpanded}
/>
</tr>
<tr className={`${className} isExpanded ${isExpanded ? '' : 'isLast'} packedTop`}>
<td />
)}
</Top>
<Middle
className={className}
isExpanded={isExpanded}
>
<Table.Column.Balance
className='relative'
label={
@ -108,13 +91,15 @@ function Validator ({ className = '', isRelay, points, sessionInfo, toggleFavori
value={exposure?.clipped?.total}
withLoading
/>
<td />
</tr>
{isExpanded && (
<ValidatorExpanded validator={validator} />
)}
</Middle>
<Bottom
className={className}
isExpanded={isExpanded}
>
<ActiveExpanded validator={validator} />
</Bottom>
</>
);
}
export default React.memo(Validator);
export default React.memo(Active);

View File

@ -5,10 +5,11 @@ import type { Validator } from '../../types';
import React from 'react';
import { AddressSmall, Table } from '@polkadot/react-components';
import { useToggle } from '@polkadot/react-hooks';
import Status from '../Active/Status';
import Bottom from '../Active/Row/Bottom';
import Middle from '../Active/Row/Middle';
import Top from '../Active/Row/Top';
interface Props {
className?: string;
@ -21,52 +22,36 @@ interface PropsExpanded {
validator: Validator;
}
function ValidatorExpanded ({ className = '' }: PropsExpanded): React.ReactElement<PropsExpanded> {
return (
<tr className={`${className} isExpanded isLast`}>
<td />
<td />
<td />
<td />
</tr>
);
function WaitingExpanded ({ className = '' }: PropsExpanded): React.ReactElement<PropsExpanded> {
return <td className={className} />;
}
function Validator ({ className = '', toggleFavorite, validator }: Props): React.ReactElement<Props> {
function Waiting ({ className = '', toggleFavorite, validator }: Props): React.ReactElement<Props> {
const [isExpanded, toggleExpanded] = useToggle();
return (
<>
<tr className={`${className} isExpanded isFirst packedBottom`}>
<Table.Column.Favorite
address={validator.stashId}
isFavorite={validator.isFavorite}
toggle={toggleFavorite}
/>
<td
className='statusInfo'
rowSpan={2}
>
<Status validator={validator} />
</td>
<td className='address relative all'>
<AddressSmall value={validator.stashId} />
</td>
<Table.Column.Expand
isExpanded={isExpanded}
toggle={toggleExpanded}
/>
</tr>
<tr className={`${className} isExpanded ${isExpanded ? '' : 'isLast'} packedTop`}>
<td colSpan={2} />
<Top
className={className}
isExpanded={isExpanded}
toggleExpanded={toggleExpanded}
toggleFavorite={toggleFavorite}
validator={validator}
/>
<Middle
className={className}
isExpanded={isExpanded}
>
<td />
<td />
</tr>
{isExpanded && (
<ValidatorExpanded validator={validator} />
)}
</Middle>
<Bottom
className={className}
isExpanded={isExpanded}
>
<WaitingExpanded validator={validator} />
</Bottom>
</>
);
}
export default React.memo(Validator);
export default React.memo(Waiting);

View File

@ -30,7 +30,7 @@ function useElectedValidatorsImpl ({ currentEra }: SessionInfo): string[] | unde
const { api } = useApi();
const electedParams = useMemo(
() => cache && currentEra && !cache.currentEra.eq(currentEra)
() => currentEra && (!cache || !cache.currentEra.eq(currentEra))
? [currentEra]
: null,
[currentEra]

View File

@ -13,7 +13,11 @@ import useElectedValidators from './useElectedValidators';
function sort (a: Validator, b: Validator): number {
return a.isFavorite === b.isFavorite
? a.isOwned === b.isOwned
? 0
? a.isElected === b.isElected
? 0
: a.isElected
? -1
: 1
: a.isOwned
? -1
: 1
@ -22,16 +26,14 @@ function sort (a: Validator, b: Validator): number {
: 1;
}
function withElected (validators: Validator[], elected?: string[]): Validator[] {
return elected
? validators.map((v): Validator => {
const isElected = elected.includes(v.stashId);
function withElected (validators: Validator[], elected: string[]): Validator[] {
return validators.map((v): Validator => {
const isElected = elected.includes(v.stashId);
return v.isElected !== isElected
? objectSpread({}, v, { isElected })
: v;
})
: validators;
return v.isElected !== isElected
? objectSpread({}, v, { isElected })
: v;
});
}
function withSort (allAccounts: string[], favorites: string[], validators: Validator[]): Validator[] {
@ -52,7 +54,7 @@ function useTaggedValidatorsImpl (favorites: string[], sessionInfo: SessionInfo,
const elected = useElectedValidators(sessionInfo);
const flagged = useMemo(
() => validators && withElected(validators, elected),
() => elected && validators && withElected(validators, elected),
[elected, validators]
);

View File

@ -13,7 +13,7 @@ import useTaggedValidators from './useTaggedValidators';
interface Cache {
activeEra: BN;
validators: Validator[];
tagged: Validator[];
}
const OPT_VALIDATORS = {
@ -37,18 +37,18 @@ let cache: Cache | undefined;
function useValidatorsActiveImpl (favorites: string[], sessionInfo: SessionInfo): Validator[] | undefined {
const { api } = useApi();
const sessionValidators = useCall(api.query.session.validators, undefined, OPT_VALIDATORS);
const validators = useTaggedValidators(favorites, sessionInfo, sessionValidators);
const tagged = useTaggedValidators(favorites, sessionInfo, sessionValidators);
useEffect((): void => {
if (validators && sessionInfo.activeEra) {
cache = { activeEra: sessionInfo.activeEra, validators };
if (tagged && sessionInfo.activeEra) {
cache = { activeEra: sessionInfo.activeEra, tagged };
}
}, [sessionInfo, validators]);
}, [sessionInfo, tagged]);
return validators || (
return tagged || (
cache &&
sessionInfo.activeEra?.eq(cache.activeEra) &&
cache.validators
cache.tagged
) || undefined;
}

View File

@ -64,15 +64,15 @@ function useValidatorsAllImpl (favorites: string[], sessionInfo: SessionInfo): V
[validators]
);
const result = useTaggedValidators(favorites, sessionInfo, validatorsIndexed);
const tagged = useTaggedValidators(favorites, sessionInfo, validatorsIndexed);
useEffect((): void => {
if (result) {
cache = result;
if (tagged) {
cache = tagged;
}
}, [result]);
}, [tagged, validatorsIndexed]);
return result || cache;
return tagged || cache;
}
export default createNamedHook('useValidatorsAll', useValidatorsAllImpl);

View File

@ -12,11 +12,11 @@ import useValidatorsAll from './useValidatorsAll';
interface Cache {
activeEra: BN;
validators: Validator[];
tagged: Validator[];
}
function excludeValidators (from?: Validator[], exclude?: Validator[]): Validator[] | undefined {
return from && exclude && from.filter(({ stashId }) =>
function excludeValidators (from: Validator[], exclude: Validator[]): Validator[] {
return from.filter(({ stashId }) =>
!exclude.some((v) => v.stashId === stashId)
);
}
@ -28,21 +28,21 @@ function useValidatorsWaitingImpl (favorites: string[], sessionInfo: SessionInfo
// both active and all is already sorted and tagged, so we don't
// need to re-sort the waiting list
const validators = useMemo(
() => excludeValidators(allValidators, activeValidators),
const tagged = useMemo(
() => allValidators && activeValidators && excludeValidators(allValidators, activeValidators),
[activeValidators, allValidators]
);
useEffect((): void => {
if (validators && sessionInfo.activeEra) {
cache = { activeEra: sessionInfo.activeEra, validators };
if (tagged && sessionInfo.activeEra) {
cache = { activeEra: sessionInfo.activeEra, tagged };
}
}, [sessionInfo, validators]);
}, [sessionInfo, tagged]);
return validators || (
return tagged || (
cache &&
sessionInfo.activeEra?.eq(cache.activeEra) &&
cache.validators
cache.tagged
) || undefined;
}