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>
This commit is contained in:
1 parent
be6c5552bd
commit
73a06ebe63
35 files changed
+2546
-17
No files matched your search
@@ -0,0 +1,46 @@
|
||||
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}`;
|
||||
+10
-3
@@ -4,10 +4,17 @@
|
||||
|
||||
export type ToastKind = "success" | "error" | "info";
|
||||
|
||||
/** A button on the toast — "Undo" after something that can be taken back. Pressing it closes the toast and does the thing. */
|
||||
export interface ToastAction {
|
||||
label: string;
|
||||
onClick: () => void | Promise<void>;
|
||||
}
|
||||
|
||||
export interface ToastItem {
|
||||
id: number;
|
||||
kind: ToastKind;
|
||||
message: string;
|
||||
action?: ToastAction;
|
||||
}
|
||||
|
||||
let nextId = 1;
|
||||
@@ -31,18 +38,18 @@ export function dismissToast(id: number): void {
|
||||
emit();
|
||||
}
|
||||
|
||||
function show(kind: ToastKind, message: string): void {
|
||||
function show(kind: ToastKind, message: string, action?: ToastAction): void {
|
||||
const text = message.trim();
|
||||
if (!text) return;
|
||||
// The same message twice in a row is one toast, not two.
|
||||
if (items.some((t) => t.kind === kind && t.message === text)) return;
|
||||
// A handful at most: the newest few are what matter.
|
||||
items = [...items, { id: nextId++, kind, message: text }].slice(-4);
|
||||
items = [...items, { id: nextId++, kind, message: text, ...(action ? { action } : {}) }].slice(-4);
|
||||
emit();
|
||||
}
|
||||
|
||||
export const toast = {
|
||||
success: (message: string) => show("success", message),
|
||||
success: (message: string, action?: ToastAction) => show("success", message, action),
|
||||
error: (message: string) => show("error", message),
|
||||
info: (message: string) => show("info", message),
|
||||
};
|
||||
Reference in new issue
Block a user