{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "heatmap-03",
  "description": "A year of daily values as a calendar grid, darker on busier days.",
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/heatmap.json",
    "https://www.dashboardblocks.com/r/radix/stat-group.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/heatmap/heatmap-03.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  HeatmapGrid,\n  HeatmapLegend,\n  type HeatmapValue,\n} from '@/registry/components/dashboardblocks/heatmap'\nimport {\n  Stat,\n  StatGroup,\n  StatLabel,\n  StatValue,\n} from '@/registry/components/dashboardblocks/stat-group'\nimport { formatDate, formatNumber } from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface Heatmap3Props extends BlockStateProps {\n  /** One value per day, oldest first, ending on `endDate`. Leave out, or pass none, to show the empty state. */\n  daily?: number[]\n  /** Defaults to a sentence about how many days had any. */\n  description?: string\n  endDate: Date\n  title: string\n  unit: string\n}\n\nconst DAY = 86_400_000\nconst WEEKS = 52\nconst DAY_NAMES = [\n  'Monday',\n  'Tuesday',\n  'Wednesday',\n  'Thursday',\n  'Friday',\n  'Saturday',\n  'Sunday',\n]\nconst ROW_LABELS = ['Mon', '', 'Wed', '', 'Fri', '', '']\n\nconst exampleProps: Heatmap3Props = {\n  daily: Array.from({ length: 360 }, (_, day) => {\n    const weekday = (day + 3) % 7\n    const weekend = weekday >= 5\n    const busy = Math.sin(day / 9) * 2.4 + Math.sin(day / 3.1) * 1.6 + day / 90\n    const value = weekend ? Math.max(0, busy - 3) : 3 + busy\n    return (day * 37) % 23 === 0 ? 0 : Math.max(0, Math.round(value))\n  }),\n  endDate: new Date(Date.UTC(2026, 8, 25)),\n  title: 'Deploys',\n  unit: 'deploys',\n}\n\nfunction getLongestRun(daily: number[]) {\n  let longest = 0\n  let current = 0\n  for (const value of daily) {\n    current = value > 0 ? current + 1 : 0\n    longest = Math.max(longest, current)\n  }\n  return longest\n}\n\nconst Heatmap3 = (props: Heatmap3Props) => {\n  const { daily = [], endDate, errorMessage, status, title, unit } = props\n  const endWeekday = (endDate.getUTCDay() + 6) % 7\n  const start = endDate.getTime() - ((WEEKS - 1) * 7 + endWeekday) * DAY\n  const dateAt = (row: number, column: number) =>\n    new Date(start + (column * 7 + row) * DAY)\n  const firstDay = endDate.getTime() - (daily.length - 1) * DAY\n\n  const values: HeatmapValue[][] = DAY_NAMES.map((_, row) =>\n    Array.from({ length: WEEKS }, (_, column) => {\n      const time = dateAt(row, column).getTime()\n      if (time > endDate.getTime() || time < firstDay) return null\n      return daily[Math.round((time - firstDay) / DAY)] ?? null\n    }),\n  )\n  const startsMonth = (column: number) =>\n    column === 0 ||\n    dateAt(0, column).getUTCMonth() !== dateAt(0, column - 1).getUTCMonth()\n  const columns = Array.from({ length: WEEKS }, (_, column) => {\n    if (!startsMonth(column)) return ''\n    if (column === 0 && [1, 2].some(startsMonth)) return ''\n    return formatDate(dateAt(0, column), 'month')\n  })\n  const total = daily.reduce((sum, value) => sum + value, 0)\n  const activeDays = daily.filter((value) => value > 0).length\n  const busiest = daily.reduce((top, value, day) => (value > daily[top] ? day : top), 0)\n  const busiestDate = new Date(firstDay + busiest * DAY)\n  const unitName = unit.charAt(0).toUpperCase() + unit.slice(1)\n  const {\n    description = `${unitName} on ${formatNumber(activeDays)} of the last ${formatNumber(daily.length)} days.`,\n  } = props\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={getBlockState(status, daily)}>\n      <Card>\n        <CardContent>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex flex-col gap-0.5'>\n              <h2 className='font-semibold tracking-tight'>{title}</h2>\n              <BlockReady className='text-sm'>\n                <p className='text-muted-foreground text-sm'>{description}</p>\n              </BlockReady>\n            </header>\n            <StatGroup className='grid-cols-2 @xl:grid-cols-3'>\n              <Stat className='col-span-2 @xl:col-span-1'>\n                <StatLabel>Total {unit}</StatLabel>\n                <StatValue>{formatNumber(total)}</StatValue>\n              </Stat>\n              <Stat className='justify-end'>\n                <StatLabel>Busiest day</StatLabel>\n                <StatValue className='text-2xl'>\n                  {formatNumber(daily[busiest] ?? 0)}\n                  <span className='text-muted-foreground ml-1 text-sm font-normal tracking-normal'>\n                    on {formatDate(busiestDate, 'day')}\n                  </span>\n                </StatValue>\n              </Stat>\n              <Stat className='justify-end'>\n                <StatLabel>Longest run</StatLabel>\n                <StatValue className='text-2xl'>\n                  {formatNumber(getLongestRun(daily))}\n                  <span className='text-muted-foreground ml-1 text-sm font-normal tracking-normal'>\n                    days in a row\n                  </span>\n                </StatValue>\n              </Stat>\n            </StatGroup>\n            <HeatmapGrid\n              minWidth='40rem'\n              cellClassName='aspect-square min-h-0'\n              cellName={({ column, row }) =>\n                `${DAY_NAMES[row]}, ${formatDate(dateAt(row, column), 'date')}`\n              }\n              columns={columns}\n              describe={({ column, row }, value) =>\n                `${DAY_NAMES[row]}, ${formatDate(dateAt(row, column), 'date')}: ${value === null ? 'no data' : `${formatNumber(value)} ${unit}`}`\n              }\n              footer={<HeatmapLegend />}\n              label={`${title}: ${unit} per day over the last ${WEEKS} weeks`}\n              rowHeader='Day'\n              rowLabelClassName='h-0 min-h-full leading-none'\n              rowNames={DAY_NAMES}\n              rows={ROW_LABELS}\n              valueName={unitName}\n              values={values}\n            />\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport { Heatmap3, exampleProps as heatmap3ExampleProps, type Heatmap3Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/heatmap/heatmap-03.tsx"
    }
  ],
  "meta": {
    "shows": "A year of daily values as a calendar grid, darker on busier days.",
    "size": "full",
    "usedWith": [
      "chart-panel-01",
      "stat-group-01"
    ],
    "component": "Heatmap3",
    "props": "{ daily?: number[]; description?: string; endDate: Date; title: string; unit: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
