ghost-portal/packages/react-components/src/StakingUnbonding.tsx
Ewa Kowalska c7416ae159
Account row collapsible row details (#5975)
* 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>
2021-08-26 13:02:24 +02:00

121 lines
3.4 KiB
TypeScript

// Copyright 2017-2021 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { DeriveSessionProgress, DeriveStakingAccount, DeriveUnlocking } from '@polkadot/api-derive/types';
import BN from 'bn.js';
import React, { useMemo } from 'react';
import styled from 'styled-components';
import { useApi, useCall } from '@polkadot/react-hooks';
import { BlockToTime, FormatBalance } from '@polkadot/react-query';
import { BN_ONE, BN_ZERO, formatBalance, formatNumber } from '@polkadot/util';
import Icon from './Icon';
import Tooltip from './Tooltip';
import { useTranslation } from './translate';
interface Props {
iconPosition: 'left' | 'right';
className?: string;
stakingInfo?: DeriveStakingAccount;
}
function extractTotals (stakingInfo?: DeriveStakingAccount, progress?: DeriveSessionProgress): [[DeriveUnlocking, BN, BN][], BN] {
if (!stakingInfo?.unlocking || !progress) {
return [[], BN_ZERO];
}
const mapped = stakingInfo.unlocking
.filter(({ remainingEras, value }) => value.gt(BN_ZERO) && remainingEras.gt(BN_ZERO))
.map((unlock): [DeriveUnlocking, BN, BN] => [
unlock,
unlock.remainingEras,
unlock.remainingEras
.sub(BN_ONE)
.imul(progress.eraLength)
.iadd(progress.eraLength)
.isub(progress.eraProgress)
]);
const total = mapped.reduce((total, [{ value }]) => total.iadd(value), new BN(0));
return [mapped, total];
}
function StakingUnbonding ({ className = '', iconPosition = 'left', stakingInfo }: Props): React.ReactElement<Props> | null {
const { api } = useApi();
const progress = useCall<DeriveSessionProgress>(api.derive.session.progress);
const { t } = useTranslation();
const [mapped, total] = useMemo(
() => extractTotals(stakingInfo, progress),
[progress, stakingInfo]
);
if (!stakingInfo || !mapped.length) {
return null;
}
const trigger = `${stakingInfo.accountId.toString()}-unlocking-trigger`;
return (
<div className={className}>
{iconPosition === 'left' && (
<Icon
className='left'
icon='clock'
tooltip={trigger}
/>
)}
<FormatBalance value={total} />
<Tooltip
text={mapped.map(([{ value }, eras, blocks], index): React.ReactNode => (
<div
className='row'
key={index}
>
<div>{t<string>('Unbonding {{value}}', { replace: { value: formatBalance(value, { forceUnit: '-' }) } })}</div>
<div className='faded'>
{api.consts.babe?.epochDuration
? (
<BlockToTime
label={`${t<string>('{{blocks}} blocks', { replace: { blocks: formatNumber(blocks) } })}, `}
value={blocks}
/>
)
: t<string>('{{eras}} eras remaining', { replace: { eras: formatNumber(eras) } })
}
</div>
</div>
))}
trigger={trigger}
/>
{iconPosition === 'right' && (
<Icon
className='right'
icon='clock'
tooltip={trigger}
/>
)}
</div>
);
}
export default React.memo(styled(StakingUnbonding)`
white-space: nowrap;
.ui--Icon.left {
margin-left: 0;
margin-right: 0.25rem;
}
.ui--Icon.right {
margin-left: 0.25rem;
margin-right: 0;
}
.ui--FormatBalance {
display: inline-block;
}
`);