import { useEffect, useState, type ReactNode } from "react"; import { Link, useSearchParams } from "react-router-dom"; import { api, type ChangeEntry, type ChangeKind } from "../api/client"; import { ErrorNotice } from "../components/Notice"; import ViewTabs from "../components/ViewTabs"; import AuditLogView from "../views/AuditLogView"; 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"; import { t } from "../i18n"; const TONE_DOT: Record = { good: "#2fb344", bad: "#e05252", info: "#4299e1", neutral: "#8a94a6" }; const RANGES: { days: number; label: string }[] = [ { days: 1, label: "Last 24 hours" }, { days: 7, label: "Last 7 days" }, { days: 30, get label() { return t("Last 30 days"); } }, { days: 90, label: "Last 90 days" }, { days: 400, get label() { return t("Everything kept"); } }, ]; const PAGE = 100; /** * What has changed, in one list: agents updated, settings changed, maintenance started, dependencies edited, alerts raised and cleared — each as a * sentence, newest first, grouped by day. Narrow it to a kind of change, a server, a stretch of time or a word. The Audit Log is the raw table behind it. */ const changeTabs = (view: "changes" | "audit") => ( ); export default function ChangeLog() { const [params] = useSearchParams(); if (params.get("view") === "audit") { return ( <>

{t("Change log")}

{changeTabs("audit")} ); } return ; } function ReadableChanges({ tabs }: { tabs: ReactNode }) { const [params, setParams] = useSearchParams(); const days = RANGES.some((r) => r.days === Number(params.get("days"))) ? Number(params.get("days")) : 7; const kind = params.get("kind") ?? ""; const serverId = /^\d+$/.test(params.get("serverId") ?? "") ? Number(params.get("serverId")) : undefined; const q = params.get("q") ?? ""; const [text, setText] = useState(q); const [entries, setEntries] = useState(null); const [more, setMore] = useState(false); const [kinds, setKinds] = useState<{ id: ChangeKind; label: string }[]>([]); const [servers, setServers] = useState<{ id: number; name: string }[]>([]); const [error, setError] = useState(null); const [loadingMore, setLoadingMore] = useState(false); 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 }, ); useEffect(() => { api.servers .list() .then((res) => setServers(res.servers.map((s) => ({ id: s.id, name: s.name })).sort((a, b) => a.name.localeCompare(b.name)))) .catch(() => {}); }, []); // What is searched for follows what is typed, a moment after it stops. useEffect(() => { const t = setTimeout(() => setParam("q", text.trim() || null), 300); return () => clearTimeout(t); // eslint-disable-next-line react-hooks/exhaustive-deps }, [text]); useEffect(() => { let alive = true; setEntries(null); api.changes .list({ days, kind, serverId, q, limit: PAGE }) .then((res) => { if (!alive) return; setEntries(res.entries); setMore(res.more); setKinds(res.kinds); setError(null); }) .catch((err) => alive && setError(readableError(err))); return () => { alive = false; }; }, [days, kind, serverId, q]); async function older() { if (!entries || entries.length === 0) return; setLoadingMore(true); try { const res = await api.changes.list({ days, kind, serverId, q, limit: PAGE, before: entries[entries.length - 1].at }); setEntries([...entries, ...res.entries]); setMore(res.more); } catch (err) { setError(readableError(err)); } finally { setLoadingMore(false); } } // Grouped by the day it happened (in this browser's time). const days_: { day: string; items: ChangeEntry[] }[] = []; for (const e of entries ?? []) { const day = formatDate(new Date(e.at)); const last = days_[days_.length - 1]; if (last && last.day === day) last.items.push(e); else days_.push({ day, items: [e] }); } function exportCsv() { downloadCsv( "change-log.csv", ["When", "Kind", "What", "Details", "By", "About"], (entries ?? []).map((e) => [e.at, kinds.find((k) => k.id === e.kind)?.label ?? e.kind, e.title, e.detail ?? "", e.actor ?? "", e.target?.name ?? ""]), ); } return ( <>

{t("Change log")}

{tabs}

What changed and when: agents updated, settings changed, maintenance started and ended, dependencies edited, alerts raised and cleared. The raw table behind it is the other tab.

setText(e.target.value)} />
{entries === null && !error && } {entries !== null && entries.length === 0 && (
{q || kind || serverId ? ( r.days === days)?.label.toLowerCase()} fits what is searched for. Try a wider range, or fewer filters.`} action={{ label: t("Clear the search and filters"), onClick: () => { setText(""); setParams(new URLSearchParams(), { replace: true }); } }} /> ) : ( r.days === days)?.label.toLowerCase()}. Try a wider range.`} action={days < 400 ? { label: t("Show everything kept"), onClick: () => setParam("days", "400") } : undefined} /> )}
)} {days_.map((group) => (

{group.day}

{group.items.map((e) => (
{formatTime(new Date(e.at), false)}
))}
))} {more && (
)} ); }