{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "habit-streak-calendar",
  "title": "Habit Streak Calendar",
  "description": "A month calendar where each click marks a day done or not, computing the current and the best streak and refusing future days. Use this when a habit or practice is logged day by day and the run of consecutive days matters.",
  "registryDependencies": [
    "@tealmac/calendar-grid",
    "@tealmac/date-time-core"
  ],
  "files": [
    {
      "path": "registry/patterns/habit-streak-calendar/habit-streak-calendar.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { CalendarGrid } from \"@/components/ui/calendar-grid\"\nimport {\n  addDays,\n  resolveLocale,\n  today as todayISO,\n} from \"@/components/ui/date-time-core\"\n\nexport interface Streak {\n  current: number\n  best: number\n}\n\nexport interface HabitStreakCalendarProps {\n  value?: string[]\n  defaultValue?: string[]\n  onValueChange?: (days: string[], streak: Streak) => void\n  today?: string\n  locale?: string\n}\n\n/** Current streak (ending today, or yesterday if today is not done yet) and the best ever. */\nexport function streaks(days: string[], today: string): Streak {\n  const set = new Set(days)\n  let best = 0\n  let run = 0\n  const sorted = [...set].sort()\n  sorted.forEach((d, i) => {\n    run = i > 0 && addDays(sorted[i - 1], 1) === d ? run + 1 : 1\n    best = Math.max(best, run)\n  })\n  let current = 0\n  let d = set.has(today) ? today : addDays(today, -1)\n  while (set.has(d)) {\n    current++\n    d = addDays(d, -1)\n  }\n  return { current, best }\n}\n\nexport function HabitStreakCalendar({\n  value,\n  defaultValue = [],\n  onValueChange,\n  today,\n  locale,\n}: HabitStreakCalendarProps) {\n  const loc = resolveLocale(locale)\n  const now = today ?? todayISO()\n  const [inner, setInner] = React.useState<string[]>(defaultValue)\n  const days = value ?? inner\n\n  function toggle(d: string) {\n    const next = days.includes(d) ? days.filter((x) => x !== d) : [...days, d]\n    if (value === undefined) setInner(next)\n    onValueChange?.(next, streaks(next, now))\n  }\n  const s = streaks(days, now)\n\n  return (\n    <div className=\"flex flex-col gap-3\">\n      <CalendarGrid\n        label=\"Habit log\"\n        locale={loc}\n        today={now}\n        value={null}\n        onValueChange={(d) => d && toggle(d)}\n        markers={Object.fromEntries(days.map((d) => [d, \"done\"]))}\n        max={now}\n        className=\"border\"\n      />\n      <p role=\"status\" className=\"text-sm\">\n        Current streak:{\" \"}\n        <strong>\n          {s.current} day{s.current === 1 ? \"\" : \"s\"}\n        </strong>\n        . Best: {s.best}.\n      </p>\n    </div>\n  )\n}\n",
      "type": "registry:block"
    }
  ],
  "type": "registry:block"
}