"use client"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; // ---- 时间配置 ---- export type ScheduleFreq = "hourly" | "daily" | "weekly" | "monthly"; export interface ScheduleConfig { freq: ScheduleFreq; minute: number; hour: number; weekday: number; date: number; } export const DEFAULT_SCHEDULE_CONFIG: ScheduleConfig = { freq: "daily", minute: 0, hour: 9, weekday: 1, date: 1, }; const WEEKDAYS = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]; const HOURS = Array.from({ length: 24 }, (_, i) => i); const MINUTES = [0, 15, 30, 45]; const DATES = Array.from({ length: 31 }, (_, i) => i + 1); export function buildCron(cfg: ScheduleConfig): string { // Keep the minute slot when the user picked one for daily/weekly/monthly too, // so "每天 9:30" doesn't silently truncate to 9:00 on save → reload round trips. const m = Number.isFinite(cfg.minute) ? cfg.minute : 0; switch (cfg.freq) { case "hourly": return `${m} * * * *`; case "daily": return `${m} ${cfg.hour} * * *`; case "weekly": return `${m} ${cfg.hour} * * ${cfg.weekday}`; case "monthly": return `${m} ${cfg.hour} ${cfg.date} * *`; } } // Parse a 5-field cron expression into the form picker state. // // Bug we keep getting bitten by: the edit dialog used to call // `parseCron(task.schedule_text)` and `schedule_text` may be the raw user // input (free-text like "每天 9 点") rather than a real cron string. The // fallback `DEFAULT_SCHEDULE_CONFIG` then made the picker silently // reinterpret the task. Callers should prefer `task.cron_expr` (the canonical // schedule produced by `_schedule_to_cron`); this function stays the single // source of truth for cron → picker, and also handles the looser step-syntax // variants (e.g. "0 */3 * * *") we sometimes generate. export function parseCron(cron: string | null | undefined): ScheduleConfig | null { if (!cron) return null; const parts = cron.trim().split(/\s+/); if (parts.length !== 5) return null; const [min, hr, date, , wd] = parts; const minute = parseInt(min ?? "", 10); const hour = parseInt(hr ?? "", 10); // Hourly: ``M * * * *`` — match before the daily branch so a minute-only // cron isn't misread as daily. if (hr === "*" && date === "*" && wd === "*") { if (!isNaN(minute)) { return { ...DEFAULT_SCHEDULE_CONFIG, freq: "hourly", minute }; } } // ``0 */N * * *`` (every N hours): map to hourly with that minute; the // picker doesn't model "every N hours" yet, so this is the safest fallback // that at least doesn't silently round to 9:00. if ((hr ?? "").startsWith("*/") && date === "*" && wd === "*") { if (!isNaN(minute)) { return { ...DEFAULT_SCHEDULE_CONFIG, freq: "hourly", minute }; } } if (date === "*" && wd === "*") { if (!isNaN(hour)) { return { ...DEFAULT_SCHEDULE_CONFIG, freq: "daily", hour, minute: isNaN(minute) ? 0 : minute, }; } } if (date === "*" && wd !== "*") { const weekday = parseInt(wd ?? "", 10); if (!isNaN(hour) && !isNaN(weekday)) { return { ...DEFAULT_SCHEDULE_CONFIG, freq: "weekly", hour, weekday, minute: isNaN(minute) ? 0 : minute, }; } } if (wd === "*" && date !== "*") { const d = parseInt(date ?? "", 10); if (!isNaN(hour) && !isNaN(d)) { return { ...DEFAULT_SCHEDULE_CONFIG, freq: "monthly", hour, date: d, minute: isNaN(minute) ? 0 : minute, }; } } return null; } export function ScheduleBuilder({ value, onChange, }: { value: ScheduleConfig; onChange: (cfg: ScheduleConfig) => void; }) { const set = (patch: Partial) => onChange({ ...value, ...patch }); return (
{value.freq === "hourly" && ( )} {value.freq === "weekly" && ( )} {value.freq === "monthly" && ( )} {value.freq !== "hourly" && ( )}
); } export function statusLabel(status?: string | null) { switch (status) { case "succeeded": return "成功"; case "failed": return "失败"; case "running": return "执行中"; case "pending": return "等待中"; default: return status || "未知"; } } export function statusVariant( status?: string | null, ): "default" | "secondary" | "destructive" | "outline" { if (status === "succeeded") return "default"; if (status === "failed") return "destructive"; if (status === "running") return "secondary"; return "outline"; }