{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-action-status",
  "title": "useActionStatus",
  "description": "One async action as a FancyButton status: pending while it runs, done briefly once it lands, then idle.",
  "files": [
    {
      "path": "registry/khaeli/hooks/use-action-status.ts",
      "content": "import * as React from \"react\"\n\ntype ActionStatus = \"idle\" | \"pending\" | \"done\"\n\n/**\n * One async action's face for a FancyButton `status`: pending while the work\n * runs, done for `doneMs` (1500 by default) once it lands, then idle. A\n * rejection returns to idle and rethrows, so the caller's error path stays\n * its own. A newer run supersedes an older one's result.\n *\n *   const save = useActionStatus()\n *   <FancyButton status={save.status} onClick={() => save.run(mutation.mutateAsync(input))}>Save</FancyButton>\n */\nfunction useActionStatus(options?: { doneMs?: number }) {\n  const doneMs = options?.doneMs ?? 1500\n  const [status, setStatus] = React.useState<ActionStatus>(\"idle\")\n  const timer = React.useRef<ReturnType<typeof setTimeout> | undefined>(undefined)\n  const latest = React.useRef(0)\n\n  React.useEffect(() => () => clearTimeout(timer.current), [])\n\n  const run = React.useCallback(\n    async <T>(work: Promise<T> | (() => Promise<T>)): Promise<T> => {\n      const ticket = ++latest.current\n      clearTimeout(timer.current)\n      setStatus(\"pending\")\n      try {\n        const result = await (typeof work === \"function\" ? work() : work)\n        if (ticket === latest.current) {\n          setStatus(\"done\")\n          timer.current = setTimeout(() => {\n            if (ticket === latest.current) setStatus(\"idle\")\n          }, doneMs)\n        }\n        return result\n      } catch (error) {\n        if (ticket === latest.current) setStatus(\"idle\")\n        throw error\n      }\n    },\n    [doneMs]\n  )\n\n  return React.useMemo(() => ({ status, run }), [status, run])\n}\n\nexport { useActionStatus, type ActionStatus }\n",
      "type": "registry:hook"
    }
  ],
  "type": "registry:hook"
}