{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "optimistic-transfer-save",
  "title": "Optimistic Transfer Save",
  "description": "A transfer list that shows each move immediately, saves in the background, ignores slow older responses and reverts with an error and Retry when a save fails. Use this when the lists live on a server and the interface must stay responsive.",
  "registryDependencies": [
    "button",
    "@tealmac/transfer-list"
  ],
  "files": [
    {
      "path": "registry/patterns/optimistic-transfer-save/optimistic-transfer-save.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { Button } from \"@/components/ui/button\"\nimport { TransferList, type TransferItem } from \"@/components/ui/transfer-list\"\n\nexport interface OptimisticTransferSaveProps {\n  items: TransferItem[]\n  /** The saved value on the server. */\n  initialValue?: string[]\n  /** Save the new list. Reject to revert. */\n  save: (ids: string[]) => Promise<void>\n  titles?: [string, string]\n}\n\ntype Status = \"idle\" | \"saving\" | \"saved\" | \"error\"\n\n/**\n * The list changes the instant a move is made. Each save has a sequence\n * number: if a newer move has started, an older response (success or failure)\n * is ignored, so a slow request can never overwrite what the person did\n * since. A failure puts the last saved list back and offers Retry.\n */\nexport function OptimisticTransferSave({\n  items,\n  initialValue = [],\n  save,\n  titles = [\"Available\", \"Selected\"],\n}: OptimisticTransferSaveProps) {\n  const [value, setValue] = React.useState(initialValue)\n  const [status, setStatus] = React.useState<Status>(\"idle\")\n  const [failed, setFailed] = React.useState<string[] | null>(null)\n  const saved = React.useRef(initialValue)\n  const seq = React.useRef(0)\n\n  const run = (next: string[]) => {\n    const mine = ++seq.current\n    setValue(next)\n    setStatus(\"saving\")\n    setFailed(null)\n    save(next).then(\n      () => {\n        if (mine !== seq.current) return\n        saved.current = next\n        setStatus(\"saved\")\n      },\n      () => {\n        if (mine !== seq.current) return\n        setValue(saved.current)\n        setFailed(next)\n        setStatus(\"error\")\n      }\n    )\n  }\n\n  return (\n    <div className=\"flex w-full flex-col gap-2\" aria-busy={status === \"saving\"}>\n      <TransferList\n        aria-label=\"Transfer\"\n        items={items}\n        titles={titles}\n        value={value}\n        onValueChange={run}\n      />\n      <div className=\"min-h-9 text-sm\">\n        {status === \"saving\" ? (\n          <p className=\"text-muted-foreground\" role=\"status\">\n            Saving...\n          </p>\n        ) : status === \"saved\" ? (\n          <p className=\"text-muted-foreground\" role=\"status\">\n            All changes saved.\n          </p>\n        ) : status === \"error\" && failed ? (\n          <p\n            role=\"alert\"\n            className=\"text-destructive flex flex-wrap items-center gap-2\"\n          >\n            Could not save. The list went back to the last saved version.\n            <Button size=\"sm\" variant=\"outline\" onClick={() => run(failed)}>\n              Retry\n            </Button>\n          </p>\n        ) : null}\n      </div>\n    </div>\n  )\n}\n",
      "type": "registry:block"
    }
  ],
  "type": "registry:block"
}