import { cn, pressable, text } from '@/styles/theme'; import { useCallback, useMemo } from 'react'; import { useIcon } from '../../hooks/useIcon'; import { formatFiatAmount } from '../../utils/formatFiatAmount'; import { truncateDecimalPlaces } from '../../utils/truncateDecimalPlaces'; import { Skeleton } from '../Skeleton'; type SelectedInputType = 'fiat' | 'crypto'; type AmountInputTypeSwitchProps = { selectedInputType: SelectedInputType; setSelectedInputType: (type: SelectedInputType) => void; asset: string; fiatAmount: string; cryptoAmount: string; exchangeRate: number; exchangeRateLoading: boolean; loadingDisplay?: React.ReactNode; currency: string; className?: string; }; export function AmountInputTypeSwitch({ selectedInputType, setSelectedInputType, asset, fiatAmount, cryptoAmount, exchangeRate, exchangeRateLoading, currency, loadingDisplay = , className, }: AmountInputTypeSwitchProps) { const iconSvg = useIcon({ icon: 'toggle' }); const handleToggle = useCallback(() => { setSelectedInputType(selectedInputType === 'fiat' ? 'crypto' : 'fiat'); }, [selectedInputType, setSelectedInputType]); const formatCrypto = useCallback( (amount: string) => { return `${truncateDecimalPlaces(amount || '0', 8)} ${asset}`; }, [asset], ); const amountLine = useMemo(() => { return ( {selectedInputType === 'fiat' ? formatCrypto(cryptoAmount) : formatFiatAmount({ amount: fiatAmount, currency: currency, minimumFractionDigits: 0, })} ); }, [cryptoAmount, fiatAmount, selectedInputType, formatCrypto, currency]); if (exchangeRateLoading || !exchangeRate) { return loadingDisplay; } return (
{amountLine}
); }