forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvictionDropdown.tsx
66 lines (55 loc) · 1.92 KB
/
ConvictionDropdown.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
// Copyright 2017-2022 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { TFunction } from 'i18next';
import React, { useRef } from 'react';
import { ApiPromise } from '@polkadot/api';
import { useApi, useBlockInterval } from '@polkadot/react-hooks';
import { BN, BN_THOUSAND } from '@polkadot/util';
import Dropdown from './Dropdown';
import { useTranslation } from './translate';
export interface Props {
className?: string;
help?: string
label?: React.ReactNode;
onChange?: (value: number) => void;
value?: number;
}
const CONVICTIONS: [number, number, BN][] = [1, 2, 4, 8, 16, 32].map((lock, index) => [index + 1, lock, new BN(lock)]);
const SEC_DAY = 60 * 60 * 24;
function createOptions (api: ApiPromise, t: TFunction, blockTime: BN): { text: string; value: number }[] {
return [
{ text: t<string>('0.1x voting balance, no lockup period'), value: 0 },
...CONVICTIONS.map(([value, lock, bnLock]): { text: string; value: number } => ({
text: t<string>('{{value}}x voting balance, locked for {{lock}}x duration ({{period}} days)', {
replace: {
lock,
period: (
bnLock.mul(
api.consts.democracy.voteLockingPeriod ||
api.consts.democracy.enactmentPeriod
).mul(blockTime).div(BN_THOUSAND).toNumber() / SEC_DAY
).toFixed(2),
value
}
}),
value
}))
];
}
function Convictions ({ className = '', help, label, onChange, value }: Props): React.ReactElement<Props> | null {
const { api } = useApi();
const { t } = useTranslation();
const blockTime = useBlockInterval();
const optionsRef = useRef(createOptions(api, t, blockTime));
return (
<Dropdown
className={className}
help={help}
label={label}
onChange={onChange}
options={optionsRef.current}
value={value}
/>
);
}
export default React.memo(Convictions);