import clsx from 'clsx' import React from 'react' import { useUser } from 'web/hooks/use-user' import { formatMoney } from 'common/util/format' import { Col } from './layout/col' import { SiteLink } from './site-link' import { ENV_CONFIG } from 'common/envs/constants' import { useWindowSize } from 'web/hooks/use-window-size' export function AmountInput(props: { amount: number | undefined onChange: (newAmount: number | undefined) => void error: string | undefined label: string disabled?: boolean className?: string inputClassName?: string // Needed to focus the amount input inputRef?: React.MutableRefObject }) { const { amount, onChange, error, label, disabled, className, inputClassName, inputRef, } = props const onAmountChange = (str: string) => { const amount = parseInt(str.replace(/\D/g, '')) const isInvalid = !str || isNaN(amount) onChange(isInvalid ? undefined : amount) } const { width } = useWindowSize() const isMobile = (width ?? 0) < 768 return ( {error && (
{error === 'Insufficient balance' ? ( <> Not enough funds. Buy more? ) : ( error )}
)} ) } export function BuyAmountInput(props: { amount: number | undefined onChange: (newAmount: number | undefined) => void error: string | undefined setError: (error: string | undefined) => void minimumAmount?: number disabled?: boolean showSliderOnMobile?: boolean className?: string inputClassName?: string // Needed to focus the amount input inputRef?: React.MutableRefObject }) { const { amount, onChange, error, setError, showSliderOnMobile: showSlider, disabled, className, inputClassName, minimumAmount, inputRef, } = props const user = useUser() const onAmountChange = (amount: number | undefined) => { onChange(amount) // Check for errors. if (amount !== undefined) { if (user && user.balance < amount) { setError('Insufficient balance') } else if (minimumAmount && amount < minimumAmount) { setError('Minimum amount: ' + formatMoney(minimumAmount)) } else { setError(undefined) } } else { setError(undefined) } } return ( <> {showSlider && ( onAmountChange(parseInt(e.target.value))} className="range range-lg z-40 mb-2 xl:hidden" step="25" /> )} ) }