{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "realtime-04",
  "description": "Running totals since midnight against yesterday at the same time, with orders per hour beside yesterday as a dashed line.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/chart.json",
    "https://www.dashboardblocks.com/r/radix/realtime.json",
    "https://www.dashboardblocks.com/r/radix/stat-group.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/realtime/realtime-04.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  ChartPlaceholder,\n  ChartTooltipContent,\n  chartAxisProps,\n  chartBarCursor,\n  ChartHatch,\n  useChartId,\n} from '@/registry/components/dashboardblocks/chart'\nimport {\n  LiveBadge,\n  LiveNumber,\n  createRandom,\n} from '@/registry/components/dashboardblocks/realtime'\nimport {\n  Stat,\n  StatChange,\n  StatGroup,\n  StatLabel,\n  StatValue,\n  getStatPlaceholders,\n} from '@/registry/components/dashboardblocks/stat-group'\nimport { formatCurrency, formatNumber } from '@/registry/lib/dashboardblocks/format'\nimport {\n  Bar,\n  Cell,\n  ComposedChart,\n  LabelList,\n  Line,\n  ResponsiveContainer,\n  Tooltip,\n  XAxis,\n} from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface TodayMetric {\n  format?: (value: number) => string\n  id: string\n  label: string\n  today: number\n  yesterday: number\n}\n\ninterface Realtime4Props extends BlockStateProps {\n  /** Defaults to a sentence about the first metric against yesterday. */\n  description?: string\n  /** Orders (or any count) per hour so far today, from midnight; the last hour is still counting. */\n  hourly?: number[]\n  hourlyLabel?: string\n  /** Leave out, or pass none, to show the empty state. */\n  metrics?: TodayMetric[]\n  title: string\n  /** The same count for each of yesterday's 24 hours, drawn as a dashed line. */\n  yesterdayHourly?: number[]\n}\n\nconst seed = createRandom(3)\nconst HOURLY = [12, 7, 4, 3, 4, 9, 22, 41, 63, 78, 86, 91, 88, 84, 57]\nconst YESTERDAY = [\n  14, 8, 5, 3, 3, 8, 19, 36, 55, 70, 79, 83, 81, 77, 74, 72, 70, 66, 61, 54, 47, 38, 29,\n  20,\n]\nconst HOUR_LABELS = Array.from({ length: 24 }, (_, hour) => {\n  const suffix = hour < 12 ? 'am' : 'pm'\n  return `${hour % 12 === 0 ? 12 : hour % 12}${suffix}`\n})\nconst HOUR_TICKS = [6, 12, 18]\n\nconst exampleProps: Realtime4Props = {\n  hourly: HOURLY.map((value) => Math.round(value * (0.9 + seed() * 0.2))),\n  hourlyLabel: 'Orders',\n  metrics: [\n    { id: 'orders', label: 'Orders', today: 647, yesterday: 588 },\n    {\n      format: (value) => formatCurrency(value, { compact: true }),\n      id: 'revenue',\n      label: 'Revenue',\n      today: 48_215,\n      yesterday: 49_870,\n    },\n    { id: 'signups', label: 'Sign-ups', today: 132, yesterday: 104 },\n  ],\n  title: 'Today so far',\n  yesterdayHourly: YESTERDAY.map((value) => Math.round(value * (0.9 + seed() * 0.2))),\n}\n\nconst CHART_HEIGHT = 128\nconst noMetrics: TodayMetric[] = []\nconst noHours: number[] = []\n\nconst Realtime4 = (props: Realtime4Props) => {\n  const {\n    hourly = noHours,\n    hourlyLabel = 'Orders',\n    metrics = noMetrics,\n    errorMessage,\n    status,\n    title,\n    yesterdayHourly,\n  } = props\n  const id = useChartId()\n  const state = getBlockState(status, metrics)\n  const shown: TodayMetric[] =\n    status === 'loading'\n      ? getStatPlaceholders(3).map((placeholder) => ({\n          id: placeholder.key,\n          label: placeholder.label,\n          today: 0,\n          yesterday: 0,\n        }))\n      : metrics\n\n  const lead = metrics[0]\n  const leadChange = lead && lead.yesterday > 0 ? lead.today / lead.yesterday - 1 : 0\n  const {\n    description = lead\n      ? `${formatNumber(Math.round(lead.today))} ${lead.label.toLowerCase()} since midnight, ${Math.abs(Math.round(leadChange * 100))}% ${leadChange >= 0 ? 'ahead of' : 'behind'} yesterday at this time.`\n      : 'Since midnight, against yesterday at the same time.',\n  } = props\n  const current = hourly.length - 1\n  const data = HOUR_LABELS.map((label, hour) => ({\n    label,\n    today: hour < hourly.length ? hourly[hour] : undefined,\n    yesterday: yesterdayHourly?.[hour],\n  }))\n  const formatCount = (value: number) =>\n    Number.isFinite(value) ? formatNumber(value) : 'Not yet'\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={state}>\n      <Card>\n        <CardContent>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex items-start justify-between gap-3'>\n              <div className='flex min-w-0 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              </div>\n              {!state && <LiveBadge />}\n            </header>\n            <StatGroup className='grid-cols-3 gap-x-4'>\n              {shown.map((metric) => (\n                <Stat key={metric.id}>\n                  <StatLabel className='truncate'>{metric.label}</StatLabel>\n                  <StatValue className='text-2xl @md:text-3xl'>\n                    <LiveNumber format={metric.format} value={metric.today} />\n                  </StatValue>\n                  <StatChange\n                    comparison='yesterday at this time'\n                    metric={{\n                      formatter: (value) =>\n                        metric.format\n                          ? metric.format(value)\n                          : formatNumber(Math.round(value)),\n                      key: metric.id,\n                      label: metric.label,\n                      previous: metric.yesterday,\n                      value: metric.today,\n                    }}\n                    variant='short'\n                  />\n                </Stat>\n              ))}\n            </StatGroup>\n            <div className='flex flex-col gap-1.5'>\n              <span className='text-muted-foreground text-sm'>\n                {hourlyLabel} per hour\n              </span>\n              {state && <ChartPlaceholder height={CHART_HEIGHT} />}\n              {!state && (\n                <div\n                  aria-hidden\n                  className='w-full tabular-nums'\n                  style={{ height: CHART_HEIGHT }}\n                >\n                  <ResponsiveContainer width='100%' height='100%'>\n                    <ComposedChart\n                      accessibilityLayer={false}\n                      data={data}\n                      margin={{ top: 8, right: 0, bottom: 0, left: 0 }}\n                    >\n                      <defs>\n                        <ChartHatch color='var(--color-chart-2)' id={`${id}-counting`} />\n                      </defs>\n                      <XAxis\n                        {...chartAxisProps}\n                        dataKey='label'\n                        interval={0}\n                        tickFormatter={(value: string, index: number) =>\n                          HOUR_TICKS.includes(index) ? value : ''\n                        }\n                      />\n                      <Tooltip\n                        content={({ active, label, payload }) => (\n                          <ChartTooltipContent\n                            active={active}\n                            label={label === undefined ? undefined : String(label)}\n                            payload={payload}\n                            valueFormatter={formatCount}\n                          />\n                        )}\n                        cursor={chartBarCursor}\n                      />\n                      <Bar\n                        dataKey='today'\n                        fill='var(--color-chart-2)'\n                        isAnimationActive={false}\n                        name='Today'\n                        radius={[2, 2, 0, 0]}\n                      >\n                        {data.map((_, hour) => (\n                          <Cell\n                            key={hour}\n                            fill={\n                              hour === current\n                                ? `url(#${id}-counting)`\n                                : 'var(--color-chart-2)'\n                            }\n                          />\n                        ))}\n                      </Bar>\n                      {yesterdayHourly && (\n                        <Line\n                          dataKey='yesterday'\n                          dot={false}\n                          isAnimationActive={false}\n                          name='Yesterday'\n                          stroke='var(--color-muted-foreground)'\n                          strokeDasharray='4 3'\n                          strokeWidth={1.5}\n                          type='monotone'\n                        >\n                          <LabelList\n                            content={({ index, x, y }) =>\n                              index === data.length - 1 ? (\n                                <text\n                                  x={Number(x)}\n                                  y={Number(y) - 8}\n                                  textAnchor='end'\n                                  fontSize={12}\n                                  fill='var(--color-muted-foreground)'\n                                >\n                                  Yesterday\n                                </text>\n                              ) : null\n                            }\n                            dataKey='yesterday'\n                          />\n                        </Line>\n                      )}\n                    </ComposedChart>\n                  </ResponsiveContainer>\n                </div>\n              )}\n              {!state && (\n                <p className='sr-only'>\n                  {`${hourlyLabel} per hour today: ${hourly.join(', ')}. The current hour is still counting.`}\n                </p>\n              )}\n              {state !== 'error' && state !== 'empty' && (\n                <p className='text-muted-foreground text-xs'>\n                  The hatched bar is the hour still counting. Changes compare with\n                  yesterday up to the same time.\n                </p>\n              )}\n            </div>\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport {\n  Realtime4,\n  exampleProps as realtime4ExampleProps,\n  type Realtime4Props,\n  type TodayMetric,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/realtime/realtime-04.tsx"
    }
  ],
  "meta": {
    "shows": "Running totals since midnight against yesterday at the same time, with orders per hour beside yesterday as a dashed line.",
    "size": "half",
    "usedWith": [
      "realtime-03",
      "chart-panel-01"
    ],
    "component": "Realtime4",
    "props": "{ description?: string; hourly?: number[]; hourlyLabel?: string; metrics?: { format?: (value: number) => string; id: string; label: string; today: number; yesterday: number }[]; title: string; yesterdayHourly?: number[]; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
