From ab00ed377a6bf745c38eac72f67b62f043209577 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 26 Nov 2020 13:50:21 +0100 Subject: [PATCH] Validator chill refresh (#4082) --- packages/page-staking/src/Actions/index.tsx | 10 +++++++++- packages/react-hooks/src/useOwnStashInfos.ts | 12 +++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/page-staking/src/Actions/index.tsx b/packages/page-staking/src/Actions/index.tsx index 236ad39568..ad63137c15 100644 --- a/packages/page-staking/src/Actions/index.tsx +++ b/packages/page-staking/src/Actions/index.tsx @@ -32,8 +32,16 @@ interface State { foundStashes?: StakerState[]; } +function assignValue ({ isStashNominating, isStashValidating }: StakerState): number { + return isStashValidating + ? 1 + : isStashNominating + ? 5 + : 99; +} + function sortStashes (a: StakerState, b: StakerState): number { - return (a.isStashValidating ? 1 : (a.isStashNominating ? 5 : 99)) - (b.isStashValidating ? 1 : (b.isStashNominating ? 5 : 99)); + return assignValue(a) - assignValue(b); } function extractState (ownStashes?: StakerState[]): State { diff --git a/packages/react-hooks/src/useOwnStashInfos.ts b/packages/react-hooks/src/useOwnStashInfos.ts index 70bd19c6bc..c3cfbd6c44 100644 --- a/packages/react-hooks/src/useOwnStashInfos.ts +++ b/packages/react-hooks/src/useOwnStashInfos.ts @@ -13,7 +13,6 @@ import { useAccounts } from './useAccounts'; import { useApi } from './useApi'; import { useIsMountedRef } from './useIsMountedRef'; import { useOwnStashes } from './useOwnStashes'; -import { useStashIds } from './useStashIds'; type ValidatorInfo = ITuple<[ValidatorPrefs, Codec]> | ValidatorPrefs; type Queried = Record; @@ -24,9 +23,9 @@ function toIdString (id?: AccountId | null): string | null { : null; } -function getStakerState (stashId: string, allAccounts: string[], allStashes: string[] | undefined, [isOwnStash, { controllerId: _controllerId, exposure, nextSessionIds, nominators, rewardDestination, sessionIds, stakingLedger, validatorPrefs }, validateInfo]: [boolean, DeriveStakingAccount, ValidatorInfo]): StakerState { +function getStakerState (stashId: string, allAccounts: string[], [isOwnStash, { controllerId: _controllerId, exposure, nextSessionIds, nominators, rewardDestination, sessionIds, stakingLedger, validatorPrefs }, validateInfo]: [boolean, DeriveStakingAccount, ValidatorInfo]): StakerState { const isStashNominating = !!(nominators?.length); - const isStashValidating = !(Array.isArray(validateInfo) ? validateInfo[1].isEmpty : validateInfo.isEmpty) || !!allStashes?.includes(stashId); + const isStashValidating = !(Array.isArray(validateInfo) ? validateInfo[1].isEmpty : validateInfo.isEmpty); const nextConcat = u8aConcat(...nextSessionIds.map((id): Uint8Array => id.toU8a())); const currConcat = u8aConcat(...sessionIds.map((id): Uint8Array => id.toU8a())); const controllerId = toIdString(_controllerId); @@ -60,7 +59,6 @@ export function useOwnStashInfos (): StakerState[] | undefined { const { allAccounts } = useAccounts(); const mountedRef = useIsMountedRef(); const ownStashes = useOwnStashes(); - const allStashes = useStashIds(); const [queried, setQueried] = useState(); useEffect((): () => void => { @@ -95,11 +93,11 @@ export function useOwnStashInfos (): StakerState[] | undefined { }, [api, mountedRef, ownStashes]); return useMemo( - () => allStashes && ownStashes && queried && ownStashes.length === Object.keys(queried).length + () => ownStashes && queried && ownStashes.length === Object.keys(queried).length ? ownStashes .filter(([stashId]) => queried[stashId]) - .map(([stashId]) => getStakerState(stashId, allAccounts, allStashes, queried[stashId])) + .map(([stashId]) => getStakerState(stashId, allAccounts, queried[stashId])) : undefined, - [allAccounts, allStashes, ownStashes, queried] + [allAccounts, ownStashes, queried] ); }