{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "resend-cooldown-button",
  "title": "Resend Button with Cooldown",
  "description": "A resend-the-code button that disables itself for a countdown after each press, says how long is left in words, and re-enables itself, with a limit on how many times it can be used. Use this when resending is rate limited.",
  "registryDependencies": [
    "button"
  ],
  "files": [
    {
      "path": "registry/patterns/resend-cooldown-button/resend-cooldown-button.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { Button } from \"@/components/ui/button\"\n\nexport interface ResendCooldownButtonProps {\n  /** Send again. Reject to show an error and skip the cooldown. */\n  onResend?: () => Promise<void>\n  /** Wait between sends. Default 30. */\n  seconds?: number\n  /** Most sends allowed. Default 3. */\n  maxSends?: number\n}\n\nexport function ResendCooldownButton({\n  onResend,\n  seconds = 30,\n  maxSends = 3,\n}: ResendCooldownButtonProps) {\n  const [left, setLeft] = React.useState(0)\n  const [sends, setSends] = React.useState(0)\n  const [busy, setBusy] = React.useState(false)\n  const [error, setError] = React.useState(false)\n  const [announce, setAnnounce] = React.useState(\"\")\n\n  React.useEffect(() => {\n    if (left <= 0) return\n    const timer = setTimeout(() => {\n      setLeft(left - 1)\n      // Say the time left at 10, 5 and 0 only, so the countdown does not chatter.\n      if ([10, 5].includes(left - 1))\n        setAnnounce(`You can resend in ${left - 1} seconds.`)\n      if (left - 1 === 0) setAnnounce(\"You can resend the code now.\")\n    }, 1000)\n    return () => clearTimeout(timer)\n  }, [left])\n\n  async function resend() {\n    setBusy(true)\n    setError(false)\n    try {\n      await onResend?.()\n      setSends((n) => n + 1)\n      setLeft(seconds)\n      setAnnounce(\"Code sent.\")\n    } catch {\n      setError(true)\n    } finally {\n      setBusy(false)\n    }\n  }\n\n  const exhausted = sends >= maxSends\n  return (\n    <div className=\"flex flex-col items-start gap-1\">\n      <Button\n        type=\"button\"\n        variant=\"outline\"\n        disabled={busy || left > 0 || exhausted}\n        onClick={() => void resend()}\n      >\n        {busy\n          ? \"Sending...\"\n          : left > 0\n            ? `Resend code in ${left}s`\n            : \"Resend code\"}\n      </Button>\n      <p className=\"text-muted-foreground text-xs\">\n        {exhausted\n          ? \"You have used all resends. Contact support if the code did not arrive.\"\n          : `${maxSends - sends} ${maxSends - sends === 1 ? \"resend\" : \"resends\"} left.`}\n      </p>\n      {error ? (\n        <p role=\"alert\" className=\"text-destructive text-xs\">\n          Could not send the code. Try again.\n        </p>\n      ) : null}\n      <span role=\"status\" aria-live=\"polite\" className=\"sr-only\">\n        {announce}\n      </span>\n    </div>\n  )\n}\n",
      "type": "registry:block"
    }
  ],
  "type": "registry:block"
}