Files
Homelab-manager/web/src/utils/bulk.ts
T
bobbanandClaude Sonnet 5.5 73a06ebe63 Flapping alerts, a noise report and bulk actions with undo
Flapping: an alert raised three times within an hour is announced once as
flapping, its further raise/clear messages are held until it has been calm
for an hour, and one message then says whether it is still active. Event
webhooks still hear everything. The state survives restarts.

Alerts > Noise lists the alerts that keep coming back (7-180 days): how often,
how long, most in an hour, and a hint on what helps.

Bulk actions on Alerts (acknowledge, remove acknowledgement, silence), Servers
(tag, silence, runs on) and Inventory (location and dates, print labels). Each
reports what it left alone and why, and has an Undo on the toast.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-19 23:55:09 +02:00

47 lines
1.7 KiB
TypeScript

import { api, type BulkResult } from "../api/client";
import { readableError } from "./errors";
import { toast } from "./toast";
type Call = (body: Record<string, unknown>) => Promise<BulkResult>;
/**
* Runs a bulk action and says how it went, with an Undo button that sends back the body the server handed over with its answer. What was left alone is said too,
* with why — so "12 of 14" is never a mystery. `reload` is called after the action and after an undo, so the page shows what is true.
*/
export async function doBulk(options: { call: Call; body: Record<string, unknown>; done: (r: BulkResult) => string; reload: () => void }): Promise<BulkResult | null> {
try {
const r = await options.call(options.body);
const why = [...new Set(r.skipped.map((s) => s.reason))];
const skipped = r.skipped.length > 0 ? ` ${r.skipped.length} left alone: ${why.slice(0, 2).join("; ")}${why.length > 2 ? "…" : ""}.` : "";
const message = r.changed > 0 ? options.done(r) + skipped : `Nothing changed.${skipped}`;
const undo = r.undo;
toast.success(
message,
undo
? {
label: "Undo",
onClick: async () => {
try {
await options.call(undo);
toast.info("Undone.");
} catch (err) {
toast.error(readableError(err));
} finally {
options.reload();
}
},
}
: undefined,
);
options.reload();
return r;
} catch (err) {
toast.error(readableError(err));
return null;
}
}
export const bulk = api.bulk;
export const plural = (n: number, one: string, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;