diff --git a/packages/app-staking/src/Overview/Address.tsx b/packages/app-staking/src/Overview/Address.tsx index b31142607b..159df3632e 100644 --- a/packages/app-staking/src/Overview/Address.tsx +++ b/packages/app-staking/src/Overview/Address.tsx @@ -53,13 +53,14 @@ interface OnlineState { const WITH_VALIDATOR_PREFS = { validatorPayment: true }; -function Address ({ address, authorsMap, className, currentElected, defaultName, filter, lastAuthors, points, recentlyOnline, stakingInfo, t, withNominations }: Props): React.ReactElement | null { +function Address ({ address, authorsMap, className, currentElected, defaultName, filter, lastAuthors, points, recentlyOnline, stakingInfo, t, withNominations = true }: Props): React.ReactElement | null { const { isSubstrateV2 } = useContext(ApiContext); const [extraInfo, setExtraInfo] = useState<[React.ReactNode, React.ReactNode][] | undefined>(); const [{ hasOfflineWarnings, onlineStatus }, setOnlineStatus] = useState({ hasOfflineWarnings: false, onlineStatus: {} }); + const [isNominationsOpen, setIsNominationsOpen] = useState(false); const [{ balanceOpts, controllerId, hasNominators, isNominatorMe, isSelected, nominators, sessionId, stashId }, setStakingState] = useState({ balanceOpts: { bonded: true }, hasNominators: false, @@ -90,11 +91,9 @@ function Address ({ address, authorsMap, className, currentElected, defaultName, const _stashId = stashId && stashId.toString(); setStakingState({ - balanceOpts: { - bonded: stakers && !stakers.own.isEmpty - ? [stakers.own.unwrap(), stakers.total.unwrap().sub(stakers.own.unwrap())] - : true - }, + balanceOpts: stakers && !stakers.own.isEmpty + ? { bonded: [stakers.own.unwrap(), stakers.total.unwrap().sub(stakers.own.unwrap())] } + : { bonded: true }, controllerId: controllerId && controllerId.toString(), hasNominators: nominators.length !== 0, isNominatorMe: nominators.some(([who]): boolean => @@ -147,9 +146,15 @@ function Address ({ address, authorsMap, className, currentElected, defaultName, ); } + const _toggleNominations = (event: React.SyntheticEvent): void => { + event.preventDefault(); + event.stopPropagation(); + + setIsNominationsOpen(!isNominationsOpen); + }; + const lastBlockNumber = authorsMap[stashId]; const isAuthor = lastAuthors && lastAuthors.includes(stashId); - // isDisabled={!!points && points.isEmpty} return ( } + stakingInfo={stakingInfo} value={stashId} withBalance={balanceOpts} withValidatorPrefs={WITH_VALIDATOR_PREFS} > {withNominations && hasNominators && ( -
- +
+ {t('Nominators ({{count}})', { replace: { count: nominators.length } })} - {nominators.map(([who, bonded]): React.ReactNode => + {isNominationsOpen && nominators.map(([who, bonded]): React.ReactNode => (
)); } @@ -49,6 +49,7 @@ function filterAccounts (list: string[] = [], without: string[]): string[] { } function CurrentList (props: Props): React.ReactElement { + const { isSubstrateV2 } = useContext(ApiContext); const [filter, setFilter] = useState('all'); const [{ electedFiltered, nextFiltered }, setFiltered] = useState<{ electedFiltered: string[]; nextFiltered: string[] }>({ electedFiltered: [], nextFiltered: [] }); const { currentElected, currentValidators, next, t } = props; @@ -56,7 +57,7 @@ function CurrentList (props: Props): React.ReactElement { useEffect((): void => { if (currentElected && currentValidators) { setFiltered({ - electedFiltered: filterAccounts(currentElected, currentValidators), + electedFiltered: isSubstrateV2 ? filterAccounts(currentElected, currentValidators) : [], nextFiltered: filterAccounts(next, currentElected) }); } @@ -85,7 +86,7 @@ function CurrentList (props: Props): React.ReactElement { emptyText={t('No addresses found')} headerText={t('validators')} > - {renderColumn(currentValidators, t('validator'), true, true, filter, props)} + {renderColumn(currentValidators, t('validator'), true, filter, props)} { > {(electedFiltered.length !== 0 || nextFiltered.length !== 0) && ( <> - {renderColumn(electedFiltered, t('intention'), true, false, filter, props)} - {renderColumn(nextFiltered, t('intention'), false, false, filter, props)} + {renderColumn(electedFiltered, t('intention'), false, filter, props)} + {renderColumn(nextFiltered, t('intention'), false, filter, props)} )} diff --git a/packages/react-api/src/with/call.tsx b/packages/react-api/src/with/call.tsx index c2e20f339d..f1a2f4f836 100644 --- a/packages/react-api/src/with/call.tsx +++ b/packages/react-api/src/with/call.tsx @@ -26,24 +26,25 @@ const NOOP = (): void => { // ignore }; +const NO_SKIP = (): boolean => false; + // a mapping of actual error messages that has already been shown const errorred: Record = {}; -export default function withCall

( - endpoint: string, - { - at, - atProp, - callOnResult, - fallbacks, - isMulti = false, - params = [], - paramName, - paramPick, - paramValid = false, - propName, - transform = echoTransform - }: Options = {}): (Inner: React.ComponentType) => React.ComponentType { +export default function withCall

(endpoint: string, { + at, + atProp, + callOnResult, + fallbacks, + isMulti = false, + params = [], + paramName, + paramPick, + paramValid = false, + propName, + skipIf = NO_SKIP, + transform = echoTransform +}: Options = {}): (Inner: React.ComponentType) => React.ComponentType { return (Inner: React.ComponentType): React.ComponentType> => { class WithPromise extends React.Component { public state: State = { @@ -199,7 +200,7 @@ export default function withCall

( } private async subscribe ([isValid, newParams]: [boolean, any[]]): Promise { - if (!isValid) { + if (!isValid || skipIf(this.props)) { return; } @@ -284,10 +285,13 @@ export default function withCall

( const _props = { ...this.props, callUpdated, - callUpdatedAt, - [propName || this.propName]: callResult + callUpdatedAt }; + if (!isUndefined(callResult)) { + (_props as any)[propName || this.propName] = callResult; + } + return ( ); diff --git a/packages/react-api/src/with/types.ts b/packages/react-api/src/with/types.ts index 61127489e3..c0a8fc6e46 100644 --- a/packages/react-api/src/with/types.ts +++ b/packages/react-api/src/with/types.ts @@ -24,6 +24,7 @@ export interface Options { paramPick?: (props: any) => any; paramValid?: boolean; propName?: string; + skipIf?: (props: any) => boolean; transform?: Transform; } diff --git a/packages/react-components/src/AddressInfo.tsx b/packages/react-components/src/AddressInfo.tsx index adaf88d87e..89a21cc13d 100644 --- a/packages/react-components/src/AddressInfo.tsx +++ b/packages/react-components/src/AddressInfo.tsx @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/camelcase */ // Copyright 2017-2019 @polkadot/react-components authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. @@ -10,7 +9,7 @@ import { BareProps, I18nProps } from './types'; import BN from 'bn.js'; import React from 'react'; import styled from 'styled-components'; -import { formatBalance, formatNumber } from '@polkadot/util'; +import { formatBalance, formatNumber, isObject } from '@polkadot/util'; import { Icon, Tooltip, TxButton } from '@polkadot/react-components'; import { withCalls, withMulti } from '@polkadot/react-api'; @@ -42,10 +41,10 @@ export interface ValidatorPrefsType { interface Props extends BareProps, I18nProps { address: string; - balances_all?: DerivedBalances; + balancesAll?: DerivedBalances; children?: React.ReactNode; extraInfo?: [string, string][]; - staking_info?: DerivedStaking; + stakingInfo?: DerivedStaking; withBalance?: boolean | BalanceActiveType; withExtended?: boolean | CryptoActiveType; withHexSessionId: string | null; @@ -71,8 +70,42 @@ const DEFAULT_PREFS = { validatorPayment: true }; +// skip balances retrieval of none of this matches +function skipBalancesIf ({ withBalance = true, withExtended = false }: Props): boolean { + if (withBalance === true || withExtended === true) { + return false; + } else if (isObject(withBalance)) { + // these all pull from the all balances + if (withBalance.available || withBalance.locked || withBalance.reserved || withBalance.total) { + return false; + } + } else if (isObject(withExtended)) { + if (withExtended.nonce) { + return false; + } + } + + return true; +} + +function skipStakingIf ({ stakingInfo, withBalance = true, withRewardDestination = false, withValidatorPrefs = false }: Props): boolean { + if (stakingInfo) { + return true; + } else if (withBalance === true || withValidatorPrefs || withRewardDestination) { + return false; + } else if (isObject(withBalance)) { + if (withBalance.unlocking || withBalance.redeemable) { + return false; + } else if (withBalance.bonded) { + return Array.isArray(withBalance.bonded); + } + } + + return true; +} + // calculates the bonded, first being the own, the second being nominated -function calcBonded (staking_info?: DerivedStaking, bonded?: boolean | BN[]): [BN, BN[]] { +function calcBonded (stakingInfo?: DerivedStaking, bonded?: boolean | BN[]): [BN, BN[]] { let other: BN[] = []; let own = new BN(0); @@ -82,28 +115,28 @@ function calcBonded (staking_info?: DerivedStaking, bonded?: boolean | BN[]): [B .filter((value): boolean => value.gtn(0)); own = bonded[0]; - } else if (staking_info && staking_info.stakingLedger && staking_info.accountId.eq(staking_info.stashId)) { - own = staking_info.stakingLedger.active.unwrap(); + } else if (stakingInfo && stakingInfo.stakingLedger && stakingInfo.accountId.eq(stakingInfo.stashId)) { + own = stakingInfo.stakingLedger.active.unwrap(); } return [own, other]; } -function renderExtended ({ balances_all, t, address, withExtended }: Props): React.ReactNode { +function renderExtended ({ balancesAll, t, address, withExtended }: Props): React.ReactNode { const extendedDisplay = withExtended === true ? DEFAULT_EXTENDED : withExtended || undefined; - if (!extendedDisplay || !balances_all) { + if (!extendedDisplay) { return null; } return (

- {extendedDisplay.nonce && ( + {balancesAll && extendedDisplay.nonce && ( <>