-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAttachFundsRadio.tsx
More file actions
148 lines (131 loc) · 4.05 KB
/
AttachFundsRadio.tsx
File metadata and controls
148 lines (131 loc) · 4.05 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { useEffect, useMemo, useState } from 'react';
import { Box } from '@interchain-ui/react';
import { Asset } from '@chain-registry/types';
import BigNumber from 'bignumber.js';
import { TbCurrencyDollarOff } from 'react-icons/tb';
import { LuListPlus } from 'react-icons/lu';
import { VscJson } from 'react-icons/vsc';
import { Coin } from '@interchainjs/react/cosmos/base/v1beta1/coin';
import { JsonInput } from './JsonInput';
import { SelectAssetContent } from './SelectAssetContent';
import { getExponentFromAsset, prettifyJson } from '@/utils';
import { Radio, RadioGroup } from '../../common';
const defaultAssetListJson = prettifyJson(
JSON.stringify([{ denom: '', amount: '' }])
);
export type SelectedAssetWithAmount = {
asset: Asset | undefined;
amount: string;
};
export const defaultSelectedAsset: SelectedAssetWithAmount = {
asset: undefined,
amount: '',
};
export type FundsOptionKey = 'no_funds' | 'select_assets' | 'json_asset_list';
type FundsOption = {
key: FundsOptionKey;
icon: React.ReactNode;
label: string;
content: React.ReactNode;
};
type AttachFundsRadioProps = {
setFunds: (funds: Coin[]) => void;
setIsAssetListJsonValid: (isValid: boolean) => void;
direction?: 'row' | 'column';
};
export const AttachFundsRadio = ({
setFunds,
setIsAssetListJsonValid,
direction = 'row',
}: AttachFundsRadioProps) => {
const [selectedOptionKey, setSelectedOptionKey] =
useState<FundsOptionKey>('no_funds');
const [assetListJson, setAssetListJson] = useState(defaultAssetListJson);
const [selectedAssetsWithAmount, setSelectedAssetsWithAmount] = useState<
SelectedAssetWithAmount[]
>([defaultSelectedAsset]);
const fundsOptionsMap: Record<FundsOptionKey, FundsOption> = useMemo(() => {
return {
no_funds: {
key: 'no_funds',
label: 'No funds attached',
icon: <TbCurrencyDollarOff size="22px" />,
content: null,
},
select_assets: {
key: 'select_assets',
label: 'Select assets',
icon: <LuListPlus size="22px" />,
content: (
<SelectAssetContent
selectedAssetsWithAmount={selectedAssetsWithAmount}
setSelectedAssetsWithAmount={setSelectedAssetsWithAmount}
/>
),
},
json_asset_list: {
key: 'json_asset_list',
label: 'JSON asset list',
icon: <VscJson size="22px" />,
content: (
<JsonInput
value={assetListJson}
setValue={setAssetListJson}
minLines={14}
height="292px"
/>
),
},
};
}, [selectedAssetsWithAmount, assetListJson]);
useEffect(() => {
setIsAssetListJsonValid(true);
if (selectedOptionKey === 'no_funds') {
setFunds([]);
}
if (selectedOptionKey === 'select_assets') {
const funds = selectedAssetsWithAmount
.filter(({ asset, amount }) => asset && amount)
.map(({ asset, amount }) => ({
denom: asset!.base,
amount: BigNumber(amount)
.shiftedBy(getExponentFromAsset(asset!) ?? 6)
.toString(),
}));
setFunds(funds);
}
if (selectedOptionKey === 'json_asset_list') {
try {
const parsedJson = JSON.parse(assetListJson);
setFunds(parsedJson);
} catch (e) {
setFunds([]);
setIsAssetListJsonValid(false);
}
}
}, [selectedOptionKey, selectedAssetsWithAmount, assetListJson]);
const optionContent = fundsOptionsMap[selectedOptionKey].content;
return (
<Box
display="flex"
flexDirection="column"
gap={selectedOptionKey === 'json_asset_list' ? '10px' : '20px'}
>
<RadioGroup
direction={direction}
name="attach_funds"
value={selectedOptionKey}
onChange={(val) => {
setSelectedOptionKey(val as FundsOptionKey);
}}
>
{Object.values(fundsOptionsMap).map(({ key, label, icon }) => (
<Radio key={key} value={key} icon={icon}>
{label}
</Radio>
))}
</RadioGroup>
{optionContent}
</Box>
);
};