{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "insights-03",
  "description": "What contributed to a change, as bars either side of zero, with a sentence naming the biggest driver and the biggest drag.",
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/comparison.json",
    "https://www.dashboardblocks.com/r/radix/format.json",
    "https://www.dashboardblocks.com/r/radix/insights.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/insights/insights-03.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 { DivergingBar } from '@/registry/components/dashboardblocks/comparison'\nimport {\n  type InsightSegment,\n  InsightText,\n  getChangeTone,\n} from '@/registry/components/dashboardblocks/insights'\nimport { KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport {\n  formatNumber,\n  formatPercent,\n  formatPoints,\n  formatSigned,\n} from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface Contribution {\n  label: string\n  value: number\n}\n\ninterface Insights3Props extends BlockStateProps {\n  /** Leave out, or pass none, to show the empty state. */\n  contributions?: Contribution[]\n  description: string\n  formatter?: (value: number) => string\n  /** @default 'up' */\n  goodDirection?: 'up' | 'down'\n  metric: string\n  previous: number\n  title: string\n}\n\nconst exampleProps: Insights3Props = {\n  contributions: [\n    { label: 'Organic search', value: 812 },\n    { label: 'Referral', value: 264 },\n    { label: 'Email', value: 206 },\n    { label: 'Paid search', value: 168 },\n    { label: 'Partnerships', value: -48 },\n    { label: 'Paid social', value: -118 },\n  ],\n  description: 'Weekly signups by channel, Sep 14–20 against Sep 7–13.',\n  metric: 'Signups',\n  previous: 5_350,\n  title: 'What drove the change',\n}\n\nconst toneColors = {\n  negative: 'var(--color-red-600)',\n  neutral: 'var(--color-muted-foreground)',\n  positive: 'var(--color-emerald-600)',\n}\n\nconst Insights3 = (props: Insights3Props) => {\n  const {\n    contributions = [],\n    description,\n    formatter = (value) => formatNumber(value, { maximumFractionDigits: 0 }),\n    goodDirection = 'up',\n    metric,\n    previous,\n    errorMessage,\n    status,\n    title,\n  } = props\n  const state = getBlockState(status, contributions)\n  const rows = [...contributions].sort((a, b) => b.value - a.value)\n  const net = rows.reduce((sum, row) => sum + row.value, 0)\n  const max = Math.max(...rows.map((row) => Math.abs(row.value)), 0)\n  const points = (value: number) =>\n    previous === 0 ? 0 : Math.round((value / Math.abs(previous)) * 1000) / 10\n  const percent = points(net)\n  const netTone = getChangeTone(net, goodDirection)\n\n  const lead = net >= 0 ? rows[0] : rows[rows.length - 1]\n  const drag = net >= 0 ? rows[rows.length - 1] : rows[0]\n  const summary: InsightSegment[] = [\n    `${metric} `,\n    {\n      text:\n        net === 0\n          ? 'unchanged'\n          : `${net > 0 ? 'up' : 'down'} ${formatPercent(Math.abs(percent) / 100)}`,\n      tone: netTone,\n    },\n  ]\n  if (lead && Math.sign(lead.value) === Math.sign(net) && net !== 0) {\n    summary.push('. Biggest driver: ', `${lead.label} `, {\n      text: formatSigned(lead.value, formatter),\n      tone: getChangeTone(lead.value, goodDirection),\n    })\n  }\n  if (drag && Math.sign(drag.value) === -Math.sign(net) && net !== 0) {\n    summary.push('. Biggest drag: ', `${drag.label} `, {\n      text: formatSigned(drag.value, formatter),\n      tone: getChangeTone(drag.value, goodDirection),\n    })\n  }\n  summary.push('.')\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              <p className='text-muted-foreground text-sm'>{description}</p>\n            </header>\n            <div className='flex flex-col gap-5 border-t pt-4'>\n              <div className='flex flex-col gap-1'>\n                <div className='flex flex-wrap items-baseline gap-x-2'>\n                  <KPIValue\n                    value={net}\n                    format={(value) => formatSigned(value, formatter)}\n                  />\n                  {!state && (\n                    <span className='text-muted-foreground text-sm tabular-nums'>\n                      {formatter(previous)} <span aria-hidden>→</span>\n                      <span className='sr-only'>to</span> {formatter(previous + net)}\n                    </span>\n                  )}\n                </div>\n                <BlockReady className='text-sm'>\n                  <InsightText segments={summary} />\n                </BlockReady>\n              </div>\n              {state === 'loading' && <BlockStateRows rows={6} />}\n              {!state && (\n                <>\n                  <ul className='flex flex-col gap-3'>\n                    {rows.map((row) => (\n                      <li\n                        key={row.label}\n                        className='grid grid-cols-[minmax(0,1fr)_auto] items-center gap-x-4 gap-y-1.5 @md:grid-cols-[8rem_minmax(0,1fr)_9rem]'\n                      >\n                        <span className='truncate text-sm'>{row.label}</span>\n                        <span className='text-right text-sm whitespace-nowrap tabular-nums @md:order-last'>\n                          <span className='font-medium'>\n                            {formatSigned(row.value, formatter)}\n                          </span>{' '}\n                          <span className='text-muted-foreground text-xs'>\n                            ({formatSigned(points(row.value), formatPoints)})\n                          </span>\n                        </span>\n                        <DivergingBar\n                          className='col-span-2 @md:col-span-1'\n                          color={toneColors[getChangeTone(row.value, goodDirection)]}\n                          max={max}\n                          value={row.value}\n                        />\n                      </li>\n                    ))}\n                  </ul>\n                  <div\n                    aria-hidden\n                    className='text-muted-foreground grid grid-cols-[minmax(0,1fr)] gap-x-4 text-xs @md:grid-cols-[8rem_minmax(0,1fr)_9rem]'\n                  >\n                    <span className='hidden @md:block' />\n                    <span className='flex justify-between'>\n                      <span>Pulled down</span>\n                      <span>Pushed up</span>\n                    </span>\n                  </div>\n                  <p className='text-muted-foreground border-t pt-4 text-xs'>\n                    Points are each segment's share of the change, as a percentage of last\n                    period's {metric.toLowerCase()}. They add up to the total change.\n                  </p>\n                </>\n              )}\n            </div>\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport { Insights3, exampleProps as insights3ExampleProps, type Insights3Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/insights/insights-03.tsx"
    }
  ],
  "meta": {
    "shows": "What contributed to a change, as bars either side of zero, with a sentence naming the biggest driver and the biggest drag.",
    "size": "half",
    "usedWith": [
      "insights-02",
      "chart-panel-01",
      "breakdown-01"
    ],
    "component": "Insights3",
    "props": "{ contributions?: { label: string; value: number }[]; description: string; formatter?: (value: number) => string; goodDirection?: \"up\" | \"down\"; metric: string; previous: number; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
