import { useState } from "react"; import cronstrue from "cronstrue"; import { CronExpressionParser } from "cron-parser"; import type { TaskRecord } from "../api/client"; import { formatDateTime, is24HourFormat } from "../utils/date"; import { sortItems, type SortDirection } from "../hooks/useSortable"; import SortableTh from "./SortableTh"; import { downloadCsv } from "../utils/csv"; import { t } from "../i18n"; export const SCHEDULE_TYPE_LABELS: Record = { cron: "Cron jobs", systemd_timer: "systemd timers", windows_task: "Windows scheduled tasks", docker: "Docker jobs", backup: "Backups", update: "Updates", n8n_workflow: "n8n workflows", manual: "Other", }; function describeSchedule(task: TaskRecord): string { if (!task.scheduleExpression) return "—"; if (task.scheduleType === "cron") { try { return cronstrue.toString(task.scheduleExpression, { verbose: false, use24HourTimeFormat: is24HourFormat() }); } catch { return task.scheduleExpression; } } return task.scheduleExpression; } interface NextRun { date: Date; estimated: boolean; } function computeNextRun(task: TaskRecord): NextRun | null { if (task.nextRunAt) return { date: new Date(task.nextRunAt), estimated: false }; if (task.scheduleType !== "cron" || !task.scheduleExpression || task.isStale || !task.enabled) return null; try { const date = CronExpressionParser.parse(task.scheduleExpression).next().toDate(); return { date, estimated: true }; } catch { return null; } } export default function ServerTaskTable({ tasks, canEditTasks, onEdit, onDelete, }: { tasks: TaskRecord[]; canEditTasks: boolean; onEdit: (task: TaskRecord) => void; onDelete: (task: TaskRecord) => void; }) { const [sortKey, setSortKey] = useState(undefined); const [sortDir, setSortDir] = useState("asc"); function requestSort(key: keyof TaskRecord) { if (sortKey === key) { setSortDir((d) => (d === "asc" ? "desc" : "asc")); } else { setSortKey(key); setSortDir("asc"); } } const byType = new Map(); for (const task of tasks) { const list = byType.get(task.scheduleType) ?? []; list.push(task); byType.set(task.scheduleType, list); } function exportCsv() { downloadCsv( "tasks.csv", ["Schedule type", "Name", "Command", "Schedule", "Enabled", "Missing", "Origin"], tasks.map((t) => [ SCHEDULE_TYPE_LABELS[t.scheduleType] ?? t.scheduleType, t.name, t.command ?? "", t.scheduleExpression ?? "", t.enabled ? "yes" : "no", t.isStale ? "yes" : "no", t.origin, ]), ); } return ( <> {tasks.length > 0 && (
)} {[...byType.entries()].map(([scheduleType, groupTasks]) => (
{SCHEDULE_TYPE_LABELS[scheduleType] ?? scheduleType}
label={t("Name")} sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label={t("Command")} sortKeyName="command" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label={t("Schedule")} sortKeyName="scheduleExpression" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label={t("Next run")} sortKeyName="nextRunAt" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label={t("Status")} sortKeyName="enabled" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> {canEditTasks && } {sortItems(groupTasks, sortKey, sortDir).map((task) => { const nextRun = computeNextRun(task); return ( {canEditTasks && ( )} ); })}
{t("Actions")}
{task.name} {task.command ?? "—"}
{describeSchedule(task)}
{task.scheduleExpression && {task.scheduleExpression}}
{nextRun ? ( {nextRun.estimated ? "~" : ""} {formatDateTime(nextRun.date)} ) : ( "—" )} {task.isStale ? ( {t("Missing")} ) : task.enabled ? ( {t("Enabled")} ) : ( {t("Disabled")} )} {task.origin === "manual" && {t("Manual")}} {task.origin === "manual" && (
)}
))} ); }