import { useState } from "react"; import { api, type HealthCheckSettings, type ServerRecord, type ServerThresholds as Thresholds } from "../api/client"; import { readableError } from "../utils/errors"; import { toast } from "../utils/toast"; import { ErrorNotice } from "./Notice"; type Field = "serverOfflineMinutes" | "diskUsagePercent" | "diskForecastDays" | "backupStaleHours"; const FIELDS: { key: Field; label: string; unit: string; min: number; max: number; hint: string; onlyForGuests?: boolean }[] = [ { key: "serverOfflineMinutes", label: "Offline after", unit: "min", min: 15, max: 10080, hint: "How long without a report before this server counts as offline — longer for a laptop that is off at night." }, { key: "diskUsagePercent", label: "Disk usage alert at", unit: "%", min: 50, max: 99, hint: "For every disk on this server that has no limit of its own below." }, { key: "diskForecastDays", label: "Warn when a disk will fill within", unit: "days", min: 0, max: 90, hint: "0 turns the forecast off for this server." }, { key: "backupStaleHours", label: "Its backup is too old after", unit: "hours", min: 0, max: 2160, hint: "For the Proxmox guest this server is. 0 means it is never called stale.", onlyForGuests: true }, ]; const has = (t: Thresholds | null) => !!t && (Object.keys(t).some((k) => k !== "mounts" && k !== "ignoreServices") || Object.keys(t.mounts ?? {}).length > 0); /** * This server's own alert limits. Anything left empty follows Settings → Notifications, so changing the global limit still reaches every * server that hasn't said otherwise. A disk can have a limit of its own, or be left alone altogether. Everyone can see what is set; only * an administrator changes it. */ export default function ServerThresholds({ server, defaults, disks, canEdit, onChanged }: { server: ServerRecord; defaults: HealthCheckSettings; disks: string[]; canEdit: boolean; onChanged: () => void }) { const own = server.thresholds; const isGuest = server.proxmoxVmid !== null; const [draft, setDraft] = useState>(() => ({ serverOfflineMinutes: String(own?.serverOfflineMinutes ?? ""), diskUsagePercent: String(own?.diskUsagePercent ?? ""), diskForecastDays: String(own?.diskForecastDays ?? ""), backupStaleHours: String(own?.backupStaleHours ?? ""), })); const [mounts, setMounts] = useState>(() => Object.fromEntries(Object.entries(own?.mounts ?? {}).map(([m, r]) => [m, { percent: String(r.percent ?? ""), ignore: !!r.ignore }])), ); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); // The disks it reports now, plus any that have a rule but aren't reported any more (so the rule can still be taken away). const mountNames = [...new Set([...disks, ...Object.keys(mounts)])]; function build(): Thresholds | null { const out: Thresholds = {}; for (const f of FIELDS) { const v = draft[f.key].trim(); if (v !== "") out[f.key] = Number(v); } const rules: NonNullable = {}; for (const [m, r] of Object.entries(mounts)) { const rule: { percent?: number; ignore?: boolean } = {}; if (r.percent.trim() !== "") rule.percent = Number(r.percent); if (r.ignore) rule.ignore = true; if (rule.percent !== undefined || rule.ignore) rules[m] = rule; } if (Object.keys(rules).length > 0) out.mounts = rules; // The services to ignore are edited on the health card; saving limits here must not lose them. if (own?.ignoreServices?.length) out.ignoreServices = own.ignoreServices; return Object.keys(out).length > 0 ? out : null; } async function save(value: Thresholds | null) { setBusy(true); setError(null); try { await api.servers.update(server.id, { thresholds: value }); toast.success(value ? "Saved. This server now follows its own limits where it has them." : "This server follows Settings again."); onChanged(); } catch (err) { setError(readableError(err)); } finally { setBusy(false); } } if (!canEdit) { if (!has(own)) return null; return (

Alert limits

This server has limits of its own; everything else follows Settings.
    {FIELDS.filter((f) => own?.[f.key] !== undefined).map((f) => (
  • {f.label}: {own![f.key]} {f.unit}
  • ))} {Object.entries(own?.mounts ?? {}).map(([m, r]) => (
  • {m}: {r.ignore ? "left alone" : `alerts at ${r.percent}%`}
  • ))}
); } return (

Alert limits

{has(own) && (
its own limits
)}
{ e.preventDefault(); void save(build()); }} >

Leave a box empty to follow Settings → Notifications (shown inside the box). Only what you fill in is different for this server.

{FIELDS.filter((f) => !f.onlyForGuests || isGuest || own?.[f.key] !== undefined).map((f) => (
setDraft((d) => ({ ...d, [f.key]: e.target.value }))} />
{f.hint}
))}
{mountNames.length > 0 && (
Disks
{mountNames.map((m) => { const r = mounts[m] ?? { percent: "", ignore: false }; const set = (patch: Partial) => setMounts((all) => ({ ...all, [m]: { ...r, ...patch } })); return ( ); })}
Disk Alert at (%) Leave it alone
{m} {!disks.includes(m) && (not reported now)} set({ percent: e.target.value })} />
)}
{has(own) && ( )}
); }