Files
Homelab-manager/web/src/utils/undoable.ts
T
bobbanandClaude Sonnet 5.5 5e26e77340 UniFi: servers plugged into switches; server page in sections; My work; settings search and undo
UniFi: the controller's client list is matched to servers by IP address, so a
server is connected through a switch or access point (and port). That is a
dependency made by itself, shown on the server page, the Network devices page
and the Connections map. IP Addresses can sync the devices' addresses.

A server's page is split into Overview, Health, Network, History, Tasks and
Settings, by ?view=; alerts link to the section they are about.

My work lists what has your name on it: alerts you have, maintenance you
started (extend, end with undo) and update runs you started, with a badge.

Settings has a search box (and the command palette lists every setting).
Removing a dependency, admin link, runbook, hand-made IP address, port opening
or restore test is undone from the message instead of asked about first.

Two backup tests used a fixed date and failed as days passed; they follow the
real clock now.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-20 19:35:45 +02:00

33 lines
1.2 KiB
TypeScript

import { readableError } from "./errors";
import { toast } from "./toast";
/**
* "Removed — Undo": take something away at once and offer to put it back, instead of asking first. Only for things that can be put back as they were by making them
* again from what is on screen (a link, a note, a written dependency) — not for anything whose history, secret or address would not come back with it.
*
* Returns whether it was removed. `reload` is called after the removal and again after an undo, so the page shows what is true.
*/
export async function removeWithUndo(options: { remove: () => Promise<unknown>; restore: () => Promise<unknown>; message: string; reload: () => void; onError?: (message: string) => void }): Promise<boolean> {
try {
await options.remove();
} catch (err) {
(options.onError ?? toast.error)(readableError(err));
return false;
}
toast.success(options.message, {
label: "Undo",
onClick: async () => {
try {
await options.restore();
toast.info("Put back.");
} catch (err) {
toast.error(`Couldn't put it back: ${readableError(err)}`);
} finally {
options.reload();
}
},
});
options.reload();
return true;
}