From d73452b865feaed5002c390029f1e8556a76a1cf Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 11 Apr 2019 14:35:34 +0200 Subject: [PATCH] Basic filters for staking accounts (#965) * Basic filters for staking accounts * Extract address getter, display names consistently * Center last block --- packages/app-staking/src/Account/index.tsx | 10 +- packages/app-staking/src/Accounts.tsx | 126 ++++++++++++++++++ packages/app-staking/src/Overview/Address.tsx | 10 +- packages/app-staking/src/Overview/Summary.tsx | 2 + packages/app-staking/src/StakeList.tsx | 79 ----------- packages/app-staking/src/index.css | 14 +- packages/app-staking/src/index.tsx | 4 +- packages/app-staking/src/types.ts | 2 + packages/ui-app/src/AddressMini.tsx | 20 ++- packages/ui-app/src/InputAddress/index.tsx | 15 +-- packages/ui-app/src/styles/components.css | 12 +- packages/ui-app/src/util/getAddrName.ts | 20 +++ packages/ui-app/src/util/index.ts | 1 + 13 files changed, 195 insertions(+), 120 deletions(-) create mode 100644 packages/app-staking/src/Accounts.tsx delete mode 100644 packages/app-staking/src/StakeList.tsx create mode 100644 packages/ui-app/src/util/getAddrName.ts diff --git a/packages/app-staking/src/Account/index.tsx b/packages/app-staking/src/Account/index.tsx index 903b15f30e..0f73540ee5 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, 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 | undefined, controllers: Array, + filter: AccountFilter, isValidator: boolean, name: string, nominators: Nominators, @@ -79,7 +80,12 @@ class Account extends React.PureComponent { } 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 (
diff --git a/packages/app-staking/src/Accounts.tsx b/packages/app-staking/src/Accounts.tsx new file mode 100644 index 0000000000..0c128838b9 --- /dev/null +++ b/packages/app-staking/src/Accounts.tsx @@ -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 { + 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 ( + +
+ +
+
+ {accounts.map((account) => { + const address = account.address(); + const name = account.getMeta().name || ''; + + return ( + + ); + })} +
+
+ + ); + } + + private getStashOptions (): Array { + const { stashes } = this.props; + + return stashes.map((stashId) => + createOption(stashId, getAddrName(stashId)) + ); + } + + private onChangeFilter = (filter: AccountFilter): void => { + this.setState({ filter }); + } +} + +export default translate(Accounts); diff --git a/packages/app-staking/src/Overview/Address.tsx b/packages/app-staking/src/Overview/Address.tsx index dacf2c9efa..129f16560d 100644 --- a/packages/app-staking/src/Overview/Address.tsx +++ b/packages/app-staking/src/Overview/Address.tsx @@ -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 { 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 () { diff --git a/packages/app-staking/src/Overview/Summary.tsx b/packages/app-staking/src/Overview/Summary.tsx index b1ea49eeb0..0d1ce52e88 100644 --- a/packages/app-staking/src/Overview/Summary.tsx +++ b/packages/app-staking/src/Overview/Summary.tsx @@ -55,6 +55,8 @@ class Summary extends React.PureComponent { )} {lastBlock} + +
diff --git a/packages/app-staking/src/StakeList.tsx b/packages/app-staking/src/StakeList.tsx deleted file mode 100644 index 8666cd87ed..0000000000 --- a/packages/app-staking/src/StakeList.tsx +++ /dev/null @@ -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 { - render () { - const { balances, balanceArray, controllers, nominators, recentlyOffline, stashes, validators } = this.props; - const accounts = keyring.getAccounts(); - - return ( -
- {accounts.map((account) => { - const address = account.address(); - const name = account.getMeta().name || ''; - - return ( - - ); - })} - {this.renderSpacer(accounts.length)} -
- ); - } - - // 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 ( -
- ); - } - - private getStashOptions (): Array { - 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); diff --git a/packages/app-staking/src/index.css b/packages/app-staking/src/index.css index ed32f2941f..8f4851ec9a 100644 --- a/packages/app-staking/src/index.css +++ b/packages/app-staking/src/index.css @@ -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; } -} \ No newline at end of file +} diff --git a/packages/app-staking/src/index.tsx b/packages/app-staking/src/index.tsx index 83e6c35d3b..ffccb2f7b5 100644 --- a/packages/app-staking/src/index.tsx +++ b/packages/app-staking/src/index.tsx @@ -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 { /> - + diff --git a/packages/app-staking/src/types.ts b/packages/app-staking/src/types.ts index da5e7c6484..af8cd5c460 100644 --- a/packages/app-staking/src/types.ts +++ b/packages/app-staking/src/types.ts @@ -31,3 +31,5 @@ export interface OfflineStatus { blockNumber: BlockNumber; count: BN; } + +export type AccountFilter = 'all' | 'controller' | 'session' | 'stash' | 'unbonded'; diff --git a/packages/ui-app/src/AddressMini.tsx b/packages/ui-app/src/AddressMini.tsx index 2c8aa3938d..5ec222358f 100644 --- a/packages/ui-app/src/AddressMini.tsx +++ b/packages/ui-app/src/AddressMini.tsx @@ -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 { size={24} value={address} /> - {this.renderAddress(address)} + {this.renderAddressOrName(address)} {children} {this.renderOfflineStatus()}
@@ -57,15 +57,25 @@ export default class AddressMini extends React.PureComponent { ); } - 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 ( -
{isShort ? toShortAddress(address) : address}
+
{ + name || ( + isShort + ? toShortAddress(address) + : address + ) + }
); } diff --git a/packages/ui-app/src/InputAddress/index.tsx b/packages/ui-app/src/InputAddress/index.tsx index 1671be8c39..3275e694e6 100644 --- a/packages/ui-app/src/InputAddress/index.tsx +++ b/packages/ui-app/src/InputAddress/index.tsx @@ -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 { ); } - 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 { diff --git a/packages/ui-app/src/styles/components.css b/packages/ui-app/src/styles/components.css index c76e87004e..1d14b83be8 100644 --- a/packages/ui-app/src/styles/components.css +++ b/packages/ui-app/src/styles/components.css @@ -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 * { diff --git a/packages/ui-app/src/util/getAddrName.ts b/packages/ui-app/src/util/getAddrName.ts new file mode 100644 index 0000000000..5e713cc0df --- /dev/null +++ b/packages/ui-app/src/util/getAddrName.ts @@ -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; +} diff --git a/packages/ui-app/src/util/index.ts b/packages/ui-app/src/util/index.ts index 740ff34abd..d1d59cf3d8 100644 --- a/packages/ui-app/src/util/index.ts +++ b/packages/ui-app/src/util/index.ts @@ -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';