{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "comparison-01",
  "description": "Each metric this period and last, with the difference and the change, under a sentence saying how many improved.",
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/format.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json",
    "https://www.dashboardblocks.com/r/radix/table.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/comparison/comparison-01.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { KPIChange } from '@/registry/components/dashboardblocks/kpi'\nimport {\n  DataTable,\n  DataTableBody,\n  DataTableCell,\n  DataTableHead,\n  DataTableHeader,\n  DataTableRow,\n  DataTableStateRows,\n} from '@/registry/components/dashboardblocks/table'\nimport {\n  formatCurrency,\n  formatNumber,\n  formatPercent,\n  formatPoints,\n  formatSigned,\n} from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface ComparedMetric {\n  current: number\n  differenceFormatter?: (value: number) => string\n  formatter?: (value: number) => string\n  /** Use `down` for metrics like churn or latency, where a decrease is good. */\n  goodDirection?: 'up' | 'down'\n  label: string\n  previous: number\n}\n\ninterface Comparison1Props extends BlockStateProps {\n  currentLabel: string\n  /** Defaults to a sentence saying how many metrics improved. */\n  description?: string\n  /** Leave out, or pass none, to show the empty state. */\n  metrics?: ComparedMetric[]\n  previousLabel: string\n  title: string\n}\n\nconst exampleProps: Comparison1Props = {\n  currentLabel: 'September',\n  metrics: [\n    {\n      current: 48_210,\n      formatter: (value) => formatCurrency(value),\n      label: 'Revenue',\n      previous: 42_890,\n    },\n    { current: 1_284, label: 'Orders', previous: 1_210 },\n    {\n      current: 37.55,\n      formatter: (value) => formatCurrency(value, { precision: 'exact' }),\n      label: 'Avg order value',\n      previous: 35.45,\n    },\n    { current: 342, label: 'New customers', previous: 318 },\n    {\n      current: 2.1,\n      differenceFormatter: (value) => formatPoints(value, { minimumFractionDigits: 1 }),\n      formatter: (value) => formatPercent(value / 100, { minimumFractionDigits: 1 }),\n      goodDirection: 'down',\n      label: 'Refund rate',\n      previous: 2.4,\n    },\n    {\n      current: 4.2,\n      formatter: (value) => `${formatNumber(value, { minimumFractionDigits: 1 })} h`,\n      goodDirection: 'down',\n      label: 'First response time',\n      previous: 3.8,\n    },\n  ],\n  previousLabel: 'August',\n  title: 'Month over month',\n}\n\nconst isGood = (metric: ComparedMetric, difference: number) =>\n  metric.goodDirection === 'down' ? difference < 0 : difference > 0\n\nfunction summarize(metrics: ComparedMetric[], previousLabel: string) {\n  const improved = metrics.filter((metric) =>\n    isGood(metric, metric.current - metric.previous),\n  ).length\n  const worse = metrics.filter(\n    (metric) =>\n      metric.current !== metric.previous &&\n      !isGood(metric, metric.current - metric.previous),\n  )\n  const lead = `${improved} of ${metrics.length} metrics improved on ${previousLabel}`\n  if (worse.length === 0) return `${lead}.`\n  return `${lead}; ${worse.map((metric) => metric.label.toLowerCase()).join(' and ')} got worse.`\n}\n\nconst Comparison1 = (props: Comparison1Props) => {\n  const { currentLabel, metrics = [], previousLabel, errorMessage, status, title } = props\n  const { description = summarize(metrics, previousLabel) } = props\n\n  return (\n    <Card>\n      <CardContent>\n        <BlockStateProvider\n          errorMessage={errorMessage}\n          state={getBlockState(status, metrics)}\n        >\n          <section className='@container/data-table flex flex-col gap-3'>\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            <DataTable caption={`${title}: ${description}`} className='border-t'>\n              <DataTableHeader>\n                <DataTableRow>\n                  <DataTableHead>Metric</DataTableHead>\n                  <DataTableHead align='end'>{currentLabel}</DataTableHead>\n                  <DataTableHead align='end'>{previousLabel}</DataTableHead>\n                  <DataTableHead align='end'>Difference</DataTableHead>\n                  <DataTableHead align='end'>Change</DataTableHead>\n                </DataTableRow>\n              </DataTableHeader>\n              <DataTableBody>\n                <DataTableStateRows colSpan={5} rows={6} />\n                {!status &&\n                  metrics.map((metric) => {\n                    const format =\n                      metric.formatter ?? ((value: number) => formatNumber(value))\n                    const difference = metric.current - metric.previous\n                    return (\n                      <DataTableRow key={metric.label}>\n                        <DataTableCell primary>{metric.label}</DataTableCell>\n                        <DataTableCell align='end' className='font-medium'>\n                          {format(metric.current)}\n                        </DataTableCell>\n                        <DataTableCell align='end' className='text-muted-foreground'>\n                          {format(metric.previous)}\n                        </DataTableCell>\n                        <DataTableCell align='end'>\n                          {formatSigned(difference, metric.differenceFormatter ?? format)}\n                        </DataTableCell>\n                        <DataTableCell align='end'>\n                          <KPIChange\n                            comparison={`in ${previousLabel}`}\n                            goodDirection={metric.goodDirection}\n                            inline\n                            previous={metric.previous}\n                            value={metric.current}\n                            variant='short'\n                          />\n                        </DataTableCell>\n                      </DataTableRow>\n                    )\n                  })}\n              </DataTableBody>\n            </DataTable>\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport { Comparison1, exampleProps as comparison1ExampleProps, type Comparison1Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/comparison/comparison-01.tsx"
    }
  ],
  "meta": {
    "shows": "Each metric this period and last, with the difference and the change, under a sentence saying how many improved.",
    "size": "full",
    "usedWith": [
      "stat-group-01",
      "line-chart-kpi-01",
      "comparison-03"
    ],
    "component": "Comparison1",
    "props": "{ currentLabel: string; description?: string; metrics?: { current: number; differenceFormatter?: (value: number) => string; formatter?: (value: number) => string; goodDirection?: \"up\" | \"down\"; label: string; previous: number }[]; previousLabel: string; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
