-
Notifications
You must be signed in to change notification settings - Fork 85
/
index.tsx
208 lines (186 loc) · 6.4 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import React, { useCallback, useEffect, useMemo, useState } from 'react'
import { localNumberFormatter, shannonToCKBFormatter } from 'utils'
import RingProgressBar from 'widgets/RingProgressBar'
import TextField from 'widgets/TextField'
import { ReactComponent as Change } from 'widgets/Icons/Change.svg'
import DropdownWithCustomRender from 'widgets/DropdownWithCustomRender'
import useGetCountDownAndFeeRateStats from 'utils/hooks/useGetCountDownAndFeeRateStats'
import { useTranslation } from 'react-i18next'
import { appState, useState as useGlobalState } from 'states'
import { FeeRateValueArrayItemType, useGetBatchGeneratedTx } from 'components/Send/hooks'
import { batchGenerateExperimental } from 'components/SUDTSend/hooks'
import Tooltip from 'widgets/Tooltip'
import styles from './pricePanel.module.scss'
interface PricePanelProps {
field: string
price: string
onPriceChange: (value: string) => void
isExperimental?: boolean
}
enum PriceTypeEnum {
Custom = 'custom',
Standard = 'standard',
}
const DEFAULT_PRICE_ARRAY = ['1000', '2000', '3000']
const DEFAULT_COUNT_DOWN = 30
const PricePanel: React.FunctionComponent<PricePanelProps> = ({
price,
field,
onPriceChange,
isExperimental,
}: PricePanelProps) => {
const [t] = useTranslation()
const [type, setType] = useState<PriceTypeEnum>(PriceTypeEnum.Standard)
const [feeRateValueArray, setFeeRateValueArray] = useState<FeeRateValueArrayItemType[]>([])
const [priceArray, setPriceArray] = useState(DEFAULT_PRICE_ARRAY)
const [currentModeIdx, setCurrentModeIdx] = useState<number>(1)
const {
app: { send = appState.send },
wallet: { id: walletID = '' },
experimental,
} = useGlobalState()
const { countDown, suggestFeeRate } = useGetCountDownAndFeeRateStats({ seconds: DEFAULT_COUNT_DOWN })
const isStandard = type === PriceTypeEnum.Standard
const label = isStandard ? t('price-switch.price') : t('price-switch.customPrice')
const percent = (countDown / DEFAULT_COUNT_DOWN) * 100
const handleDropdownChange = useCallback(
(e: { value: string; index?: number }) => {
const { value: dropdownChangedValue, index = 1 } = e
setCurrentModeIdx(index)
onPriceChange?.(dropdownChangedValue)
},
[setCurrentModeIdx, onPriceChange]
)
const dropdownValue = useMemo(() => priceArray[currentModeIdx], [priceArray, currentModeIdx])
const handleInputChange = useCallback(
(e: React.SyntheticEvent<HTMLInputElement>) => {
const { value: inputChangedValue } = e.currentTarget
onPriceChange?.(inputChangedValue)
},
[onPriceChange]
)
const inputError = useMemo(
() => (Number(price) < 1000 ? t('price-switch.errorTip', { minPrice: 1000 }) : null),
[price]
)
const feeValuesArray = useMemo(
() =>
feeRateValueArray?.map((item: FeeRateValueArrayItemType) => ({
feeRateValue: item.feeRateValue,
value: shannonToCKBFormatter(item.feeValue),
})),
[feeRateValueArray]
)
useEffect(() => {
if (isStandard) {
handleDropdownChange({ value: priceArray[currentModeIdx], index: currentModeIdx })
}
}, [isStandard, handleDropdownChange, priceArray])
const options = useMemo(
() =>
[
['slow', 'blue'],
['standard', 'green'],
['fast', 'red'],
].map(([mode, color], idx) => ({
idx,
value: priceArray[idx],
label: (
<div className={styles.labelWrap}>
<span>{t(`price-switch.${mode}`)}</span>
<span className={`${styles.labelComment} ${styles[color]}`}>
{t('price-switch.priceColumn', {
priceValue: feeValuesArray?.[idx]?.feeRateValue || priceArray[idx],
feeValue: feeValuesArray?.[idx]?.value || 0,
})}
</span>
</div>
),
})),
[priceArray, feeValuesArray]
)
useEffect(() => {
if (isExperimental && experimental) {
batchGenerateExperimental(experimental, priceArray).then(res => {
setFeeRateValueArray(res)
})
} else {
useGetBatchGeneratedTx({ walletID, items: send.outputs, priceArray, isSendMax: send.isSendMax }).then(res => {
setFeeRateValueArray(res)
})
}
}, [
send.outputs,
send.isSendMax,
priceArray,
isExperimental,
experimental,
batchGenerateExperimental,
setFeeRateValueArray,
])
useEffect(() => {
if (suggestFeeRate === 0) {
setPriceArray(['1000', '2000', '3000'])
} else {
setPriceArray(['1000', `${suggestFeeRate}`, `${Number(suggestFeeRate) * 2 - 1000}`])
}
}, [suggestFeeRate])
const inputHint = t('price-switch.hintTip', { suggestFeeRate })
return (
<div>
<div className={styles.pricePanel}>
<div className={styles.transferSwitch}>
<label htmlFor={field} aria-label={label} title={label}>
{label}
</label>
<Tooltip
tip={<>{isStandard ? t('price-switch.switchToCustomPrice') : t('price-switch.switchToPrice')}</>}
placement="top"
showTriangle
>
<button
className={styles.transferWrap}
onClick={() =>
setType(currentType =>
currentType === PriceTypeEnum.Standard ? PriceTypeEnum.Custom : PriceTypeEnum.Standard
)
}
onKeyDown={() => {}}
type="button"
>
<Change />
</button>
</Tooltip>
</div>
{isStandard ? (
<div className={styles.timeoutWrap}>
<RingProgressBar percents={percent} color="#00C891" strokeWidth="3px" size="16px" backgroundColor="#CCC" />
<span>{t('price-switch.countDownTip', { countDown })}</span>
</div>
) : null}
</div>
{isStandard ? (
<div className={styles.dropdownBox}>
<DropdownWithCustomRender
onChange={handleDropdownChange}
options={options}
placeholder={t('dropdown.placeholder')}
value={dropdownValue}
/>
</div>
) : (
<TextField
field="price"
value={localNumberFormatter(price)}
onChange={handleInputChange}
suffix="shannons/kB"
error={inputError}
hint={!inputError && inputHint ? inputHint : null}
width="100%"
/>
)}
</div>
)
}
PricePanel.displayName = 'PricePanel'
export default React.memo(PricePanel)