import { useState } from "react"; import { Link } from "react-router-dom"; import { api, type AlertItem, type CurrentUser, type MyWork } from "../api/client"; import AckDrawer from "../components/AckDrawer"; import EmptyState from "../components/EmptyState"; import { BlockSkeleton } from "../components/Skeleton"; import TimeAgo from "../components/TimeAgo"; import { useAlertsReport } from "../hooks/useAlertsReport"; import { useMyWork } from "../hooks/useMyWork"; import { formatRemaining } from "../utils/duration"; import { readableError } from "../utils/errors"; import { toast } from "../utils/toast"; const SEVERITY: Record = { critical: "bg-red-lt text-red", warning: "bg-yellow-lt text-yellow", info: "bg-blue-lt text-blue" }; type Win = MyWork["windows"][number]; /** * Everything with your name on it, in one place: the alerts you have taken or been handed, the acknowledgements about to run out, the maintenance you started that * is about to end, and the update runs you are following. What is about to run out is marked, and comes first. */ export default function MyWorkPage({ user }: { user: CurrentUser }) { const { work, reload } = useMyWork(); const { report, reload: reloadAlerts } = useAlertsReport(); const [acking, setAcking] = useState(null); const [busy, setBusy] = useState(null); const alertById = new Map((report?.alerts ?? []).map((a) => [a.id, a])); function changed() { reload(); reloadAlerts(); window.dispatchEvent(new Event("maintenance-changed")); } /** Minutes from now that a window should last to end `extra` minutes later than it does now (the server restarts the clock from now). */ const minutesFor = (w: Win, extra: number) => Math.min(7 * 24 * 60, Math.max(5, Math.ceil((Date.parse(w.endsAt) - Date.now()) / 60_000)) + extra); async function extend(w: Win, extra: number, words: string) { setBusy(w.id); try { await api.maintenance.start({ targetType: w.targetType as never, targetId: w.targetId, minutes: minutesFor(w, extra), reason: w.reason ?? undefined }); toast.success(`${w.targetName}: ${words} more.`); changed(); } catch (err) { toast.error(readableError(err)); } finally { setBusy(null); } } async function end(w: Win) { setBusy(w.id); try { await api.maintenance.end(w.id); toast.success(`Maintenance on ${w.targetName} ended.`, { label: "Undo", onClick: async () => { try { await api.maintenance.start({ targetType: w.targetType as never, targetId: w.targetId, minutes: minutesFor(w, 0), reason: w.reason ?? undefined }); toast.info("Put back."); } catch (err) { toast.error(readableError(err)); } finally { changed(); } }, }); changed(); } catch (err) { toast.error(readableError(err)); } finally { setBusy(null); } } const nothing = work !== null && work.alerts.length === 0 && work.windows.length === 0 && work.updateRuns.length === 0; return ( <>

My work

What has your name on it: alerts you acknowledged or were handed, maintenance you started and update runs you started. Whatever is about to run out is marked, and listed first.
{work === null && } {nothing && (
)} {work && work.alerts.length > 0 && (

Alerts you have

{work.alerts.map((a) => { const full = alertById.get(a.alertId); return ( ); })}
{a.severity} {a.link ? {a.message} : a.message}
{a.role === "handed" ? (a.ackedBy === (user.name ?? user.email ?? user.sub) ? "You took it" : `Handed to you by ${a.ackedBy}`) : "You acknowledged it"} {a.note ? ` — ${a.note}` : ""}
{a.until ? ( {a.endsSoon ? "Runs out " : "Until "} ) : ( until the alert is gone )}
{full && ( )} Alerts
)} {work && work.windows.length > 0 && (

Maintenance you started

Maintenance
{work.windows.map((w) => ( ))}
{w.targetName} {w.targetKind} {w.reason &&
{w.reason}
}
Ends in {formatRemaining(w.endsAt)}
)} {work && work.updateRuns.length > 0 && (

Update runs you started

Updates
{work.updateRuns.map((r) => ( ))}
{r.serverName} {r.templateName} {r.message && !r.running &&
{r.message}
}
{r.running ? ( Running ) : r.status === "done" ? ( Done ) : ( {r.status} )} {!r.running && r.updatesBefore !== null && r.updatesAfter !== null && ( {r.updatesBefore} → {r.updatesAfter} waiting{r.rebootRequired ? ", restart needed" : ""} )}
)} {acking && setAcking(null)} onSaved={changed} />} ); }