import { useState } from 'react' import { useTranslation } from 'react-i18next' import { toast } from 'sonner' import { getCurrencyDisplay, getCurrencyLabel } from '@/lib/currency' import { formatQuota, parseQuotaFromDollars } from '@/lib/format' import { cn } from '@/lib/utils' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { adjustUserQuota } from '../api' import type { QuotaAdjustMode } from '../types' interface UserQuotaDialogProps { open: boolean onOpenChange: (open: boolean) => void userId: number currentQuota: number onSuccess: () => void } export function UserQuotaDialog(props: UserQuotaDialogProps) { const { t } = useTranslation() const [mode, setMode] = useState('add') const [amount, setAmount] = useState('') const [loading, setLoading] = useState(false) const { config: currencyConfig, meta: currencyMeta } = getCurrencyDisplay() const currencyLabel = getCurrencyLabel() const tokensOnly = !currencyConfig.displayInCurrency || currencyMeta.kind === 'tokens' const amountValue = parseFloat(amount) || 0 const quotaValue = parseQuotaFromDollars(Math.abs(amountValue)) const getPreviewText = () => { const current = props.currentQuota const val = quotaValue switch (mode) { case 'add': return `${t('Current quota')}: ${formatQuota(current)} +${formatQuota(val)} = ${formatQuota(current + val)}` case 'subtract': return `${t('Current quota')}: ${formatQuota(current)} -${formatQuota(val)} = ${formatQuota(current - val)}` case 'override': { const overrideQuota = parseQuotaFromDollars(amountValue) return `${t('Current quota')}: ${formatQuota(current)} → ${formatQuota(overrideQuota)}` } default: return '' } } const handleConfirm = async () => { if (!amount && mode !== 'override') return if (quotaValue <= 0 && mode !== 'override') return setLoading(true) try { const value = mode === 'override' ? parseQuotaFromDollars(amountValue) : quotaValue const result = await adjustUserQuota({ id: props.userId, action: 'add_quota', mode, value: mode === 'override' ? value : Math.abs(value), }) if (result.success) { toast.success(t('Quota adjusted successfully')) setAmount('') setMode('add') props.onOpenChange(false) props.onSuccess() } else { toast.error(result.message || t('Failed to adjust quota')) } } catch (e: unknown) { toast.error(e instanceof Error ? e.message : t('Failed to adjust quota')) } finally { setLoading(false) } } const handleCancel = () => { setAmount('') setMode('add') props.onOpenChange(false) } const placeholder = tokensOnly ? t('Enter amount in tokens') : t('Enter amount in {{currency}}', { currency: currencyLabel }) return ( {t('Adjust Quota')} {t('Select an operation mode and enter the amount')}
{getPreviewText()}
{(['add', 'subtract', 'override'] as const).map((m) => ( ))}
setAmount(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') handleConfirm() }} />
) }