Files
Homelab-manager/web/src/components/MaintenanceTimeline.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

166 lines
7.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useMemo, useState } from "react";
import type { MaintenanceWindow } from "../api/client";
import { formatDate, formatDateTime, formatTime } from "../utils/date";
import { t } from "../i18n";
const HOUR = 3600_000;
const DAY = 24 * HOUR;
const RANGES = [
{ key: "24h", label: "Next 24 hours", span: DAY, tickEvery: 3 * HOUR },
{ key: "7d", label: "Next 7 days", span: 7 * DAY, tickEvery: DAY },
{ key: "30d", label: "Next 30 days", span: 30 * DAY, tickEvery: 5 * DAY },
] as const;
interface Span {
from: number;
to: number;
}
/** Overlapping or touching spans folded into the stretches during which at least one is running. */
function union(spans: Span[]): Span[] {
const sorted = [...spans].sort((a, b) => a.from - b.from);
const out: Span[] = [];
for (const s of sorted) {
const last = out[out.length - 1];
if (last && s.from <= last.to) last.to = Math.max(last.to, s.to);
else out.push({ ...s });
}
return out;
}
/**
* The running and scheduled windows laid out along a time axis, one row per server, integration or DNS provider, plus a top row for
* "anything at all in maintenance". It shows what a list can't: which windows overlap, and where there are gaps. The tables further
* down the page hold the same windows for reading and for ending them, so this is a picture only.
*/
export default function MaintenanceTimeline({ windows, now }: { windows: MaintenanceWindow[]; now: number }) {
const [rangeKey, setRangeKey] = useState<(typeof RANGES)[number]["key"]>("7d");
const range = RANGES.find((r) => r.key === rangeKey)!;
const start = now;
const end = now + range.span;
const { rows, everything, further } = useMemo(() => {
const inRange = windows.filter((w) => Date.parse(w.endsAt) > start && Date.parse(w.startedAt) < end);
const byTarget = new Map<string, { name: string; kind: string; windows: MaintenanceWindow[] }>();
for (const w of inRange) {
const key = `${w.targetType}:${w.targetId}`;
const row = byTarget.get(key) ?? { name: w.targetName, kind: w.targetKind, windows: [] };
row.windows.push(w);
byTarget.set(key, row);
}
return {
rows: [...byTarget.values()].sort((a, b) => a.name.localeCompare(b.name)),
everything: union(inRange.map((w) => ({ from: Math.max(Date.parse(w.startedAt), start), to: Math.min(Date.parse(w.endsAt), end) }))),
further: windows.length - inRange.length,
};
}, [windows, start, end]);
const pct = (t: number) => ((Math.min(Math.max(t, start), end) - start) / range.span) * 100;
const bar = (from: number, to: number) => ({ left: `${pct(from)}%`, width: `${Math.max(pct(to) - pct(from), 0.6)}%` });
const ticks: number[] = [];
// First tick at the next whole step after now (the next midnight for day steps), so labels land on round times.
const stepStart = new Date(now);
if (range.tickEvery >= DAY) stepStart.setHours(24, 0, 0, 0);
else {
stepStart.setMinutes(0, 0, 0);
const hours = range.tickEvery / HOUR;
stepStart.setHours(Math.floor(stepStart.getHours() / hours) * hours);
}
for (let t = stepStart.getTime(); t < end; t += range.tickEvery) if (t > start) ticks.push(t);
const tickLabel = (t: number) => (range.tickEvery >= DAY ? formatDate(new Date(t)).replace(/^\d{4}-|\/\d{4}$/, "") : formatTime(new Date(t), false));
return (
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">{t("Timeline")}</h3>
<div className="card-actions">
<div className="btn-group btn-group-sm" role="group" aria-label={t("Time range")}>
{RANGES.map((r) => (
<button key={r.key} type="button" className={`btn btn-sm ${r.key === rangeKey ? "btn-primary" : "btn-outline-secondary"}`} aria-pressed={r.key === rangeKey} onClick={() => setRangeKey(r.key)}>
{r.label}
</button>
))}
</div>
</div>
</div>
<div className="card-body">
{rows.length === 0 ? (
<div className="text-secondary">Nothing running or scheduled in this time.{further > 0 ? ` ${further} further ahead.` : ""}</div>
) : (
<>
<div className="timeline-scroll" aria-hidden="true">
<div className="timeline">
<div className="timeline-row timeline-axis">
<div className="timeline-label text-secondary">{t("From now")}</div>
<div className="timeline-track">
{ticks.map((t) => (
<span key={t} className="timeline-tick" style={{ left: `${pct(t)}%` }}>
{tickLabel(t)}
</span>
))}
</div>
</div>
<div className="timeline-row timeline-union">
<div className="timeline-label fw-bold">{t("Anything")}</div>
<div className="timeline-track">
{ticks.map((t) => (
<span key={t} className="timeline-grid" style={{ left: `${pct(t)}%` }} />
))}
{everything.map((s, i) => (
<span key={i} className="timeline-bar timeline-bar-union" style={bar(s.from, s.to)} title={`${formatDateTime(new Date(s.from))} – ${formatDateTime(new Date(s.to))}`} />
))}
</div>
</div>
{rows.map((row) => (
<div key={row.name + row.kind} className="timeline-row">
<div className="timeline-label" title={`${row.name} · ${row.kind}`}>
{row.name}
</div>
<div className="timeline-track">
{ticks.map((t) => (
<span key={t} className="timeline-grid" style={{ left: `${pct(t)}%` }} />
))}
{row.windows.map((w) => {
const from = Date.parse(w.startedAt);
const to = Date.parse(w.endsAt);
const running = from <= now;
return (
<span
key={w.id}
className={`timeline-bar ${running ? "timeline-bar-running" : "timeline-bar-scheduled"} ${w.source?.startsWith("kuma:") ? "timeline-bar-mirrored" : ""} ${w.source?.startsWith("schedule:") ? "timeline-bar-recurring" : ""}`}
style={bar(from, to)}
title={`${row.name}: ${formatDateTime(new Date(from))} – ${formatDateTime(new Date(to))}${w.reason ? ` · ${w.reason}` : ""}`}
/>
);
})}
</div>
</div>
))}
</div>
</div>
<div className="text-secondary small mt-2 d-flex flex-wrap gap-3 align-items-center">
<span>
<span className="timeline-key timeline-bar-running" /> Running
</span>
<span>
<span className="timeline-key timeline-bar-scheduled" /> Scheduled
</span>
<span>
<span className="timeline-key timeline-bar-mirrored" /> From Uptime Kuma
</span>
<span>
<span className="timeline-key timeline-bar-recurring" /> Recurring
</span>
<span>{t("The same windows are listed in the tables below.")}</span>
{further > 0 && <span>{further} further ahead than this range.</span>}
</div>
</>
)}
</div>
</div>
);
}