import { useState } from "react"; import { api, type AlertItem } from "../api/client"; import { readableError } from "../utils/errors"; import { toast } from "../utils/toast"; const CHOICES = [ { minutes: 60, label: "1 h", words: "1 hour" }, { minutes: 240, label: "4 h", words: "4 hours" }, { minutes: 1440, label: "24 h", words: "24 hours" }, ]; /** * "Silence" on an alert: pick how long, and a maintenance window goes on whatever the alert is about. That silences everything * about that server or integration, not just this one alert — which is what a maintenance window is — so it says so, and the * Maintenance page is where to end it early. The choices open in place, so nothing is clipped by the table around it. */ export default function SilenceButton({ alert, onDone }: { alert: AlertItem; onDone: () => void }) { const [choosing, setChoosing] = useState(false); const [busy, setBusy] = useState(false); const target = alert.target; if (!target || alert.silenced) return null; async function silence(minutes: number, words: string) { if (!target) return; setBusy(true); try { await api.maintenance.start({ targetType: target.type, targetId: target.id, minutes, reason: `Silenced from the Alerts page: ${alert.message}`.slice(0, 200), }); toast.success(`${target.name} is silenced for ${words} — every alert about it. End it early under Maintenance.`); // The sidebar badge and the maintenance banner listen for this. window.dispatchEvent(new Event("maintenance-changed")); setChoosing(false); onDone(); } catch (err) { toast.error(readableError(err)); } finally { setBusy(false); } } if (!choosing) { return ( ); } return (
{CHOICES.map((c) => ( ))}
); }