From f60809d9b0ce4126e62ba58fc152a8b80fbe49f8 Mon Sep 17 00:00:00 2001 From: attacker Date: Tue, 16 Jun 2026 22:08:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8B=86=E5=88=86CronJobManager.ts?= =?UTF-8?q?x(652=E2=86=92398=E8=A1=8C)=E6=8F=90=E5=8F=96CronJobFormDialog+?= =?UTF-8?q?ExecutionHistoryDialog+cronJobHelpers=E5=AD=90=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../automation/CronJobFormDialog.tsx | 159 ++++++++++ .../components/automation/CronJobManager.tsx | 298 ++---------------- .../automation/ExecutionHistoryDialog.tsx | 117 +++++++ .../components/automation/cronJobHelpers.tsx | 47 +++ 4 files changed, 345 insertions(+), 276 deletions(-) create mode 100644 frontend/src/components/automation/CronJobFormDialog.tsx create mode 100644 frontend/src/components/automation/ExecutionHistoryDialog.tsx create mode 100644 frontend/src/components/automation/cronJobHelpers.tsx diff --git a/frontend/src/components/automation/CronJobFormDialog.tsx b/frontend/src/components/automation/CronJobFormDialog.tsx new file mode 100644 index 0000000..734a01e --- /dev/null +++ b/frontend/src/components/automation/CronJobFormDialog.tsx @@ -0,0 +1,159 @@ +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 { toast } from 'sonner'; +import { cronJobApi } from '@/services/api'; + +// 新建/编辑定时任务弹窗(自包含状态,editJob=null 表示新建模式) // Logan.Li +export function CronJobFormDialog({ + open, + onOpenChange, + editJob, + onSuccess, +}: { + open: boolean; + onOpenChange: (v: boolean) => void; + editJob: any | null; + onSuccess: () => void; +}) { + const [form, setForm] = useState({ + name: '', + description: '', + cron_expr: '', + type: 'command', + content: '', + }); + + // 弹窗打开时初始化表单:新建预填空值,编辑回填已有数据 // Logan.Li + useEffect(() => { + if (!open) return; + if (editJob) { + setForm({ + name: editJob.name || '', + description: editJob.description || '', + cron_expr: editJob.cron_expr || '', + type: editJob.type || 'command', + content: editJob.content || '', + }); + } else { + setForm({ name: '', description: '', cron_expr: '', type: 'command', content: '' }); + } + }, [open, editJob]); + + const handleSave = async () => { + if (!form.name || !form.cron_expr) { + toast.error('请填写任务名称和 Cron 表达式'); + return; + } + try { + const payload = { + name: form.name, + description: form.description, + cron_expr: form.cron_expr, + type: form.type, + content: form.content, + }; + if (editJob) { + await cronJobApi.updateJob(editJob.id, payload); + toast.success('任务更新成功'); + } else { + await cronJobApi.createJob(payload); + toast.success('任务创建成功'); + } + onOpenChange(false); + onSuccess(); + } catch (error) { + console.error('Failed to save cron job:', error); + toast.error(editJob ? '更新失败' : '创建失败'); + } + }; + + return ( + + + + {editJob ? '编辑定时任务' : '新建定时任务'} + 配置 Cron 定时任务 + +
+
+ + setForm({ ...form, name: e.target.value })} + placeholder="例如: 数据库备份" + className="bg-[var(--dg-bg-secondary)] border-[var(--dg-border)] text-[var(--dg-text-primary)] mt-1" + /> +
+
+ +