From 4f3f98f97b8c7988dc7cbf209a52d69ba655c57e Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 4 Dec 2020 14:24:28 +0100 Subject: [PATCH] Don't double-up on stashId retrievals (#4157) * Don't double-up on stashId retrievals * Remove (onw-unused) useStashIds hook --- .../page-staking/src/Overview/CurrentList.tsx | 7 +++---- packages/page-staking/src/Overview/Summary.tsx | 10 +++++----- packages/page-staking/src/Overview/index.tsx | 4 +--- packages/page-staking/src/index.tsx | 16 ++-------------- packages/page-staking/src/types.ts | 2 ++ packages/page-staking/src/useSortedTargets.ts | 10 ++++++++-- packages/react-hooks/src/index.ts | 1 - packages/react-hooks/src/useStashIds.ts | 17 ----------------- 8 files changed, 21 insertions(+), 46 deletions(-) delete mode 100644 packages/react-hooks/src/useStashIds.ts diff --git a/packages/page-staking/src/Overview/CurrentList.tsx b/packages/page-staking/src/Overview/CurrentList.tsx index c040872ec4..329cefdc41 100644 --- a/packages/page-staking/src/Overview/CurrentList.tsx +++ b/packages/page-staking/src/Overview/CurrentList.tsx @@ -21,7 +21,6 @@ interface Props { favorites: string[]; hasQueries: boolean; isIntentions?: boolean; - next?: string[]; setNominators?: (nominators: string[]) => void; stakingOverview?: DeriveStakingOverview; targets: SortedTargets; @@ -71,7 +70,7 @@ function getFiltered (stakingOverview: DeriveStakingOverview, favorites: string[ }; } -function CurrentList ({ favorites, hasQueries, isIntentions, next, stakingOverview, targets, toggleFavorite }: Props): React.ReactElement | null { +function CurrentList ({ favorites, hasQueries, isIntentions, stakingOverview, targets, toggleFavorite }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); const { byAuthor, eraPoints } = useContext(isIntentions ? EmptyAuthorsContext : BlockAuthorsContext); @@ -84,8 +83,8 @@ function CurrentList ({ favorites, hasQueries, isIntentions, next, stakingOvervi const isLoading = useLoadingDelay(); const { elected, validators, waiting } = useMemo( - () => stakingOverview ? getFiltered(stakingOverview, favorites, next) : {}, - [favorites, next, stakingOverview] + () => stakingOverview ? getFiltered(stakingOverview, favorites, targets.waitingIds) : {}, + [favorites, stakingOverview, targets] ); const infoMap = useMemo( diff --git a/packages/page-staking/src/Overview/Summary.tsx b/packages/page-staking/src/Overview/Summary.tsx index f973ec6c5c..2795a92ef5 100644 --- a/packages/page-staking/src/Overview/Summary.tsx +++ b/packages/page-staking/src/Overview/Summary.tsx @@ -9,18 +9,18 @@ import SummarySession from '@polkadot/app-explorer/SummarySession'; import { CardSummary, IdentityIcon, SummaryBox } from '@polkadot/react-components'; import { BlockAuthorsContext } from '@polkadot/react-query'; +import type { SortedTargets } from '../types'; import { useTranslation } from '../translate'; interface Props { className?: string; - inflation: number; isVisible: boolean; - next?: string[]; nominators?: string[]; stakingOverview?: DeriveStakingOverview; + targets: SortedTargets; } -function Summary ({ className = '', inflation, isVisible, next, nominators, stakingOverview }: Props): React.ReactElement { +function Summary ({ className = '', isVisible, stakingOverview, targets: { inflation: { inflation }, nominators, waitingIds } }: Props): React.ReactElement { const { t } = useTranslation(); const { lastBlockAuthors, lastBlockNumber } = useContext(BlockAuthorsContext); @@ -32,12 +32,12 @@ function Summary ({ className = '', inflation, isVisible, next, nominators, stak {stakingOverview.validators.length} / {stakingOverview.validatorCount.toString()} )} - {!!next?.length && ( + {!!waitingIds?.length && ( ('waiting')} > - {next.length} + {waitingIds.length} )} {!!nominators?.length && ( diff --git a/packages/page-staking/src/Overview/index.tsx b/packages/page-staking/src/Overview/index.tsx index a5370b8161..c4e8343ec2 100644 --- a/packages/page-staking/src/Overview/index.tsx +++ b/packages/page-staking/src/Overview/index.tsx @@ -13,20 +13,18 @@ interface Props { favorites: string[]; hasQueries: boolean; isIntentions?: boolean; - next?: string[]; stakingOverview?: DeriveStakingOverview; targets: SortedTargets; toggleFavorite: (address: string) => void; } -function Overview ({ className = '', favorites, hasQueries, isIntentions, next, stakingOverview, targets, toggleFavorite }: Props): React.ReactElement { +function Overview ({ className = '', favorites, hasQueries, isIntentions, stakingOverview, targets, toggleFavorite }: Props): React.ReactElement { return (
(allStashes && stakingOverview) - ? allStashes.filter((address) => !stakingOverview.validators.includes(address as any)) - : undefined, - [allStashes, stakingOverview] - ); - const ownValidators = useMemo( () => (ownStashes || []).filter(({ isStashValidating }) => isStashValidating), [ownStashes] @@ -114,11 +106,9 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement @@ -150,7 +140,6 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement accountId.toString()); + const waitingIds = waiting.map(({ accountId }) => accountId.toString()); + const validatorIds = electedIds.concat(waitingIds); + return { avgStaked, inflation, @@ -203,8 +208,9 @@ function extractInfo (api: ApiPromise, allAccounts: string[], electedDerive: Der nominators, totalIssuance, totalStaked, - validatorIds: validators.map(({ accountId }) => accountId.toString()), - validators + validatorIds, + validators, + waitingIds }; } diff --git a/packages/react-hooks/src/index.ts b/packages/react-hooks/src/index.ts index 0aaa05c7df..dd1bd07ee0 100644 --- a/packages/react-hooks/src/index.ts +++ b/packages/react-hooks/src/index.ts @@ -29,7 +29,6 @@ export { useOwnStashes, useOwnStashIds } from './useOwnStashes'; export { useOwnStashInfos } from './useOwnStashInfos'; export { usePassword } from './usePassword'; export { useRegistrars } from './useRegistrars'; -export { useStashIds } from './useStashIds'; export { useSudo } from './useSudo'; export { useToggle } from './useToggle'; export { useVotingStatus } from './useVotingStatus'; diff --git a/packages/react-hooks/src/useStashIds.ts b/packages/react-hooks/src/useStashIds.ts deleted file mode 100644 index 6ccec9288c..0000000000 --- a/packages/react-hooks/src/useStashIds.ts +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2017-2020 @polkadot/react-hooks authors & contributors -// SPDX-License-Identifier: Apache-2.0 - -import type { AccountId } from '@polkadot/types/interfaces'; - -import { useApi } from './useApi'; -import { useCall } from './useCall'; - -const transformStashes = { - transform: (stashes: AccountId[]) => stashes.map((accountId) => accountId.toString()) -}; - -export function useStashIds (): string[] | undefined { - const { api } = useApi(); - - return useCall(api.derive.staking.stashes, undefined, transformStashes); -}