{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-undo-batch",
  "title": "useUndoBatch",
  "description": "Batches undoable actions into one Sonner toast whose Undo restores them all and retries what failed.",
  "dependencies": [
    "sonner@^2.0.8"
  ],
  "files": [
    {
      "path": "registry/khaeli/hooks/use-undo-batch.ts",
      "content": "import * as React from \"react\"\nimport { toast } from \"sonner\"\n\ntype UndoEntry = {\n  undo: () => unknown | Promise<unknown>\n  /** How many items this entry stands for (default 1). */\n  count?: number\n}\n\ntype UndoBatch = {\n  push(entry: UndoEntry): void\n  undo(): Promise<void>\n  clear(): void\n}\n\n/** How a batch tells the person about itself: sonner in the app, a fake in tests. */\ninterface UndoBatchNotifier {\n  show(toast: { message: string; failed: boolean; onUndo: () => void; onClose: () => void }): void\n  dismiss(): void\n}\n\n/**\n * The batch itself, free of React and sonner so its rules can be tested.\n * Every `push` while the toast is open joins the batch; once the toast\n * closes, the next push starts a new one, but `undo()` still reverses the\n * last batch, so a key can undo after the toast has gone. `undo()` reverses\n * entries newest first and tries every one: an entry that fails stays in the\n * batch and the toast comes back marked failed, so Undo (or the key) retries\n * exactly what did not come back.\n */\nclass UndoBatchStore {\n  private entries: UndoEntry[] = []\n  private open = false\n  // Each batch has its own generation, so a closing toast's callbacks never close a newer batch.\n  private generation = 0\n\n  constructor(\n    private notifier: UndoBatchNotifier,\n    private message: (count: number) => string\n  ) {}\n\n  update(notifier: UndoBatchNotifier, message: (count: number) => string) {\n    this.notifier = notifier\n    this.message = message\n  }\n\n  push(entry: UndoEntry) {\n    if (!this.open) this.restart([])\n    this.entries.push(entry)\n    this.show(false)\n  }\n\n  async undo() {\n    const batch = this.entries\n    this.restart([])\n    this.notifier.dismiss()\n    const failed: UndoEntry[] = []\n    for (const entry of [...batch].reverse()) {\n      try {\n        await entry.undo()\n      } catch {\n        failed.unshift(entry)\n      }\n    }\n    if (failed.length === 0) return\n    // A push while the undo ran opened a newer batch; what did not come back goes ahead of it.\n    if (this.open) this.entries = [...failed, ...this.entries]\n    else this.restart(failed)\n    this.show(true)\n  }\n\n  clear() {\n    this.restart([])\n    this.notifier.dismiss()\n  }\n\n  private restart(entries: UndoEntry[]) {\n    this.entries = entries\n    this.open = false\n    this.generation += 1\n  }\n\n  private show(failed: boolean) {\n    const batch = this.generation\n    this.open = true\n    const count = this.entries.reduce((sum, entry) => sum + (entry.count ?? 1), 0)\n    this.notifier.show({\n      message: this.message(count),\n      failed,\n      onUndo: () => void this.undo(),\n      onClose: () => {\n        if (this.generation === batch) this.open = false\n      },\n    })\n  }\n}\n\nfunction sonnerNotifier(id: string, durationMs: number): UndoBatchNotifier {\n  return {\n    show: ({ message, failed, onUndo, onClose }) => {\n      const options = {\n        id,\n        duration: durationMs,\n        action: { label: \"Undo\", onClick: onUndo },\n        onDismiss: onClose,\n        onAutoClose: onClose,\n      }\n      if (failed) toast.error(message, { ...options, description: \"Some of these didn't come back. Undo to try again.\" })\n      else toast(message, options)\n    },\n    dismiss: () => toast.dismiss(id),\n  }\n}\n\n/**\n * Rolls quick successive reversible actions into one sonner toast per `id`:\n * every `push` while the toast is open re-counts its message (\"3 tasks\n * archived\"); its Undo action, or `undo()` (for `z` or Shift+E), reverses the\n * batch. See UndoBatchStore for how a failed undo is kept for a retry.\n */\nfunction useUndoBatch(options: {\n  id: string\n  message: (count: number) => string\n  durationMs?: number\n}): UndoBatch {\n  const { id, message, durationMs = 6000 } = options\n  const [store] = React.useState(() => new UndoBatchStore(sonnerNotifier(id, durationMs), message))\n  store.update(sonnerNotifier(id, durationMs), message)\n  return React.useMemo(\n    () => ({ push: (entry) => store.push(entry), undo: () => store.undo(), clear: () => store.clear() }),\n    [store]\n  )\n}\n\nexport { UndoBatchStore, useUndoBatch, type UndoBatch, type UndoBatchNotifier, type UndoEntry }\n",
      "type": "registry:hook"
    }
  ],
  "type": "registry:hook"
}