forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStakingUnbonding.tsx
129 lines (110 loc) · 3.54 KB
/
StakingUnbonding.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2017-2022 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { DeriveSessionProgress, DeriveStakingAccount } from '@polkadot/api-derive/types';
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, BN_ONE, BN_ZERO, formatBalance, formatNumber } from '@polkadot/util';
import Icon from './Icon';
import Tooltip from './Tooltip';
import { useTranslation } from './translate';
interface Unlocking {
remainingEras: BN;
value: BN;
}
interface DeriveStakingAccountPartial {
accountId: DeriveStakingAccount['accountId'] | string;
unlocking?: Unlocking[];
}
interface Props {
iconPosition: 'left' | 'right';
className?: string;
stakingInfo?: DeriveStakingAccountPartial;
}
function extractTotals (stakingInfo?: DeriveStakingAccountPartial, progress?: DeriveSessionProgress): [[Unlocking, 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): [Unlocking, 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;
}
`);