RecentlyOffline added to Account Actions (#929)

* RecentlyOffline added to Account Actions

* travis-ci

* relocate recentlyOffline state

* comma

* comma
This commit is contained in:
kwingram25 2019-04-10 01:55:14 -07:00 committed by Jaco Greeff
parent ae47449482
commit 51e71dc3ac
13 changed files with 222 additions and 156 deletions

View File

@ -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<AccountId>,
staking_bonded?: Option<AccountId>,
staking_ledger?: Option<StakingLedger>,
@ -193,7 +194,8 @@ class Account extends React.PureComponent<Props, State> {
}
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<Props, State> {
<AddressMini
key={index}
value={nomineeId}
offlineStatus={recentlyOffline[nomineeId]}
withBalance={false}
withBonded
/>
@ -242,7 +245,7 @@ class Account extends React.PureComponent<Props, State> {
}
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<Props, State> {
return (
<div className='staking--Account-detail'>
<label className='staking--label'>{t('controller')}</label>
<AddressMini value={bondedId} />
<AddressMini
value={bondedId}
offlineStatus={recentlyOffline[bondedId]}
/>
</div>
);
}
@ -274,7 +280,7 @@ class Account extends React.PureComponent<Props, State> {
}
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<Props, State> {
return (
<div className='staking--Account-detail'>
<label className='staking--label'>{t('stash')}</label>
<AddressMini withBalance={false} withBonded value={stashId} />
<AddressMini
value={stashId}
offlineStatus={recentlyOffline[stashId]}
withBalance={false}
withBonded
/>
</div>
);
}
@ -421,6 +432,7 @@ class Account extends React.PureComponent<Props, State> {
export default translate(
withCalls<Props>(
'query.staking.recentlyOffline',
['query.session.nextKeyFor', { paramName: 'accountId' }],
['query.staking.bonded', { paramName: 'accountId' }],
['query.staking.ledger', { paramName: 'accountId' }],

View File

@ -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<Props, State> {
const offline = recentlyOffline[stashId];
return (
<RecentlyOffline offline={offline} />
<RecentlyOffline
accountId={stashId}
offline={offline}
/>
);
}
}

View File

@ -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<string>,
next: Array<string>,
nominators: Nominators,
staking_recentlyOffline?: RecentlyOffline
};
type State = {
recentlyOffline: RecentlyOfflineMap
};
class CurrentList extends React.PureComponent<Props, State> {
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<Props> {
render () {
return (
<div className='validator--ValidatorsList ui--flex-medium'>
@ -93,8 +67,7 @@ class CurrentList extends React.PureComponent<Props, State> {
}
private renderColumn (addresses: Array<string>, 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<Props>(
'derive.chain.subscribeNewHead',
'query.staking.recentlyOffline'
'derive.chain.subscribeNewHead'
)
);

View File

@ -19,7 +19,7 @@ const ZERO = new Balance(0);
export default class Overview extends React.PureComponent<Props> {
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<Props> {
current={validatorsSorted}
next={intentionsSorted}
nominators={nominators}
recentlyOffline={recentlyOffline}
/>
</div>
);

View File

@ -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<OfflineStatus>
};
type State = {
isOpen: boolean
};
class RecentlyOffline extends React.PureComponent<Props, State> {
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 (
<div
className={['staking--Account-recentlyOffline', isOpen ? 'expand' : ''].join(' ')}
onClick={this.toggleOpen}
>
<div className='badge'>
{count.toString()}
</div>
<div className='detail'>
{t('Reported offline {{count}} times, last at {{blockNumber}}', {
replace: {
count,
blockNumber: blockNumbers[blockNumbers.length - 1]
}
})}
</div>
</div>
);
}
private toggleOpen = (): void => {
this.setState(({ isOpen }) => ({
isOpen: !isOpen
}));
}
}
export default translate(RecentlyOffline);

View File

@ -17,7 +17,7 @@ type Props = I18nProps & ComponentProps;
class StakeList extends React.PureComponent<Props> {
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<Props> {
key={address}
name={name}
nominators={nominators}
recentlyOffline={recentlyOffline}
targets={this.getTargetOptions()}
validators={validators}
/>

View File

@ -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;
}
}
}
}

View File

@ -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<AccountId>,
staking_controllers?: [Array<AccountId>, Array<Option<AccountId>>],
staking_nominators?: [Array<AccountId>, Array<Array<AccountId>>]
staking_nominators?: [Array<AccountId>, Array<Array<AccountId>>],
staking_recentlyOffline?: RecentlyOffline
};
type State = {
intentions: Array<string>,
nominators: Nominators,
recentlyOffline: RecentlyOfflineMap,
tabs: Array<TabItem>,
targets: Array<string>,
validators: Array<string>
@ -50,6 +52,7 @@ class App extends React.PureComponent<Props, State> {
this.state = {
intentions: [],
nominators: {},
recentlyOffline: {},
tabs: [
{
name: 'overview',
@ -65,7 +68,7 @@ class App extends React.PureComponent<Props, State> {
};
}
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<Props, State> {
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<Props, State> {
private renderComponent (Component: React.ComponentType<ComponentProps>) {
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<Props, State> {
balanceArray={this.balanceArray}
intentions={intentions}
nominators={nominators}
recentlyOffline={recentlyOffline}
targets={targets}
validators={validators}
/>
@ -151,7 +170,8 @@ export default withMulti(
withCalls<Props>(
'derive.staking.controllers',
'query.session.validators',
'query.staking.nominators'
'query.staking.nominators',
'query.staking.recentlyOffline'
),
withObservable(accountObservable.subject, { propName: 'allAccounts' })
);

View File

@ -16,6 +16,7 @@ export type ComponentProps = {
balanceArray: (_address: AccountId | string) => Array<Balance> | undefined,
intentions: Array<string>,
nominators: Nominators,
recentlyOffline: RecentlyOfflineMap,
targets: Array<string>,
validators: Array<string>
};

View File

@ -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<OfflineStatus>,
withAddress?: boolean,
withBalance?: boolean,
withBonded?: boolean
@ -46,6 +49,7 @@ export default class AddressMini extends React.PureComponent<Props> {
/>
{this.renderAddress(address)}
{children}
{this.renderOfflineStatus()}
</div>
{this.renderBalance()}
{this.renderBonded()}
@ -96,4 +100,20 @@ export default class AddressMini extends React.PureComponent<Props> {
/>
);
}
private renderOfflineStatus () {
const { value, offlineStatus } = this.props;
if (!value || !offlineStatus) {
return null;
}
return (
<RecentlyOffline
accountId={value.toString()}
offline={offlineStatus}
tooltip
/>
);
}
}

View File

@ -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<OfflineStatus>,
tooltip?: boolean
};
type State = {
isOpen: boolean
};
class RecentlyOffline extends React.PureComponent<Props, State> {
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 (
<div
className={[
'ui--RecentlyOffline',
isOpen ? 'expand' : '',
tooltip ? 'tooltip' : ''
].join(' ')}
{...(!tooltip ?
{ onClick: this.toggleOpen } :
{}
)}
{...tooltipData}
>
<div className='badge'>
{count.toString()}
</div>
<div className='detail'>
{text}
</div>
<ReactTooltip
delayShow={250}
effect='solid'
id={`offline-${accountId}`}
place='bottom'
>
{text}
</ReactTooltip>
</div>
);
}
private toggleOpen = (): void => {
this.setState(({ isOpen }) => ({
isOpen: !isOpen
}));
}
}
export default translate(RecentlyOffline);

View File

@ -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';

View File

@ -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;