From 2f6e8c8afebc4f91a1b5221af7418992eaf80012 Mon Sep 17 00:00:00 2001 From: kwingram25 Date: Wed, 3 Apr 2019 05:52:01 -0700 Subject: [PATCH] Add recently offline badge to validators (#883) * Add recently offline badge to validators * Change formatNumber import (available from util now) * Typo. * minor fixes --- packages/app-staking/src/Overview/Address.tsx | 103 +++++++++++++----- .../app-staking/src/Overview/CurrentList.tsx | 22 +++- packages/app-staking/src/Overview/index.css | 50 +++++++++ packages/app-staking/src/types.ts | 14 ++- 4 files changed, 159 insertions(+), 30 deletions(-) diff --git a/packages/app-staking/src/Overview/Address.tsx b/packages/app-staking/src/Overview/Address.tsx index aaa62fddf5..da8cffaad8 100644 --- a/packages/app-staking/src/Overview/Address.tsx +++ b/packages/app-staking/src/Overview/Address.tsx @@ -4,11 +4,11 @@ import { DerivedBalancesMap } from '@polkadot/api-derive/types'; import { I18nProps } from '@polkadot/ui-app/types'; -import { Nominators } from '../types'; +import { Nominators, RecentlyOfflineMap } from '../types'; import React from 'react'; -import { AccountId, Balance } from '@polkadot/types'; -import { withMulti } from '@polkadot/ui-api/with'; +import { AccountId, Balance, Option } from '@polkadot/types'; +import { withCall, withMulti } from '@polkadot/ui-api/with'; import { AddressMini, AddressRow } from '@polkadot/ui-app'; import keyring from '@polkadot/ui-keyring'; @@ -21,11 +21,21 @@ type Props = I18nProps & { defaultName: string, isAuthor: boolean, lastBlock: string, - nominators: Nominators + nominators: Nominators, + recentlyOffline: RecentlyOfflineMap, + staking_bonded?: Option }; -class Address extends React.PureComponent { - private getDisplayName () { +type State = { + badgeExpanded: boolean; +}; + +class Address extends React.PureComponent { + state: State = { + badgeExpanded: false + }; + + private getDisplayName = (): string | undefined => { const { address, defaultName } = this.props; const pair = keyring.getAccount(address).isValid() @@ -37,29 +47,71 @@ class Address extends React.PureComponent { : defaultName; } + private onClickBadge = (): void => { + const { badgeExpanded } = this.state; + + this.setState({ badgeExpanded: !badgeExpanded }); + } + render () { - const { address, balanceArray, isAuthor, lastBlock, nominators, t } = this.props; + const { address, balanceArray, isAuthor, lastBlock, nominators, recentlyOffline, staking_bonded, t } = this.props; + const { badgeExpanded } = this.state; const myNominators = Object.keys(nominators).filter((nominator) => nominators[nominator].indexOf(address) !== -1 ); - const children = myNominators.length - ? ( -
- {t('Nominators ({{count}})', { - replace: { - count: myNominators.length - } - })} - {myNominators.map((accountId) => - + const bondedId: string | null = staking_bonded && staking_bonded.isSome + ? staking_bonded.unwrap().toString() + : null; + + const hasNominators = !!myNominators.length; + const isRecentlyOffline = bondedId && recentlyOffline[bondedId]; + + const children = (hasNominators || isRecentlyOffline) ? ( + <> +
+ {myNominators.length && ( + <> + + {t('Nominators ({{count}})', { + replace: { + count: myNominators.length + } + })} + + {myNominators.map((accountId) => + + )} + )} -
- ) - : undefined; +
+ {(bondedId && recentlyOffline[bondedId]) && (() => { + const { blockNumber, instances } = recentlyOffline[bondedId]; + + return ( +
+
+ {instances.toString()} +
+
+ {t('Reported offline {{instances}} times since block #{{blockNumber}}', { + replace: { + instances: instances.toString(), + blockNumber + } + })} +
+
+ ); + })()} + + ) : undefined; return (
@@ -85,5 +137,6 @@ class Address extends React.PureComponent { export default withMulti( Address, - translate + translate, + withCall('query.staking.bonded', { paramName: 'address' }) ); diff --git a/packages/app-staking/src/Overview/CurrentList.tsx b/packages/app-staking/src/Overview/CurrentList.tsx index aed9ae7e48..12883ece6f 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 } from '../types'; +import { Nominators, RecentlyOffline, RecentlyOfflineMap } from '../types'; import React from 'react'; import { AccountId, Balance, HeaderExtended } from '@polkadot/types'; @@ -20,7 +20,8 @@ type Props = I18nProps & { chain_subscribeNewHead?: HeaderExtended, current: Array, next: Array, - nominators: Nominators + nominators: Nominators, + staking_recentlyOffline?: RecentlyOffline }; class CurrentList extends React.PureComponent { @@ -66,7 +67,7 @@ class CurrentList extends React.PureComponent { } private renderColumn (addresses: Array, defaultName: string) { - const { balances, balanceArray, chain_subscribeNewHead, nominators, t } = this.props; + const { balances, balanceArray, chain_subscribeNewHead, nominators, staking_recentlyOffline, t } = this.props; if (addresses.length === 0) { return ( @@ -82,6 +83,17 @@ class CurrentList extends React.PureComponent { lastAuthor = (chain_subscribeNewHead.author || '').toString(); } + const recentlyOffline: RecentlyOfflineMap = (staking_recentlyOffline || []).reduce( + (result, [accountId, blockNumber, instances]) => ({ + ...result, + [accountId.toString()]: { + blockNumber, + instances + } + }), + {} + ); + return (
{addresses.map((address) => ( @@ -94,6 +106,7 @@ class CurrentList extends React.PureComponent { key={address} lastBlock={lastBlock} nominators={nominators} + recentlyOffline={recentlyOffline} /> ))}
@@ -105,6 +118,7 @@ export default withMulti( CurrentList, translate, withCalls( - 'derive.chain.subscribeNewHead' + 'derive.chain.subscribeNewHead', + 'query.staking.recentlyOffline' ) ); diff --git a/packages/app-staking/src/Overview/index.css b/packages/app-staking/src/Overview/index.css index 135798aa82..e060484fc4 100644 --- a/packages/app-staking/src/Overview/index.css +++ b/packages/app-staking/src/Overview/index.css @@ -54,6 +54,11 @@ position: relative; details { + summary { + cursor: pointer; + outline: none; + } + summary + div { margin-top: 0; } @@ -78,6 +83,51 @@ transform: scale(1); } } + + .recentlyOffline { + position: absolute; + font-size: 12px; + cursor: help; + display: flex; + justify-content: center; + right: 12px; + top: 88px; + 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; + } + } + } } } diff --git a/packages/app-staking/src/types.ts b/packages/app-staking/src/types.ts index 3b3fae9d6f..a511059126 100644 --- a/packages/app-staking/src/types.ts +++ b/packages/app-staking/src/types.ts @@ -2,8 +2,9 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import BN from 'bn.js'; import { DerivedBalancesMap } from '@polkadot/api-derive/types'; -import { AccountId, Balance } from '@polkadot/types'; +import { AccountId, Balance, BlockNumber } from '@polkadot/types'; export type Nominators = { // stash account and who is being nominated @@ -17,3 +18,14 @@ export type ComponentProps = { nominators: Nominators, validators: Array }; + +export type RecentlyOffline = Array<[AccountId, BlockNumber, BN]>; + +export type RecentlyOfflineMap = { + [s: string]: OfflineStatus +}; + +export interface OfflineStatus { + blockNumber: BlockNumber; + instances: BN; +}