import { useEffect, useMemo, useState } from "react"; import { Link, useSearchParams } from "react-router-dom"; import { api, type Agenda, type AgendaItem, type AgendaKind, type CurrentUser } from "../api/client"; import { ErrorNotice } from "../components/Notice"; import { BlockSkeleton } from "../components/Skeleton"; import EmptyState from "../components/EmptyState"; import { downloadCsv } from "../utils/csv"; import { formatDate, formatTime } from "../utils/date"; import { readableError } from "../utils/errors"; const KINDS: { id: AgendaKind; label: string }[] = [ { id: "secret", label: "Secrets" }, { id: "certificate", label: "Certificates" }, { id: "domain", label: "Domains" }, { id: "tailscale", label: "Tailscale keys" }, { id: "warranty", label: "Warranties" }, { id: "support", label: "End of support" }, { id: "maintenance", label: "Maintenance" }, { id: "restore", label: "Restore tests" }, { id: "updates", label: "Updates waiting" }, { id: "todo", label: "To do" }, { id: "renewal", label: "Renewals" }, ]; const KIND_LABEL = Object.fromEntries(KINDS.map((k) => [k.id, k.label])) as Record; const RANGES: { days: number; label: string }[] = [ { days: 30, label: "Next 30 days" }, { days: 90, label: "Next 3 months" }, { days: 180, label: "Next 6 months" }, { days: 400, label: "Next year" }, ]; /** "today", "tomorrow", "in 5 days", "3 days ago". */ function relative(daysLeft: number): string { if (daysLeft === 0) return "today"; if (daysLeft === 1) return "tomorrow"; if (daysLeft === -1) return "yesterday"; return daysLeft > 0 ? `in ${daysLeft} days` : `${-daysLeft} days ago`; } /** A day as the app's date format writes it (the string is a plain day, so it is read as one, not shifted by the viewer's zone). */ const dayOf = (ymd: string) => { const [y, m, d] = ymd.split("-").map(Number); return new Date(y, m - 1, d); }; const weekday = (ymd: string) => dayOf(ymd).toLocaleDateString(undefined, { weekday: "short" }); interface Section { id: string; title: string; hint?: string; items: AgendaItem[]; } function sectionsOf(items: AgendaItem[]): Section[] { const now = items.filter((i) => i.daysLeft < 0 || i.kind === "updates"); const rest = items.filter((i) => !(i.daysLeft < 0 || i.kind === "updates")); return [ { id: "now", title: "Needs attention now", hint: "Already past its date, or waiting", items: now }, { id: "today", title: "Today", items: rest.filter((i) => i.daysLeft === 0) }, { id: "week", title: "This week", items: rest.filter((i) => i.daysLeft >= 1 && i.daysLeft <= 7) }, { id: "month", title: "The rest of the month", hint: "8 to 30 days away", items: rest.filter((i) => i.daysLeft >= 8 && i.daysLeft <= 30) }, { id: "later", title: "Further ahead", items: rest.filter((i) => i.daysLeft > 30) }, ].filter((s) => s.items.length > 0); } function When({ item }: { item: AgendaItem }) { if (item.allDay) { return ( <>
{weekday(item.at)} {formatDate(dayOf(item.at))}
{item.kind !== "updates" &&
{relative(item.daysLeft)}
} ); } const start = new Date(item.at); const end = item.endsAt ? new Date(item.endsAt) : null; return ( <>
{start.toLocaleDateString(undefined, { weekday: "short" })} {formatDate(start)}
{formatTime(start, false)} {end ? `–${formatTime(end, false)}` : ""} · {relative(item.daysLeft)}
); } /** * Everything with a date ahead in one list: secrets, domains and certificates that expire, Tailscale keys, warranties, end of support, planned and recurring * maintenance, restore tests that are due, and updates that have been waiting — nearest first. Narrow it by kind, by how far ahead, or by server. The same list * can be put in a phone's calendar (Settings → Share links → a calendar feed). */ export default function ComingUp({ user }: { user: CurrentUser }) { const [params, setParams] = useSearchParams(); const days = RANGES.some((r) => r.days === Number(params.get("days"))) ? Number(params.get("days")) : 90; const chosen = useMemo(() => new Set((params.get("kinds") ?? "").split(",").filter((k): k is AgendaKind => KINDS.some((x) => x.id === k))), [params]); const serverId = /^\d+$/.test(params.get("server") ?? "") ? Number(params.get("server")) : null; const [everyOccurrence, setEveryOccurrence] = useState(false); const [agenda, setAgenda] = useState(null); const [servers, setServers] = useState>(new Map()); const [error, setError] = useState(null); useEffect(() => { let alive = true; const load = () => api.agenda .get(400) .then((res) => alive && (setAgenda(res), setError(null))) .catch((err) => alive && setError(readableError(err))); load(); api.servers .list() .then((res) => alive && setServers(new Map(res.servers.map((s) => [s.id, s.name])))) .catch(() => {}); const t = setInterval(load, 5 * 60_000); return () => { alive = false; clearInterval(t); }; }, []); const setParam = (key: string, value: string | null) => setParams( (prev) => { const next = new URLSearchParams(prev); if (value) next.set(key, value); else next.delete(key); return next; }, { replace: true }, ); const toggleKind = (id: AgendaKind) => { const next = new Set(chosen); if (next.has(id)) next.delete(id); else next.add(id); setParam("kinds", [...next].join(",") || null); }; const withinRange = useMemo(() => (agenda?.items ?? []).filter((i) => i.kind === "updates" || i.daysLeft <= days), [agenda, days]); // A schedule that happens every week would take a line a week: by default only its next time is listed, and the rest can be asked for. const { inRange, hiddenRecurring } = useMemo(() => { if (everyOccurrence) return { inRange: withinRange, hiddenRecurring: 0 }; const seen = new Set(); let hidden = 0; const kept = withinRange.filter((i) => { const m = /^(schedule|plan):(\d+):/.exec(i.id); const schedule = m ? `${m[1]}:${m[2]}` : null; if (!schedule) return true; if (seen.has(schedule)) { hidden++; return false; } seen.add(schedule); return true; }); return { inRange: kept, hiddenRecurring: hidden }; }, [withinRange, everyOccurrence]); const counts = useMemo(() => { const out = new Map(); for (const i of inRange) out.set(i.kind, (out.get(i.kind) ?? 0) + 1); return out; }, [inRange]); const serversWithItems = useMemo(() => [...new Set(inRange.map((i) => i.serverId).filter((s): s is number => s !== null))].sort((a, b) => (servers.get(a) ?? "").localeCompare(servers.get(b) ?? "")), [inRange, servers]); const shown = inRange.filter((i) => (chosen.size === 0 || chosen.has(i.kind)) && (serverId === null || i.serverId === serverId)); const sections = sectionsOf(shown); const filtered = chosen.size > 0 || serverId !== null; const needNow = inRange.filter((i) => i.daysLeft < 0 && i.kind !== "updates").length; const thisWeek = inRange.filter((i) => i.daysLeft >= 0 && i.daysLeft <= 7 && i.kind !== "updates").length; const cards = [ { label: "Past their date", value: agenda ? String(needNow) : "…", color: needNow > 0 ? "text-red" : "text-green" }, { label: "In the next 7 days", value: agenda ? String(thisWeek) : "…", color: thisWeek > 0 ? "text-yellow" : "" }, { label: "In the list", value: agenda ? String(shown.length) : "…", color: "" }, ]; function exportCsv() { downloadCsv( "coming-up.csv", ["Date", "Ends", "What", "Kind", "Details", "Days from today", "Page"], shown.map((i) => [i.at, i.endsAt ?? "", i.title, KIND_LABEL[i.kind], i.detail ?? "", i.daysLeft, i.link]), ); } return ( <>

Coming up

What is about to need attention, nearest first: things that expire, hardware that ages out, planned maintenance, restore tests that are due. Each also raises its own alert when it gets close. {user.role === "admin" ? ( <> {" "} To have it in your phone’s calendar, make a calendar feed. ) : ( " An administrator can make a calendar feed of it for your phone." )}

{agenda && agenda.notes.length > 0 && (
Something couldn’t be read, so this list may be missing entries: {agenda.notes.join(" · ")}
)}
{cards.map((c) => (
{c.value}
{c.label}
))}
{serversWithItems.length > 0 && ( )} {(hiddenRecurring > 0 || everyOccurrence) && ( )} {filtered && ( )}
{KINDS.map((k) => { const n = counts.get(k.id) ?? 0; const on = chosen.has(k.id); return ( ); })}
{agenda === null && !error && } {agenda !== null && sections.length === 0 && (
{filtered ? ( setParams(new URLSearchParams(days === 90 ? {} : { days: String(days) }), { replace: true }) }} /> ) : ( )}
)} {sections.map((s) => (

{s.title}

{s.hint ?? ""}
{s.items.map((i) => (
{i.title} {i.serverId !== null && servers.has(i.serverId) && ( {" "} · {servers.get(i.serverId)} )}
{i.detail &&
{i.detail}
}
{KIND_LABEL[i.kind]}
))}
))} ); }