Files
Homelab-manager/web/src/components/ServerTaskTable.tsx
T
bobbanandClaude Sonnet 5.5 6e39430a1f Swedish interface: labels and short texts across every page
About 1,600 more headings, buttons, table columns, status words, placeholders and short messages are translated. Tables of words built when the app loads are now read when they are drawn, so they follow the language, and a test fails if text is translated at load time.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 20:16:39 +01:00

208 lines
7.4 KiB
TypeScript

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<string, string> = {
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<keyof TaskRecord | undefined>(undefined);
const [sortDir, setSortDir] = useState<SortDirection>("asc");
function requestSort(key: keyof TaskRecord) {
if (sortKey === key) {
setSortDir((d) => (d === "asc" ? "desc" : "asc"));
} else {
setSortKey(key);
setSortDir("asc");
}
}
const byType = new Map<string, TaskRecord[]>();
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 && (
<div className="card-body py-2 d-flex justify-content-end">
<button className="btn btn-outline-secondary btn-sm" onClick={exportCsv}>
{t("Export CSV")}
</button>
</div>
)}
{[...byType.entries()].map(([scheduleType, groupTasks]) => (
<div key={scheduleType}>
<div className="card-body py-2 bg-secondary-lt">
<strong>{SCHEDULE_TYPE_LABELS[scheduleType] ?? scheduleType}</strong>
</div>
<div className="table-responsive">
<table className="table table-vcenter card-table mb-0">
<thead>
<tr>
<SortableTh<TaskRecord> label={t("Name")} sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<TaskRecord>
label={t("Command")}
sortKeyName="command"
activeKey={sortKey}
direction={sortDir}
onSort={requestSort}
/>
<SortableTh<TaskRecord>
label={t("Schedule")}
sortKeyName="scheduleExpression"
activeKey={sortKey}
direction={sortDir}
onSort={requestSort}
/>
<SortableTh<TaskRecord>
label={t("Next run")}
sortKeyName="nextRunAt"
activeKey={sortKey}
direction={sortDir}
onSort={requestSort}
/>
<SortableTh<TaskRecord>
label={t("Status")}
sortKeyName="enabled"
activeKey={sortKey}
direction={sortDir}
onSort={requestSort}
/>
{canEditTasks && <th className="w-1">{t("Actions")}</th>}
</tr>
</thead>
<tbody>
{sortItems(groupTasks, sortKey, sortDir).map((task) => {
const nextRun = computeNextRun(task);
return (
<tr key={task.id}>
<td>{task.name}</td>
<td className="text-secondary">{task.command ?? "—"}</td>
<td>
<div>{describeSchedule(task)}</div>
{task.scheduleExpression && <span className="text-secondary small">{task.scheduleExpression}</span>}
</td>
<td>
{nextRun ? (
<span
title={
nextRun.estimated
? t("Estimated from the cron expression in your browser's timezone — the server may run in a different timezone")
: undefined
}
>
{nextRun.estimated ? "~" : ""}
{formatDateTime(nextRun.date)}
</span>
) : (
"—"
)}
</td>
<td>
{task.isStale ? (
<span className="badge bg-red-lt text-red me-1">{t("Missing")}</span>
) : task.enabled ? (
<span className="badge bg-green-lt text-green me-1">{t("Enabled")}</span>
) : (
<span className="badge bg-secondary-lt text-secondary me-1">{t("Disabled")}</span>
)}
{task.origin === "manual" && <span className="badge bg-blue-lt">{t("Manual")}</span>}
</td>
{canEditTasks && (
<td>
{task.origin === "manual" && (
<div className="btn-list flex-nowrap">
<button className="btn btn-sm" onClick={() => onEdit(task)}>
{t("Edit")}
</button>
<button className="btn btn-sm btn-outline-danger" onClick={() => onDelete(task)}>
{t("Delete")}
</button>
</div>
)}
</td>
)}
</tr>
);
})}
</tbody>
</table>
</div>
</div>
))}
</>
);
}