From 1e4f97c1d551290da5e1a9d582310b46e943d368 Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 12 Apr 2022 07:05:53 +0200 Subject: [PATCH] Combine active & waiting staking views (#7354) * Combine active & waiting staking views * Sort by stashes * Filter by own * useRef for options --- .../page-staking/src/Overview/CurrentList.tsx | 48 +++++++----- packages/page-staking/src/Overview/index.tsx | 74 +++++++++++++++++-- packages/page-staking/src/Targets/index.tsx | 13 ++-- packages/page-staking/src/index.tsx | 28 ++++--- packages/page-staking/src/types.ts | 2 + packages/page-staking/src/useNominations.ts | 10 +-- 6 files changed, 126 insertions(+), 49 deletions(-) diff --git a/packages/page-staking/src/Overview/CurrentList.tsx b/packages/page-staking/src/Overview/CurrentList.tsx index b373eeffe1..e7fd350afb 100644 --- a/packages/page-staking/src/Overview/CurrentList.tsx +++ b/packages/page-staking/src/Overview/CurrentList.tsx @@ -5,7 +5,7 @@ import type { DeriveHeartbeats, DeriveStakingOverview } from '@polkadot/api-deri import type { Authors } from '@polkadot/react-query/BlockAuthors'; import type { AccountId } from '@polkadot/types/interfaces'; import type { BN } from '@polkadot/util'; -import type { SortedTargets, ValidatorInfo } from '../types'; +import type { NominatedByMap, SortedTargets, ValidatorInfo } from '../types'; import React, { useContext, useMemo, useRef, useState } from 'react'; @@ -16,14 +16,18 @@ import { BlockAuthorsContext } from '@polkadot/react-query'; import Filtering from '../Filtering'; import Legend from '../Legend'; import { useTranslation } from '../translate'; -import useNominations from '../useNominations'; import Address from './Address'; interface Props { + className?: string; favorites: string[]; hasQueries: boolean; isIntentions?: boolean; + isIntentionsTrigger?: boolean; + isOwn: boolean; minCommission?: BN; + nominatedBy?: NominatedByMap; + ownStashIds?: string[]; paraValidators?: Record; setNominators?: (nominators: string[]) => void; stakingOverview?: DeriveStakingOverview; @@ -40,45 +44,54 @@ interface Filtered { const EmptyAuthorsContext: React.Context = React.createContext({ byAuthor: {}, eraPoints: {}, lastBlockAuthors: [], lastHeaders: [] }); -function filterAccounts (accounts: string[] = [], elected: string[], favorites: string[], without: string[]): AccountExtend[] { +function filterAccounts (isOwn: boolean, accounts: string[] = [], ownStashIds: string[] = [], elected: string[], favorites: string[], without: string[]): AccountExtend[] { return accounts - .filter((accountId) => !without.includes(accountId)) + .filter((accountId) => + !without.includes(accountId) && ( + !isOwn || + ownStashIds.includes(accountId) + ) + ) .map((accountId): AccountExtend => [ accountId, elected.includes(accountId), favorites.includes(accountId) ]) - .sort(([,, isFavA]: AccountExtend, [,, isFavB]: AccountExtend) => - isFavA === isFavB - ? 0 - : (isFavA ? -1 : 1) - ); + .sort(([accA,, isFavA]: AccountExtend, [accB,, isFavB]: AccountExtend): number => { + const isStashA = ownStashIds.includes(accA); + const isStashB = ownStashIds.includes(accB); + + return isFavA === isFavB + ? isStashA === isStashB + ? 0 + : (isStashA ? -1 : 1) + : (isFavA ? -1 : 1); + }); } function accountsToString (accounts: AccountId[]): string[] { return accounts.map((a) => a.toString()); } -function getFiltered (stakingOverview: DeriveStakingOverview, favorites: string[], next?: string[]): Filtered { +function getFiltered (isOwn: boolean, stakingOverview: DeriveStakingOverview, favorites: string[], next?: string[], ownStashIds?: string[]): Filtered { const allElected = accountsToString(stakingOverview.nextElected); const validatorIds = accountsToString(stakingOverview.validators); return { - validators: filterAccounts(validatorIds, allElected, favorites, []), - waiting: filterAccounts(allElected, allElected, favorites, validatorIds).concat( - filterAccounts(next, [], favorites, allElected) + validators: filterAccounts(isOwn, validatorIds, ownStashIds, allElected, favorites, []), + waiting: filterAccounts(isOwn, allElected, ownStashIds, allElected, favorites, validatorIds).concat( + filterAccounts(isOwn, next, ownStashIds, [], favorites, allElected) ) }; } const DEFAULT_PARAS = {}; -function CurrentList ({ favorites, hasQueries, isIntentions, minCommission, paraValidators = DEFAULT_PARAS, stakingOverview, targets, toggleFavorite }: Props): React.ReactElement | null { +function CurrentList ({ className, favorites, hasQueries, isIntentions, isOwn, minCommission, nominatedBy, ownStashIds, paraValidators = DEFAULT_PARAS, stakingOverview, targets, toggleFavorite }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); const { byAuthor, eraPoints } = useContext(isIntentions ? EmptyAuthorsContext : BlockAuthorsContext); const recentlyOnline = useCall(!isIntentions && api.derive.imOnline?.receivedHeartbeats); - const nominatedBy = useNominations(isIntentions); const [nameFilter, setNameFilter] = useState(''); const [toggles, setToggle] = useSavedFlags('staking:overview', { withIdentity: false }); @@ -86,8 +99,8 @@ function CurrentList ({ favorites, hasQueries, isIntentions, minCommission, para const isLoading = useLoadingDelay(); const { validators, waiting } = useMemo( - () => stakingOverview ? getFiltered(stakingOverview, favorites, targets.waitingIds) : {}, - [favorites, stakingOverview, targets] + () => stakingOverview ? getFiltered(isOwn, stakingOverview, favorites, targets.waitingIds, ownStashIds) : {}, + [favorites, isOwn, ownStashIds, stakingOverview, targets] ); const infoMap = useMemo( @@ -122,6 +135,7 @@ function CurrentList ({ favorites, hasQueries, isIntentions, minCommission, para return ( ; stakingOverview?: DeriveStakingOverview; targets: SortedTargets; toggleFavorite: (address: string) => void; toggleLedger?: () => void; + toggleNominatedBy: () => void; } -function Overview ({ className = '', favorites, hasQueries, isIntentions, minCommission, paraValidators, stakingOverview, targets, toggleFavorite, toggleLedger }: Props): React.ReactElement { +function Overview ({ className = '', favorites, hasQueries, minCommission, nominatedBy, ownStashes, paraValidators, stakingOverview, targets, toggleFavorite, toggleLedger, toggleNominatedBy }: Props): React.ReactElement { + const { t } = useTranslation(); + const [intentIndex, _setIntentIndex] = useState(0); + const [typeIndex, setTypeIndex] = useState(1); + + const setIntentIndex = useCallback( + (index: number): void => { + index && toggleNominatedBy(); + _setIntentIndex(index); + }, + [toggleNominatedBy] + ); + + const filterOptions = useRef([ + { text: t('Own validators'), value: 'mine' }, + { text: t('All validators'), value: 'all' } + ]); + + const intentOptions = useRef([ + { text: t('Active'), value: 'active' }, + { text: t('Waiting'), value: 'waiting' } + ]); + + const ownStashIds = useMemo( + () => ownStashes && ownStashes.map(({ stashId }) => stashId), + [ownStashes] + ); + useEffect((): void => { toggleLedger && toggleLedger(); }, [toggleLedger]); + const isOwn = typeIndex === 0; + return (
+ + + + + {intentIndex === 1 && ( + + )}
); } diff --git a/packages/page-staking/src/Targets/index.tsx b/packages/page-staking/src/Targets/index.tsx index 831e572247..f565e4398b 100644 --- a/packages/page-staking/src/Targets/index.tsx +++ b/packages/page-staking/src/Targets/index.tsx @@ -5,7 +5,7 @@ import type { DeriveHasIdentity, DeriveStakingOverview } from '@polkadot/api-der import type { StakerState } from '@polkadot/react-hooks/types'; import type { u32 } from '@polkadot/types-codec'; import type { BN } from '@polkadot/util'; -import type { NominatedBy, SortedTargets, TargetSortBy, ValidatorInfo } from '../types'; +import type { NominatedByMap, SortedTargets, TargetSortBy, ValidatorInfo } from '../types'; import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import styled from 'styled-components'; @@ -20,7 +20,6 @@ import Filtering from '../Filtering'; import Legend from '../Legend'; import { useTranslation } from '../translate'; import useIdentities from '../useIdentities'; -import useNominations from '../useNominations'; import Nominate from './Nominate'; import Summary from './Summary'; import useOwnNominators from './useOwnNominators'; @@ -29,11 +28,13 @@ import Validator from './Validator'; interface Props { className?: string; isInElection: boolean; + nominatedBy?: NominatedByMap; ownStashes?: StakerState[]; stakingOverview?: DeriveStakingOverview; targets: SortedTargets; toggleFavorite: (address: string) => void; toggleLedger: () => void; + toggleNominatedBy: () => void; } interface SavedFlags { @@ -75,7 +76,7 @@ function overlapsDisplay (displays: (string[])[], test: string[]): boolean { ); } -function applyFilter (validators: ValidatorInfo[], medianComm: number, allIdentity: Record, { daysPayout, isBabe, maxPaid, withElected, withGroup, withIdentity, withPayout, withoutComm, withoutOver }: Flags, nominatedBy?: Record): ValidatorInfo[] { +function applyFilter (validators: ValidatorInfo[], medianComm: number, allIdentity: Record, { daysPayout, isBabe, maxPaid, withElected, withGroup, withIdentity, withPayout, withoutComm, withoutOver }: Flags, nominatedBy?: NominatedByMap): ValidatorInfo[] { const displays: (string[])[] = []; const parentIds: string[] = []; @@ -191,13 +192,12 @@ const DEFAULT_NAME = { isQueryFiltered: false, nameFilter: '' }; const DEFAULT_SORT: SortState = { sortBy: 'rankOverall', sortFromMax: true }; -function Targets ({ className = '', isInElection, ownStashes, targets: { avgStaked, inflation: { stakedReturn }, lowStaked, medianComm, minNominated, minNominatorBond, nominators, totalIssuance, totalStaked, validatorIds, validators }, toggleFavorite, toggleLedger }: Props): React.ReactElement { +function Targets ({ className = '', isInElection, nominatedBy, ownStashes, targets: { avgStaked, inflation: { stakedReturn }, lowStaked, medianComm, minNominated, minNominatorBond, nominators, totalIssuance, totalStaked, validatorIds, validators }, toggleFavorite, toggleLedger, toggleNominatedBy }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const allSlashes = useAvailableSlashes(); const daysPayout = useBlocksPerDays(MAX_DAYS); const ownNominators = useOwnNominators(ownStashes); - const nominatedBy = useNominations(true); const allIdentity = useIdentities(validatorIds); const [selected, setSelected] = useState([]); const [{ isQueryFiltered, nameFilter }, setNameFilter] = useState(DEFAULT_NAME); @@ -240,7 +240,8 @@ function Targets ({ className = '', isInElection, ownStashes, targets: { avgStak useEffect((): void => { toggleLedger(); - }, [toggleLedger]); + toggleNominatedBy(); + }, [toggleLedger, toggleNominatedBy]); const maxNominations = useMemo( () => api.consts.staking.maxNominations diff --git a/packages/page-staking/src/index.tsx b/packages/page-staking/src/index.tsx index 80ae353ecd..5bb0d73ae8 100644 --- a/packages/page-staking/src/index.tsx +++ b/packages/page-staking/src/index.tsx @@ -28,6 +28,7 @@ import Query from './Query'; import Slashes from './Slashes'; import Targets from './Targets'; import { useTranslation } from './translate'; +import useNominations from './useNominations'; import useSortedTargets from './useSortedTargets'; const HIDDEN_ACC = ['actions', 'payout']; @@ -52,6 +53,8 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement(api.derive.staking.overview); const [isInElection, minCommission, paraValidators] = useCallMulti<[boolean, BN | undefined, Record]>([ api.query.staking.eraElectionStatus, @@ -83,6 +86,11 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement setLoadNominations(true), + [] + ); + const items = useMemo(() => [ { isRoot: true, @@ -110,10 +118,6 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement('Bags') }, - { - name: 'waiting', - text: t('Waiting') - }, { count: slashes.reduce((count, [, unapplied]) => count + unapplied.length, 0), name: 'slashes', @@ -168,22 +172,13 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement - - - @@ -202,10 +197,13 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement ); diff --git a/packages/page-staking/src/types.ts b/packages/page-staking/src/types.ts index 6b0a6bc2b1..0c5a76cb31 100644 --- a/packages/page-staking/src/types.ts +++ b/packages/page-staking/src/types.ts @@ -17,6 +17,8 @@ export interface NominatedBy { submittedIn: EraIndex; } +export type NominatedByMap = Record; + export interface Slash { accountId: AccountId; amount: Balance; diff --git a/packages/page-staking/src/useNominations.ts b/packages/page-staking/src/useNominations.ts index b0f8a0a241..87862a46b2 100644 --- a/packages/page-staking/src/useNominations.ts +++ b/packages/page-staking/src/useNominations.ts @@ -3,16 +3,14 @@ import type { Option, StorageKey } from '@polkadot/types'; import type { Nominations } from '@polkadot/types/interfaces'; -import type { NominatedBy } from './types'; +import type { NominatedByMap } from './types'; import { useMemo } from 'react'; import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks'; -type Result = Record; - -function extractNominators (nominations: [StorageKey, Option][]): Result { - return nominations.reduce((mapped: Result, [key, optNoms]) => { +function extractNominators (nominations: [StorageKey, Option][]): NominatedByMap { + return nominations.reduce((mapped: NominatedByMap, [key, optNoms]) => { if (optNoms.isSome && key.args.length) { const nominatorId = key.args[0].toString(); const { submittedIn, targets } = optNoms.unwrap(); @@ -33,7 +31,7 @@ function extractNominators (nominations: [StorageKey, Option][]): R }, {}); } -function useNominationsImpl (isActive = true): Result | undefined { +function useNominationsImpl (isActive = true): NominatedByMap | undefined { const { api } = useApi(); const nominators = useCall<[StorageKey, Option][]>(isActive && api.query.staking.nominators.entries);