import { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Textarea } from '@/components/ui/textarea'; import { Label } from '@/components/ui/label'; import { Badge } from '@/components/ui/badge'; import { toast } from 'sonner'; import { cronJobApi, scriptApi } from '@/api'; import type { JsonData } from '@/types/common'; import i18n from '@/i18n'; // 定时任务类型定义 // Logan.Li const jobTypes = [ { value: 'shell', label: 'Shell', desc: i18n.t('关联脚本库中的 Shell 脚本') }, { value: 'python', label: 'Python', desc: i18n.t('关联脚本库中的 Python 脚本') }, { value: 'powershell', label: 'PowerShell', desc: i18n.t('关联脚本库中的 PowerShell 脚本') }, { value: 'command', label: i18n.t('自定义命令'), desc: i18n.t('手动输入命令或脚本路径') }, ]; const scriptJobTypes = ['shell', 'python', 'powershell']; function scriptTypeLabel(type: string) { if (type === 'python') return 'Python'; if (type === 'powershell') return 'PowerShell'; return 'Shell'; } export function CronJobFormDialog({ open, onOpenChange, editJob, onSuccess, }: { open: boolean; onOpenChange: (v: boolean) => void; editJob: JsonData | null; onSuccess: () => void; }) { const [form, setForm] = useState({ name: '', description: '', cron_expr: '', type: 'shell', content: '', script_id: null as number | null, }); const [scripts, setScripts] = useState([]); const [selectedScript, setSelectedScript] = useState(null); const [submitting, setSubmitting] = useState(false); // 弹窗打开时:加载脚本库 + 初始化表单 // Logan.Li useEffect(() => { if (!open) return; // 拉取脚本库 scriptApi.getScripts({ page_size: 200 }).then((res: JsonData) => { setScripts(res?.list || []); }).catch(() => setScripts([])); if (editJob) { setForm({ name: editJob.name || '', description: editJob.description || '', cron_expr: editJob.cron_expr || '', type: editJob.type || 'shell', content: editJob.content || '', script_id: editJob.script_id || null, }); // 如果关联了脚本,拉取脚本详情 if (editJob.script_id) { scriptApi.getScript(editJob.script_id).then((res: JsonData) => { setSelectedScript(res); }).catch(() => setSelectedScript(null)); } else { setSelectedScript(null); } } else { setForm({ name: '', description: '', cron_expr: '', type: 'shell', content: '', script_id: null }); setSelectedScript(null); } }, [open, editJob]); // 按类型过滤脚本库 // Logan.Li const filteredScripts = scriptJobTypes.includes(form.type) ? scripts.filter(s => s.type === form.type) : []; // 选中脚本时更新关联 const handleSelectScript = (scriptId: string) => { const sc = scripts.find(s => String(s.id) === scriptId); if (sc) { setSelectedScript(sc); setForm(prev => ({ ...prev, script_id: sc.id, content: sc.content })); } }; // 切换类型时重置脚本关联 const handleTypeChange = (type: string) => { setForm(prev => ({ ...prev, type, script_id: null, content: type === 'command' ? prev.content : '', })); setSelectedScript(null); }; const handleSave = async () => { if (!form.name || !form.cron_expr) { toast.error(i18n.t('请填写任务名称和 Cron 表达式')); return; } // 脚本类型必须选关联脚本 // Logan.Li if (scriptJobTypes.includes(form.type) && !form.script_id) { toast.error(i18n.t('请填写任务名称和 Cron 表达式')); return; } try { setSubmitting(true); const payload: Record = { name: form.name, description: form.description, cron_expr: form.cron_expr, type: form.type, content: form.content, script_id: form.script_id, }; if (editJob) { await cronJobApi.updateJob(editJob.id, payload); toast.success(i18n.t('任务更新成功')); } else { await cronJobApi.createJob(payload); toast.success(i18n.t('任务更新成功')); } onOpenChange(false); onSuccess(); } catch (error) { console.error('Failed to save cron job:', error); toast.error(editJob ? i18n.t('kubernetes.deployment.imageUpdateFailed') : i18n.t('monitor.dashboard.custom.toastCreateFailed')); } finally { setSubmitting(false); } }; return ( {editJob ? i18n.t('automation.editCronJob') : i18n.t('编辑定时任务')} {i18n.t('task.cronjobformdial.s2e10ee70')}
{/* 任务名称 */}
setForm({ ...form, name: e.target.value })} placeholder={i18n.t('例如: 数据库备份')} className="bg-[var(--dg-bg-secondary)] border-[var(--dg-border)] text-[var(--dg-text-primary)] mt-1" />
{/* 描述 */}