|
| 1 | +import React, { |
| 2 | + ReactElement, |
| 3 | + memo, |
| 4 | + useCallback, |
| 5 | + useMemo, |
| 6 | + useState, |
| 7 | + useEffect, |
| 8 | +} from 'react'; |
| 9 | +import { StyleSheet, View } from 'react-native'; |
| 10 | +import { useSelector } from 'react-redux'; |
| 11 | +import { useTranslation } from 'react-i18next'; |
| 12 | + |
| 13 | +import LNURLWNumberpad from './LNURLWNumberpad'; |
| 14 | +import { TouchableOpacity } from '../../../styles/components'; |
| 15 | +import { Caption13Up, Text02B } from '../../../styles/text'; |
| 16 | +import { SwitchIcon } from '../../../styles/icons'; |
| 17 | +import { IColors } from '../../../styles/colors'; |
| 18 | +import GradientView from '../../../components/GradientView'; |
| 19 | +import BottomSheetNavigationHeader from '../../../components/BottomSheetNavigationHeader'; |
| 20 | +import SafeAreaInset from '../../../components/SafeAreaInset'; |
| 21 | +import Money from '../../../components/Money'; |
| 22 | +import NumberPadTextField from '../../../components/NumberPadTextField'; |
| 23 | +import Button from '../../../components/Button'; |
| 24 | +import { EBalanceUnit } from '../../../store/types/wallet'; |
| 25 | +import { sendMax } from '../../../utils/wallet/transactions'; |
| 26 | +import { |
| 27 | + selectedNetworkSelector, |
| 28 | + selectedWalletSelector, |
| 29 | +} from '../../../store/reselect/wallet'; |
| 30 | +import { balanceUnitSelector } from '../../../store/reselect/settings'; |
| 31 | +import { useSwitchUnit } from '../../../hooks/wallet'; |
| 32 | +import { useCurrency } from '../../../hooks/displayValues'; |
| 33 | +import { getNumberPadText } from '../../../utils/numberpad'; |
| 34 | +import { convertToSats } from '../../../utils/conversion'; |
| 35 | +import type { LNURLWithdrawProps } from '../../../navigation/types'; |
| 36 | + |
| 37 | +const Amount = ({ |
| 38 | + navigation, |
| 39 | + route, |
| 40 | +}: LNURLWithdrawProps<'Amount'>): ReactElement => { |
| 41 | + const { t } = useTranslation('wallet'); |
| 42 | + const { wParams } = route.params; |
| 43 | + const { minWithdrawable, maxWithdrawable } = wParams; |
| 44 | + const { fiatTicker } = useCurrency(); |
| 45 | + const [nextUnit, switchUnit] = useSwitchUnit(); |
| 46 | + const selectedWallet = useSelector(selectedWalletSelector); |
| 47 | + const selectedNetwork = useSelector(selectedNetworkSelector); |
| 48 | + const unit = useSelector(balanceUnitSelector); |
| 49 | + const [text, setText] = useState(''); |
| 50 | + const [error, setError] = useState(false); |
| 51 | + |
| 52 | + // Set initial text for NumberPadTextField |
| 53 | + useEffect(() => { |
| 54 | + const result = getNumberPadText(minWithdrawable, unit); |
| 55 | + setText(result); |
| 56 | + }, [selectedWallet, selectedNetwork, minWithdrawable, unit]); |
| 57 | + |
| 58 | + const amount = useMemo((): number => { |
| 59 | + return convertToSats(text, unit); |
| 60 | + }, [text, unit]); |
| 61 | + |
| 62 | + const maxWithdrawableProps = { |
| 63 | + ...(unit !== EBalanceUnit.fiat ? { symbol: true } : { showFiat: true }), |
| 64 | + ...(error && { color: 'brand' as keyof IColors }), |
| 65 | + }; |
| 66 | + |
| 67 | + const isMaxSendAmount = amount === maxWithdrawable; |
| 68 | + |
| 69 | + const onChangeUnit = (): void => { |
| 70 | + const result = getNumberPadText(amount, nextUnit); |
| 71 | + setText(result); |
| 72 | + switchUnit(); |
| 73 | + }; |
| 74 | + |
| 75 | + const onMaxAmount = useCallback((): void => { |
| 76 | + const result = getNumberPadText(maxWithdrawable, unit); |
| 77 | + setText(result); |
| 78 | + sendMax({ selectedWallet, selectedNetwork }); |
| 79 | + }, [maxWithdrawable, unit, selectedWallet, selectedNetwork]); |
| 80 | + |
| 81 | + const onError = (): void => { |
| 82 | + setError(true); |
| 83 | + setTimeout(() => setError(false), 500); |
| 84 | + }; |
| 85 | + |
| 86 | + const isValid = amount >= minWithdrawable && amount <= maxWithdrawable; |
| 87 | + |
| 88 | + return ( |
| 89 | + <GradientView style={styles.container}> |
| 90 | + <BottomSheetNavigationHeader title={t('lnurl_w_title')} /> |
| 91 | + <View style={styles.content}> |
| 92 | + <NumberPadTextField value={text} testID="SendNumberField" /> |
| 93 | + |
| 94 | + <View style={styles.numberPad} testID="SendAmountNumberPad"> |
| 95 | + <View style={styles.actions}> |
| 96 | + <View> |
| 97 | + <Caption13Up style={styles.maxWithdrawableText} color="gray1"> |
| 98 | + {t('lnurl_w_max')} |
| 99 | + </Caption13Up> |
| 100 | + <Money |
| 101 | + key="small" |
| 102 | + sats={maxWithdrawable} |
| 103 | + size="text02m" |
| 104 | + decimalLength="long" |
| 105 | + testID="maxWithdrawable" |
| 106 | + {...maxWithdrawableProps} |
| 107 | + /> |
| 108 | + </View> |
| 109 | + <View style={styles.actionButtons}> |
| 110 | + <View style={styles.actionButtonContainer}> |
| 111 | + <TouchableOpacity |
| 112 | + style={styles.actionButton} |
| 113 | + color="white08" |
| 114 | + testID="SendNumberPadMax" |
| 115 | + onPress={onMaxAmount}> |
| 116 | + <Text02B |
| 117 | + size="12px" |
| 118 | + color={isMaxSendAmount ? 'orange' : 'brand'}> |
| 119 | + {t('send_max')} |
| 120 | + </Text02B> |
| 121 | + </TouchableOpacity> |
| 122 | + </View> |
| 123 | + |
| 124 | + <View style={styles.actionButtonContainer}> |
| 125 | + <TouchableOpacity |
| 126 | + style={styles.actionButton} |
| 127 | + color="white08" |
| 128 | + onPress={onChangeUnit} |
| 129 | + testID="SendNumberPadUnit"> |
| 130 | + <SwitchIcon color="brand" width={16.44} height={13.22} /> |
| 131 | + <Text02B |
| 132 | + style={styles.actionButtonText} |
| 133 | + size="12px" |
| 134 | + color="brand"> |
| 135 | + {nextUnit === 'BTC' && 'BTC'} |
| 136 | + {nextUnit === 'satoshi' && 'sats'} |
| 137 | + {nextUnit === 'fiat' && fiatTicker} |
| 138 | + </Text02B> |
| 139 | + </TouchableOpacity> |
| 140 | + </View> |
| 141 | + </View> |
| 142 | + </View> |
| 143 | + |
| 144 | + <LNURLWNumberpad |
| 145 | + value={text} |
| 146 | + maxAmount={maxWithdrawable} |
| 147 | + onChange={setText} |
| 148 | + onError={onError} |
| 149 | + /> |
| 150 | + </View> |
| 151 | + |
| 152 | + <View style={styles.buttonContainer}> |
| 153 | + <Button |
| 154 | + size="large" |
| 155 | + text={t('continue')} |
| 156 | + disabled={!isValid} |
| 157 | + testID="ContinueAmount" |
| 158 | + onPress={(): void => { |
| 159 | + navigation.navigate('Confirm', { amount, wParams }); |
| 160 | + }} |
| 161 | + /> |
| 162 | + </View> |
| 163 | + </View> |
| 164 | + <SafeAreaInset type="bottom" minPadding={16} /> |
| 165 | + </GradientView> |
| 166 | + ); |
| 167 | +}; |
| 168 | + |
| 169 | +const styles = StyleSheet.create({ |
| 170 | + container: { |
| 171 | + flex: 1, |
| 172 | + }, |
| 173 | + content: { |
| 174 | + flex: 1, |
| 175 | + paddingHorizontal: 16, |
| 176 | + }, |
| 177 | + numberPad: { |
| 178 | + flex: 1, |
| 179 | + marginTop: 'auto', |
| 180 | + maxHeight: 450, |
| 181 | + }, |
| 182 | + actions: { |
| 183 | + borderBottomColor: 'rgba(255, 255, 255, 0.1)', |
| 184 | + borderBottomWidth: 1, |
| 185 | + marginTop: 28, |
| 186 | + marginBottom: 5, |
| 187 | + paddingBottom: 16, |
| 188 | + flexDirection: 'row', |
| 189 | + justifyContent: 'space-between', |
| 190 | + alignItems: 'flex-end', |
| 191 | + }, |
| 192 | + maxWithdrawableText: { |
| 193 | + marginBottom: 5, |
| 194 | + }, |
| 195 | + actionButtons: { |
| 196 | + flexDirection: 'row', |
| 197 | + justifyContent: 'flex-end', |
| 198 | + marginLeft: 'auto', |
| 199 | + }, |
| 200 | + actionButtonContainer: { |
| 201 | + alignItems: 'center', |
| 202 | + }, |
| 203 | + actionButton: { |
| 204 | + marginLeft: 16, |
| 205 | + paddingVertical: 7, |
| 206 | + paddingHorizontal: 8, |
| 207 | + borderRadius: 8, |
| 208 | + flexDirection: 'row', |
| 209 | + alignItems: 'center', |
| 210 | + }, |
| 211 | + actionButtonText: { |
| 212 | + marginLeft: 11, |
| 213 | + }, |
| 214 | + buttonContainer: { |
| 215 | + justifyContent: 'flex-end', |
| 216 | + }, |
| 217 | +}); |
| 218 | + |
| 219 | +export default memo(Amount); |
0 commit comments