From 614d090aa26657969c11b3e789ea17c9d6945d5a Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 31 Jan 2023 15:13:39 +0200 Subject: [PATCH] Shared rows between next-gen active/waiting (#8901) * Shared rows between next-gene active/waiting * elected map --- .../src/Validators/Active/Row/Bottom.tsx | 27 ++++++ .../src/Validators/Active/Row/Middle.tsx | 22 +++++ .../src/Validators/Active/Row/Top.tsx | 54 ++++++++++++ .../src/Validators/Active/index.tsx | 85 ++++++++----------- .../src/Validators/Waiting/index.tsx | 65 ++++++-------- .../page-staking2/src/useElectedValidators.ts | 2 +- .../page-staking2/src/useTaggedValidators.ts | 24 +++--- .../page-staking2/src/useValidatorsActive.ts | 14 +-- .../page-staking2/src/useValidatorsAll.ts | 10 +-- .../page-staking2/src/useValidatorsWaiting.ts | 20 ++--- 10 files changed, 199 insertions(+), 124 deletions(-) create mode 100644 packages/page-staking2/src/Validators/Active/Row/Bottom.tsx create mode 100644 packages/page-staking2/src/Validators/Active/Row/Middle.tsx create mode 100644 packages/page-staking2/src/Validators/Active/Row/Top.tsx diff --git a/packages/page-staking2/src/Validators/Active/Row/Bottom.tsx b/packages/page-staking2/src/Validators/Active/Row/Bottom.tsx new file mode 100644 index 0000000000..d3aaa23a1a --- /dev/null +++ b/packages/page-staking2/src/Validators/Active/Row/Bottom.tsx @@ -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 | null { + if (!isExpanded) { + return null; + } + + return ( + + + + {children} + + + ); +} + +export default React.memo(Bottom); diff --git a/packages/page-staking2/src/Validators/Active/Row/Middle.tsx b/packages/page-staking2/src/Validators/Active/Row/Middle.tsx new file mode 100644 index 0000000000..f75740308b --- /dev/null +++ b/packages/page-staking2/src/Validators/Active/Row/Middle.tsx @@ -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 { + return ( + + + {children} + + + ); +} + +export default React.memo(Middle); diff --git a/packages/page-staking2/src/Validators/Active/Row/Top.tsx b/packages/page-staking2/src/Validators/Active/Row/Top.tsx new file mode 100644 index 0000000000..8a998baf39 --- /dev/null +++ b/packages/page-staking2/src/Validators/Active/Row/Top.tsx @@ -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 { + return ( + + + + + + + + {children} + + + + ); +} + +export default React.memo(Top); diff --git a/packages/page-staking2/src/Validators/Active/index.tsx b/packages/page-staking2/src/Validators/Active/index.tsx index 934e4023e0..c51dd02b1f 100644 --- a/packages/page-staking2/src/Validators/Active/index.tsx +++ b/packages/page-staking2/src/Validators/Active/index.tsx @@ -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 { - return ( - - - - - - - ); +function ActiveExpanded ({ className = '' }: PropsExpanded): React.ReactElement { + return ; } -function Validator ({ className = '', isRelay, points, sessionInfo, toggleFavorite, validator }: Props): React.ReactElement { +function Active ({ className = '', isRelay, points, sessionInfo, toggleFavorite, validator }: Props): React.ReactElement { 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 ( <> - - - - + {points && ( + - - - - {points && ( - - )} - - - - - + )} + + - - - {isExpanded && ( - - )} + + + + ); } -export default React.memo(Validator); +export default React.memo(Active); diff --git a/packages/page-staking2/src/Validators/Waiting/index.tsx b/packages/page-staking2/src/Validators/Waiting/index.tsx index b1cc336cde..ccbad006eb 100644 --- a/packages/page-staking2/src/Validators/Waiting/index.tsx +++ b/packages/page-staking2/src/Validators/Waiting/index.tsx @@ -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 { - return ( - - - - - - - ); +function WaitingExpanded ({ className = '' }: PropsExpanded): React.ReactElement { + return ; } -function Validator ({ className = '', toggleFavorite, validator }: Props): React.ReactElement { +function Waiting ({ className = '', toggleFavorite, validator }: Props): React.ReactElement { const [isExpanded, toggleExpanded] = useToggle(); return ( <> - - - - - - - - - - - - + + - - - {isExpanded && ( - - )} + + + + ); } -export default React.memo(Validator); +export default React.memo(Waiting); diff --git a/packages/page-staking2/src/useElectedValidators.ts b/packages/page-staking2/src/useElectedValidators.ts index 3f28c33dba..7295ec0499 100644 --- a/packages/page-staking2/src/useElectedValidators.ts +++ b/packages/page-staking2/src/useElectedValidators.ts @@ -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] diff --git a/packages/page-staking2/src/useTaggedValidators.ts b/packages/page-staking2/src/useTaggedValidators.ts index 7a4950ae02..62e1adf06c 100644 --- a/packages/page-staking2/src/useTaggedValidators.ts +++ b/packages/page-staking2/src/useTaggedValidators.ts @@ -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] ); diff --git a/packages/page-staking2/src/useValidatorsActive.ts b/packages/page-staking2/src/useValidatorsActive.ts index eda67a947b..f99739c7ef 100644 --- a/packages/page-staking2/src/useValidatorsActive.ts +++ b/packages/page-staking2/src/useValidatorsActive.ts @@ -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; } diff --git a/packages/page-staking2/src/useValidatorsAll.ts b/packages/page-staking2/src/useValidatorsAll.ts index f2d3415a2f..a09aca3a8e 100644 --- a/packages/page-staking2/src/useValidatorsAll.ts +++ b/packages/page-staking2/src/useValidatorsAll.ts @@ -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); diff --git a/packages/page-staking2/src/useValidatorsWaiting.ts b/packages/page-staking2/src/useValidatorsWaiting.ts index c8a247a43c..5532ab4eee 100644 --- a/packages/page-staking2/src/useValidatorsWaiting.ts +++ b/packages/page-staking2/src/useValidatorsWaiting.ts @@ -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; }