* Retrofitting basic unit tests to Accounts Extraction of useAccounts and useBalance hooks * Retrofitting basic unit tests to Accounts Extraction of useAccounts and useBalance hooks * Fixing accounts unit tests - basic account balances * Cleaning up after PR remarks * Fixing arrangements of balances 1/3 Moving ArrangedAccount around * Add useStakingInfo hook * Adopt linter suggestions * Fixing arrangements of balances 2/3 Linking arranged balances values to mocked balances calls. * Fixing arrangements of balances 3/3 Formatting utils made consistent * Details row odd-even + (un)hide * Improving th/td/footer layout * Add tags to details row * Changing layout of account balance with details * Add total/details balances unit tests * Add tags unit tests * Clean up of TODOs, adding comments * Improve tags unit testing * Alternate account row details coloring unit test * Row details toggling unit tests * Row details toggling unit tests - bad commit fix * Style address balances (#5897) * Fix jumping column problem * Improve balances positioning * Format sidebar * Position header * Extract common formatting of balances in tests. Thx @vitaliyworks, great hint. * Revert Sidebar styling * Use rems * Lint * Use useToggle for expanding account row * Add tags header * Apply FormatBalance review comments * Display parent account info above account name (#5947) * Display parent address * Improve layout * Set keyring in tests * Remove redundant code from components * Use derivedAccount only in specific test * Apply lint fixes * Remove parent column from expanded row * Apply review suggestions * Improve styling in ParentAddress component * Improve layout of parent address and add short address * Apply review suggestions * Use map instead of for loops * Fix display of AddressSmall * Fix height of icons in AddressSmall * Fix bouncing cells in address row * Fix density of accounts display Co-authored-by: Marcin Wachulski <marcin@ethworks.io> Co-authored-by: Vitaliy Mysak <vitaliy@ethworks.io> Co-authored-by: MiZiet <55240109+MiZiet@users.noreply.github.com> Co-authored-by: MiZiet <michalzietalak@o2.pl> Co-authored-by: Jaco Greeff <jacogr@gmail.com>
152 lines
4.5 KiB
TypeScript
152 lines
4.5 KiB
TypeScript
// Copyright 2017-2021 @polkadot/react-query authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type BN from 'bn.js';
|
|
import type { Compact } from '@polkadot/types';
|
|
import type { Registry } from '@polkadot/types/types';
|
|
|
|
import React, { useMemo } from 'react';
|
|
import styled from 'styled-components';
|
|
|
|
import { useApi } from '@polkadot/react-hooks';
|
|
import { formatBalance, isString } from '@polkadot/util';
|
|
|
|
import { useTranslation } from './translate';
|
|
|
|
interface Props {
|
|
children?: React.ReactNode;
|
|
className?: string;
|
|
format?: [number, string];
|
|
formatIndex?: number;
|
|
isShort?: boolean;
|
|
label?: React.ReactNode;
|
|
labelPost?: LabelPost;
|
|
value?: Compact<any> | BN | string | null | 'all';
|
|
valueFormatted?: string;
|
|
withCurrency?: boolean;
|
|
withSi?: boolean;
|
|
}
|
|
|
|
// for million, 2 * 3-grouping + comma
|
|
const M_LENGTH = 6 + 1;
|
|
const K_LENGTH = 3 + 1;
|
|
|
|
type LabelPost = string | React.ReactNode
|
|
|
|
function getFormat (registry: Registry, formatIndex = 0): [number, string] {
|
|
const decimals = registry.chainDecimals;
|
|
const tokens = registry.chainTokens;
|
|
|
|
return [
|
|
formatIndex < decimals.length
|
|
? decimals[formatIndex]
|
|
: decimals[0],
|
|
formatIndex < tokens.length
|
|
? tokens[formatIndex]
|
|
: tokens[1]
|
|
];
|
|
}
|
|
|
|
function createElement (prefix: string, postfix: string, unit: string, label: LabelPost = '', isShort = false): React.ReactNode {
|
|
return <>{`${prefix}${isShort ? '' : '.'}`}{!isShort && <span className='ui--FormatBalance-postfix'>{`0000${postfix || ''}`.slice(-4)}</span>}<span className='ui--FormatBalance-unit'> {unit}</span>{label}</>;
|
|
}
|
|
|
|
function splitFormat (value: string, label?: LabelPost, isShort?: boolean): React.ReactNode {
|
|
const [prefix, postfixFull] = value.split('.');
|
|
const [postfix, unit] = postfixFull.split(' ');
|
|
|
|
return createElement(prefix, postfix, unit, label, isShort);
|
|
}
|
|
|
|
function applyFormat (value: Compact<any> | BN | string, [decimals, token]: [number, string], withCurrency = true, withSi?: boolean, _isShort?: boolean, labelPost?: LabelPost): React.ReactNode {
|
|
const [prefix, postfix] = formatBalance(value, { decimals, forceUnit: '-', withSi: false }).split('.');
|
|
const isShort = _isShort || (withSi && prefix.length >= K_LENGTH);
|
|
const unitPost = withCurrency ? token : '';
|
|
|
|
if (prefix.length > M_LENGTH) {
|
|
const [major, rest] = formatBalance(value, { decimals, withUnit: false }).split('.');
|
|
const minor = rest.substr(0, 4);
|
|
const unit = rest.substr(4);
|
|
|
|
return <>{major}.<span className='ui--FormatBalance-postfix'>{minor}</span><span className='ui--FormatBalance-unit'>{unit}{unit ? unitPost : ` ${unitPost}`}</span>{labelPost || ''}</>;
|
|
}
|
|
|
|
return createElement(prefix, postfix, unitPost, labelPost, isShort);
|
|
}
|
|
|
|
function FormatBalance ({ children, className = '', format, formatIndex, isShort, label, labelPost, value, valueFormatted, withCurrency, withSi }: Props): React.ReactElement<Props> {
|
|
const { t } = useTranslation();
|
|
const { api } = useApi();
|
|
|
|
const formatInfo = useMemo(
|
|
() => format || getFormat(api.registry, formatIndex),
|
|
[api, format, formatIndex]
|
|
);
|
|
|
|
// labelPost here looks messy, however we ensure we have one less text node
|
|
return (
|
|
<div className={`ui--FormatBalance ${className}`}>
|
|
{label ? <>{label} </> : ''}
|
|
<span
|
|
className='ui--FormatBalance-value'
|
|
data-testid='balance-summary'
|
|
>{
|
|
valueFormatted
|
|
? splitFormat(valueFormatted, labelPost, isShort)
|
|
: value
|
|
? value === 'all'
|
|
? t<string>('everything{{labelPost}}', { replace: { labelPost } })
|
|
: applyFormat(value, formatInfo, withCurrency, withSi, isShort, labelPost)
|
|
: isString(labelPost)
|
|
? `-${labelPost}`
|
|
: labelPost
|
|
}</span>{children}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default React.memo(styled(FormatBalance)`
|
|
display: inline-block;
|
|
vertical-align: baseline;
|
|
white-space: nowrap;
|
|
|
|
* {
|
|
vertical-align: baseline !important;
|
|
}
|
|
|
|
> label,
|
|
> .label {
|
|
display: inline-block;
|
|
margin-right: 0.25rem;
|
|
vertical-align: baseline;
|
|
}
|
|
|
|
.ui--FormatBalance-unit {
|
|
font-size: 0.825em;
|
|
text-transform: uppercase;
|
|
}
|
|
|
|
.ui--FormatBalance-value {
|
|
text-align: right;
|
|
|
|
> .ui--FormatBalance-postfix {
|
|
font-weight: var(--font-weight-light);
|
|
opacity: 0.7;
|
|
vertical-align: baseline;
|
|
}
|
|
}
|
|
|
|
> .ui--Button {
|
|
margin-left: 0.25rem;
|
|
}
|
|
|
|
.ui--Icon {
|
|
margin-bottom: -0.25rem;
|
|
margin-top: 0.25rem;
|
|
}
|
|
|
|
.ui--Icon+.ui--FormatBalance-value {
|
|
margin-left: 0.375rem;
|
|
}
|
|
`);
|