{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "breakdown-02",
  "description": "One bar per group showing its mix of categories, sorted by a chosen category's share, which is labelled on every row.",
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/breakdown.json",
    "https://www.dashboardblocks.com/r/radix/table.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/breakdown/breakdown-02.tsx",
      "content": "'use client'\n\nimport {\n  BlockStateProvider,\n  BlockStateRows,\n  BlockStateText,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  BreakdownBar,\n  BreakdownLegend,\n  formatShare,\n} from '@/registry/components/dashboardblocks/breakdown'\nimport {\n  DataTable,\n  DataTableBody,\n  DataTableCell,\n  DataTableHead,\n  DataTableHeader,\n  DataTableRow,\n  DataTableRowHead,\n} from '@/registry/components/dashboardblocks/table'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface CompositionCategory {\n  color: string\n  key: string\n  label: string\n}\n\ninterface CompositionGroup {\n  label: string\n  values: Record<string, number>\n}\n\ninterface Breakdown2Props extends BlockStateProps {\n  categories: CompositionCategory[]\n  description: string\n  /** Leave out, or pass none, to show the empty state. */\n  groups?: CompositionGroup[]\n  /** What the groups are, plural, for the summary sentence. @default 'groups' */\n  groupsLabel?: string\n  highlight: string\n  title: string\n}\n\nconst exampleProps: Breakdown2Props = {\n  categories: [\n    { color: 'var(--color-chart-2)', key: 'mobile', label: 'Mobile' },\n    { color: 'var(--color-chart-3)', key: 'desktop', label: 'Desktop' },\n    { color: 'var(--color-chart-4)', key: 'tablet', label: 'Tablet' },\n  ],\n  description: 'Sessions by device in each region over the last 30 days.',\n  groups: [\n    { label: 'Asia Pacific', values: { desktop: 9_120, mobile: 21_480, tablet: 1_940 } },\n    { label: 'Latin America', values: { desktop: 4_310, mobile: 8_760, tablet: 820 } },\n    { label: 'Europe', values: { desktop: 14_920, mobile: 13_410, tablet: 2_310 } },\n    {\n      label: 'North America',\n      values: { desktop: 18_240, mobile: 14_630, tablet: 3_180 },\n    },\n    { label: 'Africa', values: { desktop: 1_120, mobile: 3_870, tablet: 140 } },\n  ],\n  groupsLabel: 'regions',\n  highlight: 'mobile',\n  title: 'Device mix by region',\n}\n\nconst Breakdown2 = (props: Breakdown2Props) => {\n  const {\n    categories,\n    description,\n    groups = [],\n    groupsLabel = 'groups',\n    highlight,\n    errorMessage,\n    status,\n    title,\n  } = props\n  const state = getBlockState(status, groups)\n  const highlighted = categories.find((category) => category.key === highlight)\n  const rows = groups\n    .map((group) => {\n      const total = categories.reduce(\n        (sum, category) => sum + (group.values[category.key] ?? 0),\n        0,\n      )\n      const leader = categories.reduce<CompositionCategory | undefined>(\n        (best, category) =>\n          best && (group.values[best.key] ?? 0) >= (group.values[category.key] ?? 0)\n            ? best\n            : category,\n        undefined,\n      )\n      return {\n        group,\n        leads: leader?.key === highlight,\n        share: total > 0 ? (group.values[highlight] ?? 0) / total : 0,\n        total,\n      }\n    })\n    .sort((a, b) => b.share - a.share)\n  const leads = rows.filter((row) => row.leads).length\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 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-3 border-t pt-4'>\n              {state ? (\n                <BlockStateText className='text-sm' />\n              ) : (\n                highlighted && (\n                  <p className='text-muted-foreground text-sm'>\n                    <span className='text-foreground font-medium'>\n                      {highlighted.label}\n                    </span>{' '}\n                    leads in {leads} of {rows.length} {groupsLabel}.\n                  </p>\n                )\n              )}\n              <BreakdownLegend\n                items={categories.map((category) => ({\n                  color: category.color,\n                  label: category.label,\n                }))}\n              />\n            </div>\n            {state === 'loading' && <BlockStateRows rows={5} />}\n            {!state && (\n              <DataTable density='compact' caption={`${title}: ${description}`}>\n                <DataTableHeader>\n                  <DataTableRow>\n                    <DataTableHead>\n                      <span className='sr-only'>Group</span>\n                    </DataTableHead>\n                    <DataTableHead>\n                      <span className='sr-only'>Share by category</span>\n                    </DataTableHead>\n                    <DataTableHead align='end'>{highlighted?.label}</DataTableHead>\n                  </DataTableRow>\n                </DataTableHeader>\n                <DataTableBody>\n                  {rows.map(({ group, share, total }) => (\n                    <DataTableRow key={group.label}>\n                      <DataTableRowHead className='w-32 whitespace-nowrap'>\n                        {group.label}\n                      </DataTableRowHead>\n                      <DataTableCell>\n                        <BreakdownBar\n                          className='h-2.5'\n                          segments={categories.map((category) => ({\n                            color: category.color,\n                            label: category.label,\n                            value: group.values[category.key] ?? 0,\n                          }))}\n                        />\n                        <span className='sr-only'>\n                          {categories\n                            .map(\n                              (category) =>\n                                `${category.label} ${formatShare(group.values[category.key] ?? 0, total)}`,\n                            )\n                            .join(', ')}\n                        </span>\n                      </DataTableCell>\n                      <DataTableCell align='end' className='w-16'>\n                        {formatShare(share, 1)}\n                      </DataTableCell>\n                    </DataTableRow>\n                  ))}\n                </DataTableBody>\n              </DataTable>\n            )}\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport { Breakdown2, exampleProps as breakdown2ExampleProps, type Breakdown2Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/breakdown/breakdown-02.tsx"
    }
  ],
  "meta": {
    "shows": "One bar per group showing its mix of categories, sorted by a chosen category's share, which is labelled on every row.",
    "size": "half",
    "usedWith": [
      "chart-panel-03",
      "data-table-02"
    ],
    "component": "Breakdown2",
    "props": "{ categories: { color: string; key: string; label: string }[]; description: string; groups?: { label: string; values: Record<string, number> }[]; groupsLabel?: string; highlight: string; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
