Basic filters for staking accounts (#965)

* Basic filters for staking accounts

* Extract address getter, display names consistently

* Center last block
This commit is contained in:
Jaco Greeff 2019-04-11 14:35:34 +02:00 committed by GitHub
parent fd3d0664aa
commit d73452b865
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 195 additions and 120 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, RecentlyOfflineMap } from '../types';
import { AccountFilter, Nominators, RecentlyOfflineMap } from '../types';
import React from 'react';
import { AccountId, Balance, Exposure, Option, StakingLedger, ValidatorPrefs } from '@polkadot/types';
@ -25,6 +25,7 @@ type Props = ApiProps & I18nProps & {
balances: DerivedBalancesMap,
balanceArray: (_address: AccountId | string) => Array<Balance> | undefined,
controllers: Array<string>,
filter: AccountFilter,
isValidator: boolean,
name: string,
nominators: Nominators,
@ -79,7 +80,12 @@ class Account extends React.PureComponent<Props, State> {
}
render () {
const { accountId, balanceArray, name } = this.props;
const { accountId, balanceArray, filter, name } = this.props;
const { controllerId, stashId } = this.state;
if ((filter === 'controller' && !stashId) || (filter === 'stash' && !controllerId) || (filter === 'unbonded' && (controllerId || stashId))) {
return null;
}
return (
<article className='staking--Account'>

View File

@ -0,0 +1,126 @@
// 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 { I18nProps } from '@polkadot/ui-app/types';
import { AccountFilter, ComponentProps } from './types';
import React from 'react';
import styled from 'styled-components';
import { Dropdown } from '@polkadot/ui-app';
import { getAddrName } from '@polkadot/ui-app/util';
import keyring from '@polkadot/ui-keyring';
import createOption from '@polkadot/ui-keyring/options/item';
import Account from './Account';
import translate from './translate';
import { KeyringSectionOption } from '@polkadot/ui-keyring/options/types';
type Props = I18nProps & ComponentProps;
type State = {
filter: AccountFilter,
filterOptions: Array<{ text: React.ReactNode, value: AccountFilter }>
};
const Wrapper = styled.div`
.accounts {
display: flex;
flex-wrap: wrap;
.spacer {
flex: 1 1;
margin: .25rem;
padding: 1rem 1.5rem;
}
}
.filter {
display: flex;
justify-content: flex-end;
margin-bottom: 0.75rem;
> div {
max-width: 30rem;
}
}
`;
class Accounts extends React.PureComponent<Props, State> {
state: State;
constructor (props: Props) {
super(props);
const { t } = props;
this.state = {
filter: 'all',
filterOptions: [
{ text: t('Show all accounts'), value: 'all' },
{ text: t('Show all unbonded'), value: 'unbonded' },
{ text: t('Show only stashes'), value: 'stash' },
{ text: t('Show only controllers'), value: 'controller' }
]
};
}
render () {
const { balances, balanceArray, controllers, nominators, recentlyOffline, stashes, t, validators } = this.props;
const { filter, filterOptions } = this.state;
const accounts = keyring.getAccounts();
return (
<Wrapper>
<div className='filter'>
<Dropdown
help={t('Select which types of accounts to display, either all, only the stash accounts or the controller accounts.')}
label={t('filter')}
onChange={this.onChangeFilter}
options={filterOptions}
value={filter}
/>
</div>
<div className='accounts'>
{accounts.map((account) => {
const address = account.address();
const name = account.getMeta().name || '';
return (
<Account
accountId={address}
balances={balances}
balanceArray={balanceArray}
controllers={controllers}
filter={filter}
isValidator={validators.includes(address)}
key={address}
name={name}
nominators={nominators}
recentlyOffline={recentlyOffline}
stashes={stashes}
stashOptions={this.getStashOptions()}
validators={validators}
/>
);
})}
<div className='spacer' />
</div>
</Wrapper>
);
}
private getStashOptions (): Array<KeyringSectionOption> {
const { stashes } = this.props;
return stashes.map((stashId) =>
createOption(stashId, getAddrName(stashId))
);
}
private onChangeFilter = (filter: AccountFilter): void => {
this.setState({ filter });
}
}
export default translate(Accounts);

View File

@ -10,7 +10,7 @@ import React from 'react';
import { AccountId, Balance, Option, StakingLedger } from '@polkadot/types';
import { withCalls, withMulti } from '@polkadot/ui-api/with';
import { AddressMini, AddressRow, RecentlyOffline } from '@polkadot/ui-app';
import keyring from '@polkadot/ui-keyring';
import { getAddrName } from '@polkadot/ui-app/util';
import { formatBalance } from '@polkadot/util';
import translate from '../translate';
@ -114,13 +114,7 @@ class Address extends React.PureComponent<Props, State> {
return defaultName;
}
const pair = keyring.getAccount(stashId).isValid()
? keyring.getAccount(stashId)
: keyring.getAddress(stashId);
return pair.isValid()
? (pair.getMeta().name || defaultName)
: defaultName;
return getAddrName(stashId) || defaultName;
}
private renderKeys () {

View File

@ -55,6 +55,8 @@ class Summary extends React.PureComponent<Props> {
)}
{lastBlock}
</CardSummary>
</section>
<section>
<SummarySession withBroken={false} />
</section>
</SummaryBox>

View File

@ -1,79 +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 { I18nProps } from '@polkadot/ui-app/types';
import { ComponentProps } from './types';
import React from 'react';
import keyring from '@polkadot/ui-keyring';
import createOption from '@polkadot/ui-keyring/options/item';
import Account from './Account';
import translate from './translate';
import { KeyringSectionOption } from '@polkadot/ui-keyring/options/types';
type Props = I18nProps & ComponentProps;
class StakeList extends React.PureComponent<Props> {
render () {
const { balances, balanceArray, controllers, nominators, recentlyOffline, stashes, validators } = this.props;
const accounts = keyring.getAccounts();
return (
<div className='staking--StakeList'>
{accounts.map((account) => {
const address = account.address();
const name = account.getMeta().name || '';
return (
<Account
accountId={address}
balances={balances}
balanceArray={balanceArray}
controllers={controllers}
isValidator={validators.includes(address)}
key={address}
name={name}
nominators={nominators}
recentlyOffline={recentlyOffline}
stashes={stashes}
stashOptions={this.getStashOptions()}
validators={validators}
/>
);
})}
{this.renderSpacer(accounts.length)}
</div>
);
}
// HACK This is a hack of a dummy element to get the spacing right, i.e. the last
// element in an oddly spaced list should still only take up a single column
private renderSpacer (accountsLen: number) {
if (accountsLen % 2 === 0) {
return null;
}
return (
<div className='spacer' />
);
}
private getStashOptions (): Array<KeyringSectionOption> {
const { stashes } = this.props;
return stashes.map((stashId) => {
const pair = keyring.getAccount(stashId).isValid()
? keyring.getAccount(stashId)
: keyring.getAddress(stashId);
const name = pair.isValid()
? pair.getMeta().name
: undefined;
return createOption(stashId, name);
});
}
}
export default translate(StakeList);

View File

@ -14,6 +14,7 @@
.staking--Account {
position: relative;
flex: 1 1;
min-width: 48%;
.ui--AddressMini {
display: block;
@ -109,17 +110,6 @@
}
}
.staking--StakeList {
display: flex;
flex-wrap: wrap;
.spacer {
flex: 1 1;
margin: .25rem;
padding: 1rem 1.5rem;
}
}
.staking--accounts-info {
position: absolute;
top: 1rem;
@ -129,4 +119,4 @@
.staking--label {
margin: 0 0 -0.5rem 0;
}
}
}

View File

@ -19,7 +19,7 @@ import accountObservable from '@polkadot/ui-keyring/observable/accounts';
import './index.css';
import basicMd from './md/basic.md';
import StakeList from './StakeList';
import Accounts from './Accounts';
import Overview from './Overview';
import translate from './translate';
@ -120,7 +120,7 @@ class App extends React.PureComponent<Props, State> {
/>
</header>
<Switch>
<Route path={`${basePath}/actions`} render={this.renderComponent(StakeList)} />
<Route path={`${basePath}/actions`} render={this.renderComponent(Accounts)} />
<Route render={this.renderComponent(Overview)} />
</Switch>
</main>

View File

@ -31,3 +31,5 @@ export interface OfflineStatus {
blockNumber: BlockNumber;
count: BN;
}
export type AccountFilter = 'all' | 'controller' | 'session' | 'stash' | 'unbonded';

View File

@ -8,9 +8,9 @@ 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 { classes, getAddrName, toShortAddress } from './util';
import BalanceDisplay from './Balance';
import BondedDisplay from './Bonded';
import IdentityIcon from './IdentityIcon';
@ -47,7 +47,7 @@ export default class AddressMini extends React.PureComponent<Props> {
size={24}
value={address}
/>
{this.renderAddress(address)}
{this.renderAddressOrName(address)}
{children}
{this.renderOfflineStatus()}
</div>
@ -57,15 +57,25 @@ export default class AddressMini extends React.PureComponent<Props> {
);
}
private renderAddress (address: string) {
private renderAddressOrName (address: string) {
const { isShort = true, withAddress = true } = this.props;
if (!withAddress) {
return null;
}
const name = getAddrName(address);
console.error('address', address, name);
return (
<div className='ui--AddressMini-address'>{isShort ? toShortAddress(address) : address}</div>
<div className={`ui--AddressMini-address ${name ? 'withName' : 'withAddr'}`}>{
name || (
isShort
? toShortAddress(address)
: address
)
}</div>
);
}

View File

@ -15,7 +15,7 @@ import createItem from '@polkadot/ui-keyring/options/item';
import { withMulti, withObservable } from '@polkadot/ui-api';
import Dropdown from '../Dropdown';
import { classes } from '../util';
import { classes, getAddrName } from '../util';
import addressToAddress from '../util/toAddress';
type Props = BareProps & {
@ -175,19 +175,12 @@ class InputAddress extends React.PureComponent<Props, State> {
);
}
private renderLabel = ({ value }: KeyringSectionOption): string | null => {
private renderLabel = ({ value }: KeyringSectionOption): string | undefined => {
if (!value) {
return null;
return undefined;
}
const pair = keyring.getAccount(value).isValid()
? keyring.getAccount(value)
: keyring.getAddress(value);
const name = pair.isValid()
? pair.getMeta().name
: undefined;
return name || `${value.slice(0, 6)}…${value.slice(-6)}`;
return getAddrName(value, true);
}
private getLastOptionValue (): KeyringSectionOption | undefined {

View File

@ -60,7 +60,13 @@ header .ui--Button-Group {
}
.ui--AddressMini-address {
font-family: monospace;
&.withAddr {
font-family: monospace;
}
&.withName {
text-transform: uppercase;
}
}
.ui--AddressMini .ui--IdentityIcon {
@ -144,6 +150,10 @@ header .ui--Button-Group {
.ui--AddressSummary-data {
padding: 0;
white-space: nowrap;
.ui--AddressSummary-name {
text-transform: uppercase;
}
}
.ui--AddressSummary-data * {

View File

@ -0,0 +1,20 @@
// Copyright 2017-2019 @polkadot/ui-app 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 keyring from '@polkadot/ui-keyring';
import toShortAddress from './toShortAddress';
export default function getAddrName (address: string, withShort?: boolean): string | undefined {
const pair = keyring.getAccount(address).isValid()
? keyring.getAccount(address)
: keyring.getAddress(address);
const name = pair.isValid()
? pair.getMeta().name
: undefined;
return !name && withShort
? toShortAddress(address)
: name;
}

View File

@ -3,5 +3,6 @@
// of the Apache-2.0 license. See the LICENSE file for details.
export { default as classes } from './classes';
export { default as getAddrName } from './getAddrName';
export { default as toAddress } from './toAddress';
export { default as toShortAddress } from './toShortAddress';