import clsx from 'clsx' import { useUser } from '../hooks/use-user' import { formatMoney } from '../lib/util/format' import { AddFundsButton } from './add-funds-button' import { Col } from './layout/col' import { Row } from './layout/row' export function AmountInput(props: { amount: number | undefined onChange: (newAmount: number | undefined) => void error: string | undefined setError: (error: string | undefined) => void minimumAmount?: number disabled?: boolean className?: string inputClassName?: string }) { const { amount, onChange, error, setError, disabled, className, inputClassName, minimumAmount, } = props const user = useUser() const onAmountChange = (str: string) => { const amount = parseInt(str.replace(/[^\d]/, '')) if (str && isNaN(amount)) return onChange(str ? amount : undefined) if (user && user.balance < amount) { setError('Insufficient balance') } else if (minimumAmount && amount < minimumAmount) { setError('Minimum amount: ' + formatMoney(minimumAmount)) } else { setError(undefined) } } const remainingBalance = Math.max(0, (user?.balance ?? 0) - (amount ?? 0)) return ( {error && (
{error}
)} {user && (
Remaining balance
{formatMoney(Math.floor(remainingBalance))}
{user.balance !== 1000 && }
)} ) }