From 51e71dc3acd52f931e18410c94a3e6698a97b594 Mon Sep 17 00:00:00 2001 From: kwingram25 Date: Wed, 10 Apr 2019 01:55:14 -0700 Subject: [PATCH] RecentlyOffline added to Account Actions (#929) * RecentlyOffline added to Account Actions * travis-ci * relocate recentlyOffline state * comma * comma --- packages/app-staking/src/Account/index.tsx | 24 +++-- packages/app-staking/src/Overview/Address.tsx | 9 +- .../app-staking/src/Overview/CurrentList.tsx | 36 +------- packages/app-staking/src/Overview/index.tsx | 3 +- packages/app-staking/src/RecentlyOffline.tsx | 60 ------------- packages/app-staking/src/StakeList.tsx | 3 +- packages/app-staking/src/index.css | 47 +--------- packages/app-staking/src/index.tsx | 32 +++++-- packages/app-staking/src/types.ts | 1 + packages/ui-app/src/AddressMini.tsx | 20 +++++ packages/ui-app/src/RecentlyOffline.tsx | 90 +++++++++++++++++++ packages/ui-app/src/index.tsx | 1 + packages/ui-app/src/styles/components.css | 52 +++++++++++ 13 files changed, 222 insertions(+), 156 deletions(-) delete mode 100644 packages/app-staking/src/RecentlyOffline.tsx create mode 100644 packages/ui-app/src/RecentlyOffline.tsx diff --git a/packages/app-staking/src/Account/index.tsx b/packages/app-staking/src/Account/index.tsx index 19f8553c4e..8d915ca415 100644 --- a/packages/app-staking/src/Account/index.tsx +++ b/packages/app-staking/src/Account/index.tsx @@ -6,7 +6,7 @@ import { DerivedBalancesMap } from '@polkadot/api-derive/types'; import { I18nProps } from '@polkadot/ui-app/types'; import { ApiProps } from '@polkadot/ui-api/types'; import { KeyringSectionOption } from '@polkadot/ui-keyring/options/types'; -import { Nominators } from '../types'; +import { Nominators, RecentlyOfflineMap } from '../types'; import React from 'react'; import { AccountId, Balance, Exposure, Option, StakingLedger, ValidatorPrefs } from '@polkadot/types'; @@ -28,6 +28,7 @@ type Props = ApiProps & I18nProps & { isValidator: boolean, name: string, nominators: Nominators, + recentlyOffline: RecentlyOfflineMap, session_nextKeyFor?: Option, staking_bonded?: Option, staking_ledger?: Option, @@ -193,7 +194,8 @@ class Account extends React.PureComponent { } private renderNominee () { - const { t } = this.props; + const { recentlyOffline, t } = this.props; + const nominees = this.getNominees(); if (!nominees || !nominees.length) { @@ -208,6 +210,7 @@ class Account extends React.PureComponent { @@ -242,7 +245,7 @@ class Account extends React.PureComponent { } private renderBondedId () { - const { t } = this.props; + const { recentlyOffline, t } = this.props; const { bondedId } = this.state; if (!bondedId) { @@ -252,7 +255,10 @@ class Account extends React.PureComponent { return (
- +
); } @@ -274,7 +280,7 @@ class Account extends React.PureComponent { } private renderStashId () { - const { t } = this.props; + const { recentlyOffline, t } = this.props; const { stashId } = this.state; if (!stashId) { @@ -284,7 +290,12 @@ class Account extends React.PureComponent { return (
- +
); } @@ -421,6 +432,7 @@ class Account extends React.PureComponent { export default translate( withCalls( + 'query.staking.recentlyOffline', ['query.session.nextKeyFor', { paramName: 'accountId' }], ['query.staking.bonded', { paramName: 'accountId' }], ['query.staking.ledger', { paramName: 'accountId' }], diff --git a/packages/app-staking/src/Overview/Address.tsx b/packages/app-staking/src/Overview/Address.tsx index b35025a1c7..a7c47a179a 100644 --- a/packages/app-staking/src/Overview/Address.tsx +++ b/packages/app-staking/src/Overview/Address.tsx @@ -9,12 +9,10 @@ import { Nominators, RecentlyOfflineMap } from '../types'; import React from 'react'; import { AccountId, Balance, Option, StakingLedger } from '@polkadot/types'; import { withCalls, withMulti } from '@polkadot/ui-api/with'; -import { AddressMini, AddressRow } from '@polkadot/ui-app'; +import { AddressMini, AddressRow, RecentlyOffline } from '@polkadot/ui-app'; import keyring from '@polkadot/ui-keyring'; import { formatBalance } from '@polkadot/util'; -import RecentlyOffline from '../RecentlyOffline'; - import translate from '../translate'; type Props = I18nProps & { @@ -198,7 +196,10 @@ class Address extends React.PureComponent { const offline = recentlyOffline[stashId]; return ( - + ); } } diff --git a/packages/app-staking/src/Overview/CurrentList.tsx b/packages/app-staking/src/Overview/CurrentList.tsx index 089bee2116..1942af3715 100644 --- a/packages/app-staking/src/Overview/CurrentList.tsx +++ b/packages/app-staking/src/Overview/CurrentList.tsx @@ -4,7 +4,7 @@ import { DerivedBalancesMap } from '@polkadot/api-derive/types'; import { I18nProps } from '@polkadot/ui-app/types'; -import { Nominators, RecentlyOffline, RecentlyOfflineMap } from '../types'; +import { Nominators, RecentlyOfflineMap } from '../types'; import React from 'react'; import { AccountId, Balance, HeaderExtended } from '@polkadot/types'; @@ -21,36 +21,10 @@ type Props = I18nProps & { current: Array, next: Array, nominators: Nominators, - staking_recentlyOffline?: RecentlyOffline -}; - -type State = { recentlyOffline: RecentlyOfflineMap }; -class CurrentList extends React.PureComponent { - state: State = { recentlyOffline: {} }; - - static getDerivedStateFromProps ({ staking_recentlyOffline = [] }: Props): State { - return { - recentlyOffline: staking_recentlyOffline.reduce( - (result, [accountId, blockNumber, count]) => { - const account = accountId.toString(); - - if (!result[account]) { - result[account] = []; - } - - result[account].push({ - blockNumber, - count - }); - - return result; - }, {} as RecentlyOfflineMap) - }; - } - +class CurrentList extends React.PureComponent { render () { return (
@@ -93,8 +67,7 @@ class CurrentList extends React.PureComponent { } private renderColumn (addresses: Array, defaultName: string) { - const { balances, balanceArray, chain_subscribeNewHead, nominators, t } = this.props; - const { recentlyOffline } = this.state; + const { balances, balanceArray, chain_subscribeNewHead, nominators, recentlyOffline, t } = this.props; if (addresses.length === 0) { return ( @@ -134,7 +107,6 @@ export default withMulti( CurrentList, translate, withCalls( - 'derive.chain.subscribeNewHead', - 'query.staking.recentlyOffline' + 'derive.chain.subscribeNewHead' ) ); diff --git a/packages/app-staking/src/Overview/index.tsx b/packages/app-staking/src/Overview/index.tsx index 30a88d2b4e..29b8bc5c92 100644 --- a/packages/app-staking/src/Overview/index.tsx +++ b/packages/app-staking/src/Overview/index.tsx @@ -19,7 +19,7 @@ const ZERO = new Balance(0); export default class Overview extends React.PureComponent { render () { - const { balances, balanceArray, intentions, nominators, validators } = this.props; + const { balances, balanceArray, intentions, nominators, recentlyOffline, validators } = this.props; const intentionsSorted = this.sortByBalance( intentions.filter((address) => !validators.includes(address) @@ -40,6 +40,7 @@ export default class Overview extends React.PureComponent { current={validatorsSorted} next={intentionsSorted} nominators={nominators} + recentlyOffline={recentlyOffline} />
); diff --git a/packages/app-staking/src/RecentlyOffline.tsx b/packages/app-staking/src/RecentlyOffline.tsx deleted file mode 100644 index 11163c29b4..0000000000 --- a/packages/app-staking/src/RecentlyOffline.tsx +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2017-2019 @polkadot/app-staking authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. -import { OfflineStatus } from './types'; -import { I18nProps } from '@polkadot/ui-app/types'; - -import BN from 'bn.js'; -import React from 'react'; -import { formatNumber } from '@polkadot/util'; - -import translate from './translate'; - -type Props = I18nProps & { - offline: Array -}; - -type State = { - isOpen: boolean -}; - -class RecentlyOffline extends React.PureComponent { - state: State = { - isOpen: false - }; - - render () { - const { offline, t } = this.props; - const { isOpen } = this.state; - - const count = offline.reduce((total, { count }) => total.add(count), new BN(0)); - const blockNumbers = offline.map(({ blockNumber }) => `#${formatNumber(blockNumber)}`); - - return ( -
-
- {count.toString()} -
-
- {t('Reported offline {{count}} times, last at {{blockNumber}}', { - replace: { - count, - blockNumber: blockNumbers[blockNumbers.length - 1] - } - })} -
-
- ); - } - - private toggleOpen = (): void => { - this.setState(({ isOpen }) => ({ - isOpen: !isOpen - })); - } -} - -export default translate(RecentlyOffline); diff --git a/packages/app-staking/src/StakeList.tsx b/packages/app-staking/src/StakeList.tsx index 94899768ff..f0e1d21549 100644 --- a/packages/app-staking/src/StakeList.tsx +++ b/packages/app-staking/src/StakeList.tsx @@ -17,7 +17,7 @@ type Props = I18nProps & ComponentProps; class StakeList extends React.PureComponent { render () { - const { balances, balanceArray, intentions, nominators, validators } = this.props; + const { balances, balanceArray, intentions, nominators, recentlyOffline, validators } = this.props; const accounts = keyring.getAccounts(); return ( @@ -36,6 +36,7 @@ class StakeList extends React.PureComponent { key={address} name={name} nominators={nominators} + recentlyOffline={recentlyOffline} targets={this.getTargetOptions()} validators={validators} /> diff --git a/packages/app-staking/src/index.css b/packages/app-staking/src/index.css index 44f7cd39e9..ed32f2941f 100644 --- a/packages/app-staking/src/index.css +++ b/packages/app-staking/src/index.css @@ -129,49 +129,4 @@ .staking--label { margin: 0 0 -0.5rem 0; } -} - -.staking--Account-recentlyOffline { - position: absolute; - bottom: 0.75rem; - font-size: 12px; - cursor: help; - display: flex; - justify-content: center; - right: 0.75rem; - width: 22px; - height: 22px; - padding: 0; - background: red; - color: #eee; - border-radius: 16px; - box-shadow: 0 3px 3px rgba(0, 0, 0, 0.2); - transition: all ease .2s; - - & > * { - line-height: 22px; - overflow: hidden; - transition: all ease 0.25; - } - - .badge { - font-weight: bold; - width: auto; - } - - .detail { - width: 0; - } - - &.expand { - width: 300px; - - .badge { - width: 0; - } - - .detail { - width: auto; - } - } -} +} \ No newline at end of file diff --git a/packages/app-staking/src/index.tsx b/packages/app-staking/src/index.tsx index 28cee6fbcc..336d51cfbf 100644 --- a/packages/app-staking/src/index.tsx +++ b/packages/app-staking/src/index.tsx @@ -6,7 +6,7 @@ import { DerivedBalancesMap } from '@polkadot/api-derive/types'; import { AppProps, I18nProps } from '@polkadot/ui-app/types'; import { ApiProps } from '@polkadot/ui-api/types'; import { SubjectInfo } from '@polkadot/ui-keyring/observable/types'; -import { ComponentProps, Nominators } from './types'; +import { ComponentProps, Nominators, RecentlyOffline, RecentlyOfflineMap } from './types'; import React from 'react'; import { Route, Switch } from 'react-router'; @@ -28,12 +28,14 @@ type Props = AppProps & ApiProps & I18nProps & { balances?: DerivedBalancesMap, session_validators?: Array, staking_controllers?: [Array, Array>], - staking_nominators?: [Array, Array>] + staking_nominators?: [Array, Array>], + staking_recentlyOffline?: RecentlyOffline }; type State = { intentions: Array, nominators: Nominators, + recentlyOffline: RecentlyOfflineMap, tabs: Array, targets: Array, validators: Array @@ -50,6 +52,7 @@ class App extends React.PureComponent { this.state = { intentions: [], nominators: {}, + recentlyOffline: {}, tabs: [ { name: 'overview', @@ -65,7 +68,7 @@ class App extends React.PureComponent { }; } - static getDerivedStateFromProps ({ staking_controllers = [[], []], session_validators = [], staking_nominators = [[], []] }: Props): State { + static getDerivedStateFromProps ({ staking_controllers = [[], []], session_validators = [], staking_nominators = [[], []], staking_recentlyOffline = [] }: Props): State { return { intentions: staking_controllers[1].filter((optId) => optId.isSome).map((accountId) => accountId.unwrap().toString() @@ -80,7 +83,22 @@ class App extends React.PureComponent { targets: staking_controllers[0].map((accountId) => accountId.toString()), validators: session_validators.map((authorityId) => authorityId.toString() - ) + ), + recentlyOffline: staking_recentlyOffline.reduce( + (result, [accountId, blockNumber, count]) => { + const account = accountId.toString(); + + if (!result[account]) { + result[account] = []; + } + + result[account].push({ + blockNumber, + count + }); + + return result; + }, {} as RecentlyOfflineMap) } as State; } @@ -111,7 +129,7 @@ class App extends React.PureComponent { private renderComponent (Component: React.ComponentType) { return (): React.ReactNode => { - const { intentions, nominators, targets, validators } = this.state; + const { intentions, nominators, recentlyOffline, targets, validators } = this.state; const { balances = {} } = this.props; return ( @@ -120,6 +138,7 @@ class App extends React.PureComponent { balanceArray={this.balanceArray} intentions={intentions} nominators={nominators} + recentlyOffline={recentlyOffline} targets={targets} validators={validators} /> @@ -151,7 +170,8 @@ export default withMulti( withCalls( 'derive.staking.controllers', 'query.session.validators', - 'query.staking.nominators' + 'query.staking.nominators', + 'query.staking.recentlyOffline' ), withObservable(accountObservable.subject, { propName: 'allAccounts' }) ); diff --git a/packages/app-staking/src/types.ts b/packages/app-staking/src/types.ts index e0d6de2cba..121f741461 100644 --- a/packages/app-staking/src/types.ts +++ b/packages/app-staking/src/types.ts @@ -16,6 +16,7 @@ export type ComponentProps = { balanceArray: (_address: AccountId | string) => Array | undefined, intentions: Array, nominators: Nominators, + recentlyOffline: RecentlyOfflineMap, targets: Array, validators: Array }; diff --git a/packages/ui-app/src/AddressMini.tsx b/packages/ui-app/src/AddressMini.tsx index 0fcbb7f344..062f2aae76 100644 --- a/packages/ui-app/src/AddressMini.tsx +++ b/packages/ui-app/src/AddressMini.tsx @@ -7,7 +7,9 @@ import { BareProps } from './types'; import BN from 'bn.js'; import React from 'react'; import { AccountId, AccountIndex, Address, Balance } from '@polkadot/types'; +import { OfflineStatus } from '@polkadot/app-staking/types'; +import { RecentlyOffline } from '@polkadot/ui-app'; import { classes, toShortAddress } from './util'; import BalanceDisplay from './Balance'; import BondedDisplay from './Bonded'; @@ -19,6 +21,7 @@ type Props = BareProps & { isPadded?: boolean, isShort?: boolean, value?: AccountId | AccountIndex | Address | string, + offlineStatus?: Array, withAddress?: boolean, withBalance?: boolean, withBonded?: boolean @@ -46,6 +49,7 @@ export default class AddressMini extends React.PureComponent { /> {this.renderAddress(address)} {children} + {this.renderOfflineStatus()} {this.renderBalance()} {this.renderBonded()} @@ -96,4 +100,20 @@ export default class AddressMini extends React.PureComponent { /> ); } + + private renderOfflineStatus () { + const { value, offlineStatus } = this.props; + + if (!value || !offlineStatus) { + return null; + } + + return ( + + ); + } } diff --git a/packages/ui-app/src/RecentlyOffline.tsx b/packages/ui-app/src/RecentlyOffline.tsx new file mode 100644 index 0000000000..6f0ca3da05 --- /dev/null +++ b/packages/ui-app/src/RecentlyOffline.tsx @@ -0,0 +1,90 @@ +// Copyright 2017-2019 @polkadot/app-staking authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. +import { AccountId } from '@polkadot/types'; +import { I18nProps } from '@polkadot/ui-app/types'; +import { OfflineStatus } from '@polkadot/app-staking/types'; + +import BN from 'bn.js'; +import React from 'react'; +import { formatNumber } from '@polkadot/util'; + +import ReactTooltip from 'react-tooltip'; + +import translate from './translate'; + +type Props = I18nProps & { + accountId: AccountId | string, + offline: Array, + tooltip?: boolean +}; + +type State = { + isOpen: boolean +}; + +class RecentlyOffline extends React.PureComponent { + state: State = { + isOpen: false + }; + + render () { + const { offline, tooltip = false, t } = this.props; + const { isOpen } = this.state; + const accountId = this.props.accountId.toString(); + + const count = offline.reduce((total, { count }) => total.add(count), new BN(0)); + const blockNumbers = offline.map(({ blockNumber }) => `#${formatNumber(blockNumber)}`); + + const tooltipData = { + 'data-for': `offline-${accountId}`, + 'data-tip': true, + 'data-tip-disable': !tooltip + }; + + const text = t('Reported offline {{count}} times, last at {{blockNumber}}', { + replace: { + count, + blockNumber: blockNumbers[blockNumbers.length - 1] + } + }); + + return ( +
+
+ {count.toString()} +
+
+ {text} +
+ + {text} + +
+ ); + } + + private toggleOpen = (): void => { + this.setState(({ isOpen }) => ({ + isOpen: !isOpen + })); + } +} + +export default translate(RecentlyOffline); diff --git a/packages/ui-app/src/index.tsx b/packages/ui-app/src/index.tsx index 0eac4ea027..d92fd4c0bb 100644 --- a/packages/ui-app/src/index.tsx +++ b/packages/ui-app/src/index.tsx @@ -33,6 +33,7 @@ export { default as Modal } from './Modal'; export { default as Output } from './Output'; export { default as Password } from './Password'; export { default as Progress } from './Progress'; +export { default as RecentlyOffline } from './RecentlyOffline'; export { default as Static } from './Static'; export { default as Status } from './Status'; export { default as SummaryBox } from './SummaryBox'; diff --git a/packages/ui-app/src/styles/components.css b/packages/ui-app/src/styles/components.css index 575155e546..38071fbd4b 100644 --- a/packages/ui-app/src/styles/components.css +++ b/packages/ui-app/src/styles/components.css @@ -262,6 +262,58 @@ header .ui--Button-Group { } } +.ui--RecentlyOffline { + position: absolute; + bottom: 0.75rem; + font-size: 12px; + cursor: help; + display: flex; + justify-content: center; + right: 0.75rem; + width: 22px; + height: 22px; + padding: 0; + background: red; + color: #eee; + border-radius: 16px; + box-shadow: 0 3px 3px rgba(0, 0, 0, 0.2); + text-align: center; + transition: all ease .2s; + + & > * { + line-height: 22px; + overflow: hidden; + transition: all ease 0.25; + } + + .badge { + font-weight: bold; + width: auto; + } + + .detail { + width: 0; + } + + &.expand { + width: 300px; + + .badge { + width: 0; + } + + .detail { + width: auto; + } + } + + &.tooltip { + position: static; + margin: .3rem 0 .3rem .5rem; + } +} + + .ui--Static { overflow: hidden; word-break: break-all;