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(); 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 (

{t("Timeline")}

{RANGES.map((r) => ( ))}
{rows.length === 0 ? (
Nothing running or scheduled in this time.{further > 0 ? ` ${further} further ahead.` : ""}
) : ( <>
Running Scheduled From Uptime Kuma Recurring {t("The same windows are listed in the tables below.")} {further > 0 && {further} further ahead than this range.}
)}
); }