{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "metric-list-04",
  "description": "A list of metrics where picking one charts it beside the list, against its 30-day average.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/chart-panel.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json",
    "https://www.dashboardblocks.com/r/radix/metric-list.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/metric-list/metric-list-04.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  BlockStateRows,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  ChartPlaceholder,\n  ChartTooltipContent,\n  chartActiveDot,\n  chartAxisProps,\n  chartGridProps,\n  chartLastDot,\n  chartLineCursor,\n  chartReferenceLabel,\n  chartReferenceProps,\n  chartValueAxisWidth,\n  useChartId,\n} from '@/registry/components/dashboardblocks/chart'\nimport {\n  ChartPanelFigure,\n  ChartPanelTable,\n} from '@/registry/components/dashboardblocks/chart-panel'\nimport { KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport {\n  type Metric,\n  MetricChange,\n  MetricSparkline,\n  describeHistory,\n  formatMetricValue,\n  getMetricFormatter,\n} from '@/registry/components/dashboardblocks/metric-list'\nimport { formatDate } from '@/registry/lib/dashboardblocks/format'\nimport { useId, useState } from 'react'\nimport {\n  Area,\n  AreaChart,\n  CartesianGrid,\n  ReferenceLine,\n  ResponsiveContainer,\n  Tooltip,\n  XAxis,\n  YAxis,\n} from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\nimport { cn } from '@/lib/utils'\n\ninterface WatchedMetric extends Metric {\n  /** One value per entry in `labels`, oldest first. */\n  history: number[]\n}\n\ninterface MetricList4Props extends BlockStateProps {\n  /** Follows the previous value in each change, such as `30 days ago`. */\n  comparison: string\n  defaultSelected?: string\n  description: string\n  labels: string[]\n  /** Leave out, or pass none, to show the empty state. */\n  metrics?: WatchedMetric[]\n  onSelectedChange?: (key: string) => void\n  title: string\n}\n\nconst NOW = Date.UTC(2026, 8, 25)\n\nconst exampleDaily = (\n  end: number,\n  slope: number,\n  wobble: number,\n  weekendDip = wobble,\n  digits = 0,\n) =>\n  Array.from({ length: 30 }, (_, day) => {\n    const weekly = day % 7 === 5 || day % 7 === 6 ? -weekendDip : 0\n    const value = end - (29 - day) * slope + Math.sin(day / 2.1) * wobble * 0.6 + weekly\n    return Number(value.toFixed(digits))\n  })\n\nconst fromHistory = (history: number[]) => ({\n  history,\n  previous: history[0],\n  value: history[history.length - 1],\n})\n\nconst exampleProps: MetricList4Props = {\n  comparison: '30 days ago',\n  description: 'Daily over the last 30 days. Pick a metric to chart it.',\n  labels: Array.from({ length: 30 }, (_, day) =>\n    formatDate(new Date(NOW - (29 - day) * 86_400_000), 'day'),\n  ),\n  metrics: [\n    {\n      ...fromHistory(exampleDaily(13_310, 38, 420)),\n      format: 'compact',\n      key: 'dau',\n      label: 'Active users',\n    },\n    {\n      ...fromHistory(exampleDaily(196_750, 420, 500, 0)),\n      format: 'currency-compact',\n      key: 'mrr',\n      label: 'MRR',\n    },\n    {\n      ...fromHistory(exampleDaily(142, 0.6, 18)),\n      key: 'signups',\n      label: 'Sign-ups',\n    },\n    {\n      ...fromHistory(exampleDaily(0.42, -0.006, 0.06, -0.04, 2)),\n      changeType: 'points',\n      format: 'percent',\n      goodDirection: 'down',\n      key: 'errors',\n      label: 'Error rate',\n    },\n    {\n      ...fromHistory(exampleDaily(412, 1.8, 16)),\n      format: 'milliseconds',\n      goodDirection: 'down',\n      key: 'latency',\n      label: 'p95 API latency',\n    },\n  ],\n  title: 'Watchlist',\n}\n\nconst MetricList4 = (props: MetricList4Props) => {\n  const {\n    comparison,\n    defaultSelected,\n    description,\n    labels,\n    metrics = [],\n    onSelectedChange,\n    errorMessage,\n    status,\n    title,\n  } = props\n  const id = useId()\n  const gradient = useChartId()\n  const [selected, setSelected] = useState(defaultSelected ?? metrics[0]?.key)\n  const metric = metrics.find((item) => item.key === selected) ?? metrics[0]\n  const state = getBlockState(status, metric)\n  const showHeading = state === undefined || state === 'loading'\n\n  const format = getMetricFormatter(metric?.format)\n  const data = (metric?.history ?? []).map((value, index) => ({\n    label: labels[index] ?? String(index + 1),\n    value,\n  }))\n  const average =\n    data.reduce((sum, point) => sum + point.value, 0) / Math.max(1, data.length)\n  const last = data.length - 1\n\n  return (\n    <Card>\n      <CardContent>\n        <BlockStateProvider errorMessage={errorMessage} state={state}>\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            <div className='grid border-t @2xl:grid-cols-[19rem_minmax(0,1fr)] @2xl:gap-8'>\n              <fieldset className='min-w-0'>\n                <legend className='sr-only'>Metric to chart</legend>\n                {state === 'loading' && (\n                  <BlockStateRows\n                    className='border-b'\n                    rows={Math.max(metrics.length, 5)}\n                  />\n                )}\n                {!state && metric && (\n                  <ul className='divide-y border-b'>\n                    {metrics.map((item) => (\n                      <li key={item.key}>\n                        <label className='hover:bg-muted/50 has-checked:bg-muted/60 has-focus-visible:outline-ring relative grid cursor-pointer grid-cols-[minmax(0,1fr)_3rem_5.5rem] items-center gap-x-3 py-2.5 pr-2 pl-3 transition-colors has-focus-visible:outline-2 has-focus-visible:-outline-offset-2 motion-reduce:transition-none'>\n                          <input\n                            type='radio'\n                            name={id}\n                            value={item.key}\n                            checked={item.key === metric.key}\n                            onChange={() => {\n                              setSelected(item.key)\n                              onSelectedChange?.(item.key)\n                            }}\n                            className='sr-only'\n                          />\n                          <span\n                            aria-hidden\n                            className={cn(\n                              'bg-foreground absolute inset-y-0 left-0 w-0.5',\n                              item.key !== metric.key && 'hidden',\n                            )}\n                          />\n                          <span\n                            className={cn(\n                              'text-sm',\n                              item.key === metric.key && 'font-medium',\n                            )}\n                          >\n                            {item.label}\n                          </span>\n                          <span aria-hidden>\n                            <MetricSparkline\n                              animated={false}\n                              className='w-full'\n                              color={\n                                item.key === metric.key\n                                  ? 'var(--color-chart-2)'\n                                  : 'var(--color-muted-foreground)'\n                              }\n                              data={item.history}\n                              formatter={getMetricFormatter(item.format)}\n                              label={describeHistory(item)}\n                              labels={labels}\n                            />\n                          </span>\n                          <span className='flex flex-col items-end gap-0.5'>\n                            <span className='text-sm font-medium whitespace-nowrap tabular-nums'>\n                              {formatMetricValue(item, item.value)}\n                            </span>\n                            <MetricChange comparison={comparison} metric={item} />\n                          </span>\n                        </label>\n                      </li>\n                    ))}\n                  </ul>\n                )}\n              </fieldset>\n              <section\n                aria-labelledby={showHeading ? `${id}-heading` : undefined}\n                className='flex min-w-0 flex-col gap-4 pt-5'\n              >\n                <div className='flex flex-col gap-1'>\n                  {showHeading && (\n                    <h3 id={`${id}-heading`} className='text-sm font-medium'>\n                      <BlockReady className='text-sm'>{metric?.label}</BlockReady>\n                    </h3>\n                  )}\n                  <KPIValue format={format} value={metric?.value} />\n                  {metric && (\n                    <MetricChange\n                      comparison={comparison}\n                      metric={metric}\n                      variant='sentence'\n                    />\n                  )}\n                </div>\n                {state || !metric ? (\n                  <ChartPanelFigure className='h-48 @2xl:h-56'>\n                    <ChartPlaceholder height='100%' />\n                  </ChartPanelFigure>\n                ) : (\n                  <>\n                    <ChartPanelFigure className='h-48 @2xl:h-56'>\n                      <ResponsiveContainer width='100%' height='100%'>\n                        <AreaChart\n                          aria-label={metric.label}\n                          data={data}\n                          margin={{ top: 8, right: 8, bottom: 0, left: 0 }}\n                        >\n                          <defs>\n                            <linearGradient id={gradient} x1='0' x2='0' y1='0' y2='1'>\n                              <stop\n                                offset='0%'\n                                stopColor='var(--color-chart-2)'\n                                stopOpacity={0.3}\n                              />\n                              <stop\n                                offset='100%'\n                                stopColor='var(--color-chart-2)'\n                                stopOpacity={0}\n                              />\n                            </linearGradient>\n                          </defs>\n                          <CartesianGrid {...chartGridProps} />\n                          <XAxis\n                            {...chartAxisProps}\n                            dataKey='label'\n                            interval='preserveStartEnd'\n                            minTickGap={40}\n                          />\n                          <YAxis\n                            {...chartAxisProps}\n                            domain={['auto', 'auto']}\n                            tickFormatter={(value: number) => format(value)}\n                            width={chartValueAxisWidth}\n                          />\n                          <Tooltip\n                            content={(tooltipProps) => (\n                              <ChartTooltipContent\n                                {...tooltipProps}\n                                valueFormatter={format}\n                              />\n                            )}\n                            cursor={chartLineCursor}\n                          />\n                          <ReferenceLine\n                            {...chartReferenceProps}\n                            y={average}\n                            label={{\n                              ...chartReferenceLabel,\n                              position: 'insideBottomLeft',\n                              value: `30-day average ${format(average)}`,\n                            }}\n                          />\n                          <Area\n                            key={metric.key}\n                            activeDot={chartActiveDot}\n                            dataKey='value'\n                            dot={chartLastDot(data.length, 'var(--color-chart-2)')}\n                            fill={`url(#${gradient})`}\n                            name={metric.label}\n                            stroke='var(--color-chart-2)'\n                            strokeWidth={2}\n                            type='monotone'\n                          />\n                        </AreaChart>\n                      </ResponsiveContainer>\n                    </ChartPanelFigure>\n                    <ChartPanelTable\n                      caption={`${metric.label} by day, ${data[0]?.label ?? ''} to ${data[last]?.label ?? ''}`}\n                      columns={[\n                        { key: 'label', label: 'Day' },\n                        { format, key: 'value', label: metric.label },\n                      ]}\n                      rows={data}\n                    />\n                  </>\n                )}\n              </section>\n            </div>\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport { MetricList4, exampleProps as metricList4ExampleProps, type MetricList4Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/metric-list/metric-list-04.tsx"
    }
  ],
  "meta": {
    "shows": "A list of metrics where picking one charts it beside the list, against its 30-day average.",
    "size": "full",
    "usedWith": [
      "stat-group-03",
      "metric-list-01",
      "chart-panel-01"
    ],
    "component": "MetricList4",
    "props": "{ comparison: string; defaultSelected?: string; description: string; labels: string[]; metrics?: { history: number[]; changeType?: \"percent\" | \"points\"; format?: \"number\" | \"compact\" | \"currency\" | \"currency-compact\" | \"percent\" | \"duration\" | \"milliseconds\" | (value: number) => string; goodDirection?: \"up\" | \"down\"; key: string; label: string; previous?: number; value: number }[]; onSelectedChange?: (key: string) => void; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
