A small translation system (the English text is its own key, so anything without a Swedish entry stays English) and a Language switch in the user menu, saved on the account (users.language) and following the browser when no choice has been made. Swedish covers the menu, shared parts (search, tables, dialogs, relative times), the sign-in page and the everyday pages: Dashboard, Alerts, Servers, Updates, Maintenance, My work, Coming up, To do, Integrations and the display settings. Server-written messages stay English for now. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
261 lines
11 KiB
TypeScript
261 lines
11 KiB
TypeScript
import { useCallback, useEffect, useState } from "react";
|
||
import { Link, useParams } from "react-router-dom";
|
||
import { IconAlertTriangle, IconBell, IconCheck, IconClockPause, IconHandStop, IconLink, IconNote, IconPlayerPlay, IconSend, IconSettings } from "@tabler/icons-react";
|
||
import { api, type CurrentUser, type IncidentDetail as Detail, type TimelineEvent, type TimelineKind } from "../api/client";
|
||
import { ackWords } from "../components/AckDrawer";
|
||
import RunbookEntry from "../components/RunbookEntry";
|
||
import TimeAgo from "../components/TimeAgo";
|
||
import { BlockSkeleton } from "../components/Skeleton";
|
||
import { ErrorNotice } from "../components/Notice";
|
||
import { confirmDialog } from "../utils/dialogs";
|
||
import { formatDateTime } from "../utils/date";
|
||
import { readableError } from "../utils/errors";
|
||
import { toast } from "../utils/toast";
|
||
import { CATEGORY_NAMES, durationWords, incidentPath, SEVERITY_BADGE } from "../components/IncidentHistory";
|
||
import { useBreadcrumbTail } from "../utils/breadcrumb";
|
||
import WhatChangedBefore from "../components/WhatChangedBefore";
|
||
import { t } from "../i18n";
|
||
|
||
const KIND_ICON: Record<TimelineKind, JSX.Element> = {
|
||
alert: <IconAlertTriangle size={16} />,
|
||
ack: <IconHandStop size={16} />,
|
||
maintenance: <IconClockPause size={16} />,
|
||
change: <IconSettings size={16} />,
|
||
health: <IconBell size={16} />,
|
||
run: <IconPlayerPlay size={16} />,
|
||
related: <IconLink size={16} />,
|
||
note: <IconNote size={16} />,
|
||
notify: <IconSend size={16} />,
|
||
};
|
||
|
||
const TONE_COLOR: Record<TimelineEvent["tone"], string> = { good: "bg-green-lt text-green", bad: "bg-red-lt text-red", neutral: "bg-secondary-lt text-secondary", info: "bg-blue-lt text-blue" };
|
||
|
||
/** One incident, with everything the app knows about the time around it in the order it happened, and a place for notes. */
|
||
export default function IncidentDetail({ user }: { user: CurrentUser }) {
|
||
const { id: idParam } = useParams();
|
||
const id = Number(idParam);
|
||
const canWrite = user.role === "admin" || user.role === "operator";
|
||
const [detail, setDetail] = useState<Detail | null>(null);
|
||
const [error, setError] = useState<string | null>(null);
|
||
const [missing, setMissing] = useState(false);
|
||
const [note, setNote] = useState("");
|
||
// Under Alerts in the breadcrumb: "Operations › Alerts › Incident".
|
||
useBreadcrumbTail("Incident");
|
||
const [busy, setBusy] = useState(false);
|
||
|
||
const load = useCallback(() => {
|
||
if (!Number.isInteger(id)) {
|
||
setMissing(true);
|
||
return Promise.resolve();
|
||
}
|
||
return api.incidents
|
||
.get(id)
|
||
.then((d) => {
|
||
setDetail(d);
|
||
setError(null);
|
||
})
|
||
.catch((err) => {
|
||
if (/\((404|400)\)/.test(String(err))) setMissing(true);
|
||
else setError(readableError(err));
|
||
});
|
||
}, [id]);
|
||
|
||
useEffect(() => {
|
||
void load();
|
||
}, [load]);
|
||
|
||
// While it is open the picture is still changing.
|
||
const open = detail?.incident.open ?? false;
|
||
useEffect(() => {
|
||
if (!open) return;
|
||
const timer = setInterval(() => void load(), 30_000);
|
||
return () => clearInterval(timer);
|
||
}, [open, load]);
|
||
|
||
async function addNote() {
|
||
setBusy(true);
|
||
try {
|
||
await api.incidents.addNote(id, note.trim());
|
||
setNote("");
|
||
await load();
|
||
} catch (err) {
|
||
toast.error(readableError(err));
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
async function removeNote(noteId: number) {
|
||
if (!(await confirmDialog({ title: "Remove this note?", message: "It is taken out of the incident's history.", confirmLabel: "Remove", danger: true }))) return;
|
||
try {
|
||
await api.incidents.removeNote(id, noteId);
|
||
await load();
|
||
} catch (err) {
|
||
toast.error(readableError(err));
|
||
}
|
||
}
|
||
|
||
if (missing) {
|
||
return (
|
||
<div className="card">
|
||
<div className="card-body text-secondary">
|
||
There is no incident like that (it may be old enough to have been removed). <Link to="/alerts?view=history">All incidents</Link>.
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
if (!detail) {
|
||
return (
|
||
<>
|
||
<ErrorNotice error={error} />
|
||
<BlockSkeleton lines={6} />
|
||
</>
|
||
);
|
||
}
|
||
|
||
const { incident, ack, timeline } = detail;
|
||
const target = incident.target;
|
||
|
||
return (
|
||
<>
|
||
<div className="mb-2">
|
||
<Link to="/alerts?view=history" className="text-secondary">
|
||
← Alerts · History
|
||
</Link>
|
||
</div>
|
||
<div className="d-flex flex-wrap align-items-center gap-2 mb-1">
|
||
<h2 className="page-title mb-0">{incident.message}</h2>
|
||
</div>
|
||
<div className="d-flex flex-wrap align-items-center gap-2 mb-3">
|
||
{incident.open ? <span className="badge bg-red-lt text-red">open</span> : <span className="badge bg-green-lt text-green">resolved</span>}
|
||
<span className={`badge ${SEVERITY_BADGE[incident.peakSeverity] ?? ""}`}>{incident.peakSeverity}</span>
|
||
<span className="text-secondary">{CATEGORY_NAMES[incident.category] ?? incident.category}</span>
|
||
{incident.causedBy && <span className="badge bg-secondary-lt text-secondary">caused by {incident.causedBy}</span>}
|
||
{target && (
|
||
<span className="text-secondary">
|
||
· {target.type === "server" ? <Link to={`/servers/${target.id}`}>{target.name}</Link> : target.name}
|
||
</span>
|
||
)}
|
||
{target && <RunbookEntry targetType={target.type} targetId={target.id} targetName={target.name} canEdit={canWrite} forCategory={incident.category} />}
|
||
{incident.open && (
|
||
<Link to="/alerts" className="ms-auto">
|
||
Open in Alerts
|
||
</Link>
|
||
)}
|
||
</div>
|
||
<ErrorNotice error={error} />
|
||
|
||
<div className="row row-cards mb-3">
|
||
<div className="col-6 col-lg-3">
|
||
<div className="card card-sm">
|
||
<div className="card-body">
|
||
<div className="h3 mb-0" title={formatDateTime(new Date(incident.openedAt))}>
|
||
<TimeAgo value={incident.openedAt} />
|
||
</div>
|
||
<div className="text-secondary">Began</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div className="col-6 col-lg-3">
|
||
<div className="card card-sm">
|
||
<div className="card-body">
|
||
<div className="h3 mb-0">{durationWords(incident.durationMs)}</div>
|
||
<div className="text-secondary">{incident.open ? "So far" : "Lasted"}</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div className="col-6 col-lg-3">
|
||
<div className="card card-sm">
|
||
<div className="card-body">
|
||
<div className="h3 mb-0">{incident.resolvedAt ? <TimeAgo value={incident.resolvedAt} /> : "—"}</div>
|
||
<div className="text-secondary">Ended</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div className="col-6 col-lg-3">
|
||
<div className="card card-sm">
|
||
<div className="card-body">
|
||
<div className="h3 mb-0">{incident.notes}</div>
|
||
<div className="text-secondary">{incident.notes === 1 ? "Note" : "Notes"}</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
{ack && (
|
||
<div className={`alert ${ack.active ? "alert-info" : "alert-secondary"}`}>
|
||
<div>
|
||
<strong>{ack.active ? ackWords(ack) : "Its acknowledgement has run out"}</strong>
|
||
{ack.note && <div>{ack.note}</div>}
|
||
<div className="small">
|
||
Acknowledged by {ack.ackedBy} <TimeAgo value={ack.ackedAt} />.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
<div className="row g-3">
|
||
<div className="col-lg-8">
|
||
<WhatChangedBefore incidentId={incident.id} openedAt={incident.openedAt} />
|
||
<div className="card mb-3">
|
||
<div className="card-header">
|
||
<h3 className="card-title">What happened</h3>
|
||
</div>
|
||
<div className="list-group list-group-flush">
|
||
{timeline.map((e, i) => (
|
||
<div className="list-group-item" key={`${e.at}-${i}`}>
|
||
<div className="d-flex gap-3">
|
||
<span className={`badge ${TONE_COLOR[e.tone]} flex-shrink-0 align-self-start`} style={{ width: 28, height: 28, display: "inline-flex", alignItems: "center", justifyContent: "center" }}>
|
||
{KIND_ICON[e.kind] ?? <IconCheck size={16} />}
|
||
</span>
|
||
<div className="flex-fill">
|
||
<div className="d-flex flex-wrap gap-2 align-items-baseline">
|
||
<strong>{e.incidentId ? <Link to={incidentPath(e.incidentId)}>{e.title}</Link> : e.title}</strong>
|
||
<span className="text-secondary small" title={formatDateTime(new Date(e.at))}>
|
||
{formatDateTime(new Date(e.at))}
|
||
</span>
|
||
{e.actor && e.actor !== "system" && <span className="text-secondary small">· {e.actor}</span>}
|
||
</div>
|
||
{e.detail && <div className={e.kind === "note" ? "mt-1" : "text-secondary small"} style={e.kind === "note" ? { whiteSpace: "pre-wrap" } : undefined}>{e.detail}</div>}
|
||
{e.noteId !== undefined && (user.role === "admin" || e.actor === user.name) && (
|
||
<button type="button" className="btn btn-sm btn-ghost-secondary mt-1" onClick={() => void removeNote(e.noteId!)}>
|
||
Remove note
|
||
</button>
|
||
)}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
))}
|
||
</div>
|
||
<div className="card-footer text-secondary small">
|
||
Put together from the alert itself, the audit log, the maintenance windows, update runs and other alerts about the same thing.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="col-lg-4">
|
||
<div className="card">
|
||
<div className="card-header">
|
||
<h3 className="card-title">{t("Notes")}</h3>
|
||
</div>
|
||
<div className="card-body">
|
||
{detail.notes.length === 0 && <div className="text-secondary mb-3">Nothing written yet. What was it? What fixed it? What would you do differently?</div>}
|
||
{canWrite ? (
|
||
<>
|
||
<textarea className="form-control mb-2" rows={4} maxLength={4000} placeholder="Add a note…" aria-label="Add a note" value={note} onChange={(e) => setNote(e.target.value)} />
|
||
<button type="button" className="btn btn-primary" disabled={busy || note.trim() === ""} onClick={() => void addNote()}>
|
||
{busy ? "Saving…" : "Add note"}
|
||
</button>
|
||
<div className="form-hint text-secondary small mt-2">It appears in the history on the left, with your name and the time.</div>
|
||
</>
|
||
) : (
|
||
detail.notes.length > 0 && <div className="text-secondary small">Operators can add notes.</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</>
|
||
);
|
||
}
|