De-dupe account filters (staking & components) (#3311)

This commit is contained in:
Jaco Greeff 2020-08-01 16:09:03 +02:00 committed by GitHub
parent a97184681a
commit 6df29eadb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 37 deletions

View File

@ -8,10 +8,10 @@ import { DeriveAccountInfo, DeriveStakingQuery } from '@polkadot/api-derive/type
import BN from 'bn.js';
import React, { useCallback, useEffect, useState } from 'react';
import { AddressSmall, Icon } from '@polkadot/react-components';
import { checkVisibility } from '@polkadot/react-components/util';
import { useApi, useCall } from '@polkadot/react-hooks';
import { FormatBalance } from '@polkadot/react-query';
import { checkVisibility } from '../../util';
import Favorite from './Favorite';
import NominatedBy from './NominatedBy';
import Status from './Status';
@ -82,8 +82,8 @@ function Address ({ address, className = '', filterName, hasQueries, isElected,
}, [stakingInfo]);
useEffect((): void => {
setIsVisible(
checkVisibility(api, address, filterName, withIdentity, accountInfo)
accountInfo && setIsVisible(
checkVisibility(api, address, accountInfo, filterName, withIdentity)
);
}, [api, accountInfo, address, filterName, withIdentity]);

View File

@ -10,6 +10,7 @@ import BN from 'bn.js';
import React, { useCallback, useEffect, useState } from 'react';
import { ApiPromise } from '@polkadot/api';
import { AddressSmall, Badge, Checkbox, Icon } from '@polkadot/react-components';
import { checkVisibility } from '@polkadot/react-components/util';
import { FormatBalance } from '@polkadot/react-query';
import { useApi, useCall } from '@polkadot/react-hooks';
import { formatNumber } from '@polkadot/util';
@ -18,7 +19,6 @@ import keyring from '@polkadot/ui-keyring';
import MaxBadge from '../MaxBadge';
import Favorite from '../Overview/Address/Favorite';
import { useTranslation } from '../translate';
import { checkVisibility } from '../util';
interface Props {
allSlashes?: [BN, UnappliedSlash[]][];
@ -68,7 +68,7 @@ function Validator ({ allSlashes, canSelect, filterName, info, isNominated, isSe
useEffect((): void => {
if (accountInfo) {
info.hasIdentity = checkIdentity(api, accountInfo);
setVisibility(checkVisibility(api, info.key, filterName, withIdentity, accountInfo));
setVisibility(checkVisibility(api, info.key, accountInfo, filterName, withIdentity));
}
}, [accountInfo, api, filterName, info, withIdentity]);

View File

@ -8,9 +8,9 @@ import React, { useCallback, useEffect, useState } from 'react';
import styled from 'styled-components';
import { useApi, useCall } from '@polkadot/react-hooks';
import { getAddressName } from './util';
import AddressMini from './AddressMini';
import Toggle from './Toggle';
import { checkVisibility } from './util';
interface Props {
address: string;
@ -22,37 +22,16 @@ interface Props {
value?: boolean;
}
function getIsFiltered (address: string, filter?: string, info?: DeriveAccountInfo): boolean {
if (!filter || address.includes(filter)) {
return false;
}
const [,, extracted] = getAddressName(address);
const filterLower = filter.toLowerCase();
if (extracted.toLowerCase().includes(filterLower)) {
return false;
}
if (info) {
const { accountId, accountIndex, identity, nickname } = info;
if (identity.display?.toLowerCase().includes(filterLower) || accountId?.toString().includes(filter) || accountIndex?.toString().includes(filter) || nickname?.toLowerCase().includes(filterLower)) {
return false;
}
}
return true;
}
function AddressToggle ({ address, className = '', filter, isHidden, noToggle, onChange, value }: Props): React.ReactElement<Props> | null {
const { api } = useApi();
const info = useCall<DeriveAccountInfo>(api.derive.accounts.info, [address]);
const [isFiltered, setIsFiltered] = useState(false);
const [isVisible, setIsVisible] = useState(true);
useEffect((): void => {
setIsFiltered(getIsFiltered(address, filter, info));
}, [address, filter, info]);
info && setIsVisible(
checkVisibility(api, address, info, filter, true)
);
}, [api, address, filter, info]);
const _onClick = useCallback(
() => onChange && onChange(!value),
@ -61,7 +40,7 @@ function AddressToggle ({ address, className = '', filter, isHidden, noToggle, o
return (
<div
className={`ui--AddressToggle ${className}${(value || noToggle) ? ' isAye' : ' isNay'}${isHidden || isFiltered ? ' isHidden' : ''}`}
className={`ui--AddressToggle ${className}${(value || noToggle) ? ' isAye' : ' isNay'}${isHidden || !isVisible ? ' isHidden' : ''}`}
onClick={_onClick}
>
<AddressMini

View File

@ -15,7 +15,7 @@ interface Props {
function Available ({ address, filter, isHidden, onSelect }: Props): React.ReactElement<Props> | null {
const _onSelect = useCallback(
(): void => onSelect(address),
() => onSelect(address),
[address, onSelect]
);

View File

@ -7,15 +7,15 @@ import { DeriveAccountInfo } from '@polkadot/api-derive/types';
import { ApiPromise } from '@polkadot/api';
import keyring from '@polkadot/ui-keyring';
export function checkVisibility (api: ApiPromise, address: string, filterName: string, withIdentity: boolean, accountInfo?: DeriveAccountInfo): boolean {
export default function checkVisibility (api: ApiPromise, address: string, accountInfo: DeriveAccountInfo, filterName = '', onlyNamed = false): boolean {
let isVisible = false;
const filterLower = filterName.toLowerCase();
if (filterLower || withIdentity) {
if (filterLower || onlyNamed) {
if (accountInfo) {
const { accountId, accountIndex, identity, nickname } = accountInfo;
if (!withIdentity && (accountId?.toString().includes(filterName) || accountIndex?.toString().includes(filterName))) {
if (!onlyNamed && (accountId?.toString().includes(filterName) || accountIndex?.toString().includes(filterName))) {
isVisible = true;
} else if (api.query.identity && api.query.identity.identityOf) {
isVisible = (!!identity?.display && identity.display.toLowerCase().includes(filterLower)) ||

View File

@ -2,6 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export { default as checkVisibility } from './checkVisibility';
export { default as classes } from './classes';
export { default as getAddressMeta } from './getAddressMeta';
export { default as getAddressName } from './getAddressName';