import { useRef } from 'react'; import type BigNumber from 'bignumber.js'; import { TextInput, type StyleProp, type ViewStyle } from 'react-native'; import { FlexView, useTheme, TokenButton, Shimmer, Text, UiUtil, Link } from '@reown/appkit-ui-react-native'; import styles from './styles'; import { NumberUtil, type SwapTokenWithBalance } from '@reown/appkit-common-react-native'; export interface SwapInputProps { token?: SwapTokenWithBalance; value?: string; gasPrice?: number; style?: StyleProp; loading?: boolean; loadingValues?: boolean; onTokenPress?: () => void; onMaxPress?: () => void; onChange?: (value: string) => void; balance?: BigNumber; marketValue?: number; editable?: boolean; autoFocus?: boolean; testID?: string; } const MINIMUM_USD_VALUE_TO_CONVERT = 0.00005; export function SwapInput({ token, value, style, loading, loadingValues, onTokenPress, onMaxPress, onChange, marketValue, editable, autoFocus, testID }: SwapInputProps) { const Theme = useTheme(); const valueInputRef = useRef(null); const isMarketValueGreaterThanZero = !!marketValue && NumberUtil.bigNumber(marketValue).isGreaterThan('0'); const maxAmount = UiUtil.formatNumberToLocalString(token?.quantity?.numeric, 3); const maxError = Number(value) > Number(token?.quantity?.numeric); const showMax = onMaxPress && !!token?.quantity?.numeric && NumberUtil.multiply(token?.quantity.numeric, token?.price).isGreaterThan( MINIMUM_USD_VALUE_TO_CONVERT ); const handleInputChange = (_value: string) => { const formattedValue = _value.replace(/,/g, '.'); if (Number(formattedValue) >= 0 || formattedValue === '') { onChange?.(formattedValue); } }; const handleMaxPress = () => { if (valueInputRef.current) { valueInputRef.current.blur(); } onMaxPress?.(); }; return ( {loading ? ( ) : ( <> {loadingValues ? ( ) : ( )} {loadingValues ? ( ) : showMax || isMarketValueGreaterThanZero ? ( {isMarketValueGreaterThanZero ? `~$${UiUtil.formatNumberToLocalString(marketValue, 6)}` : ''} {showMax ? ( {showMax ? maxAmount : ''} Max ) : null} ) : null} )} ); }