// 云账单预警规则弹窗(新建/编辑)— 自包含状态,从 CloudBilling 提取 // Logan.Li import { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Switch } from '@/components/ui/switch'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Loader2 } from 'lucide-react'; import { toast } from 'sonner'; import { billingApi } from '@/api'; import { providerLabels } from './CloudBillingHelpers'; import type { JsonData } from '@/types/common'; import { useTranslation } from 'react-i18next'; const emptyRule = { name: '', provider: '', metric: 'daily', threshold: '', operator: '>', enabled: true }; interface Props { open: boolean; onOpenChange: (open: boolean) => void; editingRule: JsonData | null; onSuccess: () => void; } export function BillingRuleDialog({ open, onOpenChange, editingRule, onSuccess }: Props) { const { t } = useTranslation(); const [form, setForm] = useState(emptyRule); const [submitting, setSubmitting] = useState(false); // 弹窗打开时初始化表单 // Logan.Li useEffect(() => { if (!open) return; if (editingRule) { setForm({ name: editingRule.name, provider: editingRule.provider || '', metric: editingRule.metric || 'daily', threshold: editingRule.threshold, operator: editingRule.operator || '>', enabled: editingRule.enabled, }); } else { setForm({ ...emptyRule }); } }, [open, editingRule]); const handleSave = async () => { if (!form.name || !form.threshold) { toast.error(t('cloud.billingruledialog.sd36855')); return; } setSubmitting(true); try { const data = { ...form, threshold: parseFloat(form.threshold) }; if (editingRule) { await billingApi.updateAlertRule(editingRule.id, data); } else { await billingApi.createAlertRule(data); } onOpenChange(false); onSuccess(); toast(t('common.savedSuccessfully')); } catch (e) { toast.error(t('common.saveFailed'), { description: (e as JsonData).message }); } finally { setSubmitting(false); } }; return ( {editingRule ? t('cloud.billingruledialog.sb8cfa0') : t('cloud.billingruledialog.sd58c54')} {t('cloud.billingruledialog.s75227f')}
setForm({ ...form, name: e.target.value })} />
setForm({ ...form, threshold: e.target.value })} />
setForm({ ...form, enabled: v })} />
); }