{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "activity-heatmap",
  "title": "Activity heatmap",
  "description": "GitHub-style calendar heatmap of a year of bookings with tooltips, bookings or revenue metric, 12-month or 90-day range, streak cards, best hours bars and best weekdays.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/number-flow.json",
    "https://goodui.dev/r/tooltip.json"
  ],
  "files": [
    {
      "path": "registry/blocks/dashboard/activity-heatmap.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { CalendarDays, Clock3, Flame, Trophy } from \"lucide-react\"\nimport { Tooltip, TooltipGroup } from \"@/components/goodui/tooltip\"\nimport { NumberFlow } from \"@/components/goodui/number-flow\"\nimport { cn } from \"@/lib/utils\"\n\n/* ---------- Demo data: one year of daily bookings ending Sat 31 Oct 2026 ---------- */\n\nconst END = Date.UTC(2026, 9, 31) // last cell\nconst DAYS = 371 // 53 weeks\nconst MONTHS = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\nconst DOW = [\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\"]\n\ntype Metric = \"bookings\" | \"revenue\"\ntype Day = { ts: number; dow: number; bookings: number; revenue: number }\n\nfunction hash(i: number) {\n  const x = Math.sin(i * 12.9898 + 78.233) * 43758.5453\n  return x - Math.floor(x)\n}\n\n// Deterministic: Saturdays busy, Sundays closed, a slow February, a holiday dip at Christmas.\nconst START = END - (DAYS - 1) * 86400000\nconst startDow = new Date(START).getUTCDay()\nconst DATA: Day[] = Array.from({ length: DAYS }, (_, i) => {\n  const ts = START + i * 86400000\n  const d = new Date(ts)\n  const dow = d.getUTCDay()\n  const month = d.getUTCMonth()\n  let base = dow === 0 ? 0 : dow === 6 ? 30 : dow === 5 ? 24 : 17\n  if (month === 1) base *= 0.7\n  if (month === 11 && d.getUTCDate() >= 23) base *= 0.25\n  if (month === 11 && d.getUTCDate() < 23) base *= 1.25\n  if (month === 5 || month === 6) base *= 1.1\n  const closed = (month === 11 && (d.getUTCDate() === 25 || d.getUTCDate() === 26)) || (month === 0 && d.getUTCDate() === 1) || (month === 7 && d.getUTCDate() >= 10 && d.getUTCDate() <= 16)\n  if (closed) base = 0\n  const bookings = base === 0 ? 0 : Math.max(0, Math.round(base + (hash(i) - 0.4) * 14 + i / 60))\n  return { ts, dow, bookings, revenue: Math.round(bookings * (58 + hash(i + 7) * 30)) }\n})\n\nconst HOURS = Array.from({ length: 13 }, (_, h) => h + 8) // 08:00–20:00\nconst HOUR_LOAD = [6, 14, 22, 27, 25, 18, 21, 28, 30, 26, 19, 11, 4]\nconst WEEKDAY_LOAD = [0, 62, 70, 74, 88, 96, 100]\n\nconst fmtDate = (ts: number) => {\n  const d = new Date(ts)\n  return `${DOW[d.getUTCDay()]} ${d.getUTCDate()} ${MONTHS[d.getUTCMonth()]} ${d.getUTCFullYear()}`\n}\nconst money = (n: number) => `$${n.toLocaleString(\"en-US\")}`\nconst ring = \"outline-none focus-visible:ring-2 focus-visible:ring-ring/60 focus-visible:ring-offset-1 focus-visible:ring-offset-background\"\n\n/* ---------- Block ---------- */\n\nexport default function ActivityHeatmap() {\n  const [metric, setMetric] = React.useState<Metric>(\"bookings\")\n  const [focus, setFocus] = React.useState<Day | null>(null)\n  const [range, setRange] = React.useState<\"year\" | \"quarter\">(\"year\")\n\n  const shown = range === \"year\" ? DATA : DATA.slice(DAYS - 91)\n  const max = Math.max(...shown.map((d) => d[metric]))\n  const level = (v: number) => (v === 0 ? 0 : Math.min(4, 1 + Math.floor((v / max) * 3.99)))\n  const firstDow = range === \"year\" ? startDow : new Date(shown[0]!.ts).getUTCDay()\n  const cols = Math.ceil((shown.length + firstDow) / 7)\n  const weeks = Array.from({ length: cols }, (_, w) => Array.from({ length: 7 }, (_, d) => shown[w * 7 + d - firstDow] ?? null))\n\n  // Streaks: consecutive open days with at least one booking.\n  let cur = 0\n  let longest = 0\n  let run = 0\n  for (const d of DATA) {\n    if (d.dow === 0) continue\n    if (d.bookings > 0) {\n      run++\n      longest = Math.max(longest, run)\n    } else run = 0\n  }\n  cur = run\n  const total = shown.reduce((s, d) => s + d[metric], 0)\n  const busiest = shown.reduce((a, b) => (b[metric] > a[metric] ? b : a))\n  const openDays = shown.filter((d) => d.dow !== 0).length\n  const monthLabels = weeks.map((w, i) => {\n    const first = w.find((d) => d)\n    if (!first) return null\n    const m = new Date(first.ts).getUTCMonth()\n    const prev = weeks[i - 1]?.find((d) => d)\n    return prev && new Date(prev.ts).getUTCMonth() === m ? null : MONTHS[m]\n  })\n  const shades = [\"bg-muted\", \"bg-primary/25\", \"bg-primary/50\", \"bg-primary/75\", \"bg-primary\"]\n\n  return (\n    <div className=\"p-4 md:p-6\">\n      <div className=\"mx-auto flex max-w-6xl flex-col gap-5\">\n        <div className=\"flex flex-wrap items-end justify-between gap-3\">\n          <div>\n            <h2 className=\"text-xl font-semibold tracking-tight\">Booking activity</h2>\n            <p className=\"text-sm text-muted-foreground\">Every day of the last year at a glance · Downtown studio</p>\n          </div>\n          <div className=\"flex flex-wrap gap-2\">\n            <div role=\"radiogroup\" aria-label=\"Metric\" className=\"flex rounded-lg bg-muted p-0.5 text-xs\">\n              {([\"bookings\", \"revenue\"] as const).map((m) => (\n                <button key={m} type=\"button\" role=\"radio\" aria-checked={metric === m} onClick={() => setMetric(m)} className={cn(\"rounded-md px-2.5 py-1 font-medium text-foreground/70 capitalize transition-colors\", metric === m && \"bg-background text-foreground shadow-xs\", ring)}>\n                  {m}\n                </button>\n              ))}\n            </div>\n            <div role=\"radiogroup\" aria-label=\"Range\" className=\"flex rounded-lg bg-muted p-0.5 text-xs\">\n              {(\n                [\n                  [\"year\", \"12 months\"],\n                  [\"quarter\", \"90 days\"],\n                ] as const\n              ).map(([k, label]) => (\n                <button key={k} type=\"button\" role=\"radio\" aria-checked={range === k} onClick={() => setRange(k)} className={cn(\"rounded-md px-2.5 py-1 font-medium text-foreground/70 transition-colors\", range === k && \"bg-background text-foreground shadow-xs\", ring)}>\n                  {label}\n                </button>\n              ))}\n            </div>\n          </div>\n        </div>\n\n        {/* Streak strip */}\n        <div className=\"grid gap-3 sm:grid-cols-4\">\n          <div className=\"flex items-center gap-3 rounded-2xl border bg-gradient-to-br from-orange-500/15 to-rose-500/10 p-4\">\n            <span className=\"flex size-10 items-center justify-center rounded-xl bg-orange-500 text-white\">\n              <Flame className=\"size-5\" aria-hidden=\"true\" />\n            </span>\n            <div>\n              <p className=\"text-xs font-medium text-muted-foreground\">Current streak</p>\n              <p className=\"text-2xl font-semibold tabular-nums\">\n                {cur} <span className=\"text-sm font-normal text-muted-foreground\">open days</span>\n              </p>\n            </div>\n          </div>\n          <div className=\"flex items-center gap-3 rounded-2xl border bg-card p-4\">\n            <span className=\"flex size-10 items-center justify-center rounded-xl bg-amber-500/15 text-amber-700 dark:text-amber-300\">\n              <Trophy className=\"size-5\" aria-hidden=\"true\" />\n            </span>\n            <div>\n              <p className=\"text-xs font-medium text-muted-foreground\">Longest streak</p>\n              <p className=\"text-2xl font-semibold tabular-nums\">\n                {longest} <span className=\"text-sm font-normal text-muted-foreground\">days</span>\n              </p>\n            </div>\n          </div>\n          <div className=\"flex items-center gap-3 rounded-2xl border bg-card p-4\">\n            <span className=\"flex size-10 items-center justify-center rounded-xl bg-primary/10 text-primary\">\n              <CalendarDays className=\"size-5\" aria-hidden=\"true\" />\n            </span>\n            <div>\n              <p className=\"text-xs font-medium text-muted-foreground\">Total {metric}</p>\n              <p className=\"text-2xl font-semibold tabular-nums\">\n                <NumberFlow value={total} format={metric === \"revenue\" ? { style: \"currency\", currency: \"USD\", maximumFractionDigits: 0 } : undefined} />\n              </p>\n            </div>\n          </div>\n          <div className=\"flex items-center gap-3 rounded-2xl border bg-card p-4\">\n            <span className=\"flex size-10 items-center justify-center rounded-xl bg-emerald-500/12 text-emerald-700 dark:text-emerald-300\">\n              <Clock3 className=\"size-5\" aria-hidden=\"true\" />\n            </span>\n            <div className=\"min-w-0\">\n              <p className=\"text-xs font-medium text-muted-foreground\">Busiest day</p>\n              <p className=\"truncate text-sm font-semibold\">{fmtDate(busiest.ts)}</p>\n              <p className=\"text-xs text-muted-foreground tabular-nums\">{metric === \"revenue\" ? money(busiest.revenue) : `${busiest.bookings} bookings`}</p>\n            </div>\n          </div>\n        </div>\n\n        {/* Heatmap */}\n        <section className=\"rounded-2xl border bg-card p-4 sm:p-5\" aria-label=\"Calendar heatmap\">\n          <div className=\"flex flex-wrap items-center justify-between gap-2\">\n            <h3 className=\"text-sm font-semibold\">{range === \"year\" ? \"Last 12 months\" : \"Last 90 days\"}</h3>\n            <p className=\"text-xs text-muted-foreground tabular-nums\" aria-live=\"polite\">\n              {focus ? `${fmtDate(focus.ts)} · ${focus.bookings} bookings · ${money(focus.revenue)}` : `${openDays} open days · avg ${metric === \"revenue\" ? money(Math.round(total / openDays)) : Math.round(total / openDays)} per day`}\n            </p>\n          </div>\n          <TooltipGroup delay={100}>\n            <div className=\"relative mt-3 overflow-x-auto pb-1\">\n              <div className=\"flex gap-2\" style={{ minWidth: range === \"year\" ? 760 : 0 }}>\n                <div className=\"flex flex-col gap-[3px] pt-5 text-[10px] text-muted-foreground\" aria-hidden=\"true\">\n                  {DOW.map((d, i) => (\n                    <span key={d} className=\"h-3 leading-3\">\n                      {i % 2 === 1 ? d : \"\"}\n                    </span>\n                  ))}\n                </div>\n                <div className=\"flex-1\">\n                  <div className=\"flex h-5 text-[10px] text-muted-foreground\" aria-hidden=\"true\">\n                    {monthLabels.map((m, i) => (\n                      <span key={i} className=\"min-w-0 flex-1 whitespace-nowrap\">\n                        {m ?? \"\"}\n                      </span>\n                    ))}\n                  </div>\n                  <div className=\"flex gap-[3px]\" aria-label={`Daily ${metric}`}>\n                    {weeks.map((w, wi) => (\n                      <div key={wi} className=\"flex flex-1 flex-col gap-[3px]\">\n                        {w.map((d, di) =>\n                          d ? (\n                            <Tooltip key={di} content={`${fmtDate(d.ts)}: ${d.bookings} bookings, ${money(d.revenue)}`} side=\"top\">\n                              <button\n                                type=\"button\"\n                                aria-label={`${fmtDate(d.ts)}: ${d.bookings} bookings`}\n                                onFocus={() => setFocus(d)}\n                                onBlur={() => setFocus(null)}\n                                onMouseEnter={() => setFocus(d)}\n                                onMouseLeave={() => setFocus(null)}\n                                className={cn(\"block h-3 w-full rounded-[3px] transition-transform hover:scale-125\", shades[level(d[metric])], d.dow === 0 && \"bg-muted/40\", ring)}\n                              />\n                            </Tooltip>\n                          ) : (\n                            <span key={di} className=\"block h-3 w-full\" aria-hidden=\"true\" />\n                          ),\n                        )}\n                      </div>\n                    ))}\n                  </div>\n                </div>\n              </div>\n            </div>\n          </TooltipGroup>\n          <div className=\"mt-3 flex items-center justify-end gap-1.5 text-[11px] text-muted-foreground\">\n            <span>Less</span>\n            {shades.map((s) => (\n              <span key={s} className={cn(\"size-3 rounded-[3px]\", s)} aria-hidden=\"true\" />\n            ))}\n            <span>More</span>\n          </div>\n        </section>\n\n        <div className=\"grid gap-4 md:grid-cols-[1.5fr_1fr] [&>*]:min-w-0\">\n          {/* Best hours */}\n          <section className=\"rounded-2xl border bg-card p-4 sm:p-5\" aria-label=\"Best hours\">\n            <h3 className=\"text-sm font-semibold\">Best hours</h3>\n            <p className=\"text-xs text-muted-foreground\">Average bookings per hour, open days only</p>\n            <div className=\"mt-4 flex h-36 items-end gap-1.5\" role=\"img\" aria-label=\"Bookings by hour; busiest at 16:00 and 11:00\">\n              {HOURS.map((h, i) => {\n                const v = HOUR_LOAD[i]!\n                const top = v === Math.max(...HOUR_LOAD)\n                return (\n                  <div key={h} className=\"group flex h-full flex-1 flex-col items-center justify-end gap-1\">\n                    <span className={cn(\"text-[10px] font-medium tabular-nums\", top ? \"text-primary\" : \"text-muted-foreground opacity-0 group-hover:opacity-100\")}>{v}</span>\n                    <div className={cn(\"w-full rounded-t-md transition-colors\", top ? \"bg-primary\" : \"bg-primary/35 group-hover:bg-primary/60\")} style={{ height: `${(v / 30) * 100}%` }} />\n                    <span className=\"text-[10px] text-muted-foreground tabular-nums\">{h}</span>\n                  </div>\n                )\n              })}\n            </div>\n          </section>\n\n          {/* Best weekdays */}\n          <section className=\"rounded-2xl border bg-card p-4 sm:p-5\" aria-label=\"Best weekdays\">\n            <h3 className=\"text-sm font-semibold\">Best weekdays</h3>\n            <p className=\"text-xs text-muted-foreground\">Relative to the busiest day</p>\n            <ul className=\"mt-4 flex flex-col gap-2\">\n              {[1, 2, 3, 4, 5, 6, 0].map((d) => (\n                <li key={d} className=\"flex items-center gap-3 text-xs\">\n                  <span className=\"w-8 text-muted-foreground\">{DOW[d]}</span>\n                  <span className=\"h-2.5 flex-1 overflow-hidden rounded-full bg-muted\" aria-hidden=\"true\">\n                    <span className={cn(\"block h-full rounded-full\", WEEKDAY_LOAD[d] === 100 ? \"bg-primary\" : \"bg-primary/50\")} style={{ width: `${WEEKDAY_LOAD[d]}%` }} />\n                  </span>\n                  <span className=\"w-10 text-right font-medium tabular-nums\">{d === 0 ? \"closed\" : `${WEEKDAY_LOAD[d]}%`}</span>\n                </li>\n              ))}\n            </ul>\n          </section>\n        </div>\n      </div>\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/activity-heatmap.tsx"
    }
  ],
  "categories": [
    "dashboard"
  ],
  "type": "registry:block"
}
