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; restore: () => Promise; message: string; reload: () => void; onError?: (message: string) => void }): Promise { 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; }