{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "leaderboard-04",
  "description": "Products ranked by revenue with how far each moved in the ranking since the last period.",
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/format.json",
    "https://www.dashboardblocks.com/r/radix/leaderboard.json",
    "https://www.dashboardblocks.com/r/radix/table.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/leaderboard/leaderboard-04.tsx",
      "content": "import {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  LeaderboardRank,\n  LeaderboardRankChange,\n} from '@/registry/components/dashboardblocks/leaderboard'\nimport {\n  DataTable,\n  DataTableBody,\n  DataTableCell,\n  DataTableHead,\n  DataTableHeader,\n  DataTableRow,\n  DataTableRowHead,\n  DataTableStateRows,\n} from '@/registry/components/dashboardblocks/table'\nimport {\n  formatCurrency,\n  formatNumber,\n  formatOrdinal,\n  joinDetails,\n} from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface Product {\n  category: string\n  /** Places moved since the last period, or `new` for a first entry. */\n  change: number | 'new'\n  id: number\n  name: string\n  revenue: number\n  unitsSold: number\n}\n\ninterface Leaderboard4Props extends BlockStateProps {\n  currency?: string\n  /** Defaults to a sentence about the biggest climber and new entries. */\n  description?: string\n  /** Names the last period in the sentence, such as `last week`. */\n  period?: string\n  /** Leave out, or pass none, to show the empty state. */\n  products?: Product[]\n  title: string\n}\n\nconst exampleProps: Leaderboard4Props = {\n  currency: 'USD',\n  period: 'last week',\n  products: [\n    {\n      id: 1,\n      name: 'Studio Monitor 27\"',\n      category: 'Displays',\n      revenue: 48_920,\n      unitsSold: 112,\n      change: 1,\n    },\n    {\n      id: 2,\n      name: 'Wireless Headphones',\n      category: 'Audio',\n      revenue: 41_370,\n      unitsSold: 287,\n      change: -1,\n    },\n    {\n      id: 3,\n      name: 'Ergonomic Chair',\n      category: 'Furniture',\n      revenue: 36_540,\n      unitsSold: 84,\n      change: 0,\n    },\n    {\n      id: 4,\n      name: 'Mechanical Keyboard',\n      category: 'Peripherals',\n      revenue: 22_815,\n      unitsSold: 169,\n      change: 3,\n    },\n    {\n      id: 5,\n      name: 'Desk Lamp Pro',\n      category: 'Lighting',\n      revenue: 14_260,\n      unitsSold: 203,\n      change: 'new',\n    },\n    {\n      id: 6,\n      name: 'Precision Mouse',\n      category: 'Peripherals',\n      revenue: 9_880,\n      unitsSold: 152,\n      change: -2,\n    },\n  ],\n  title: 'Top products',\n}\n\nfunction summarize(products: Product[], period: string) {\n  const climber = products\n    .map((product, index) => ({ ...product, rank: index + 1 }))\n    .filter((product) => typeof product.change === 'number' && product.change > 0)\n    .sort((a, b) => (b.change as number) - (a.change as number))[0]\n  const fresh = products.filter((product) => product.change === 'new')\n  const parts = [\n    climber &&\n      `${climber.name} climbed ${climber.change} ${climber.change === 1 ? 'place' : 'places'} to ${formatOrdinal(climber.rank)}`,\n    fresh.length === 1 && `${fresh[0].name} is new`,\n    fresh.length > 1 && `${fresh.length} products are new`,\n  ].filter(Boolean)\n  return parts.length > 0\n    ? `${parts.join(' and ')} since ${period}.`\n    : `No changes in the ranking since ${period}.`\n}\n\nconst Leaderboard4 = (props: Leaderboard4Props) => {\n  const {\n    currency = 'USD',\n    period = 'the last period',\n    products = [],\n    errorMessage,\n    status,\n    title,\n  } = props\n  const state = getBlockState(status, products)\n  const { description = summarize(products, period) } = props\n\n  return (\n    <Card>\n      <CardContent>\n        <BlockStateProvider errorMessage={errorMessage} state={state}>\n          <section className='@container 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} className='border-t'>\n              <DataTableHeader>\n                <DataTableRow>\n                  <DataTableHead className='w-10'>#</DataTableHead>\n                  <DataTableHead>Product</DataTableHead>\n                  <DataTableHead align='end'>Revenue</DataTableHead>\n                  <DataTableHead align='end' className='w-20'>\n                    Move\n                  </DataTableHead>\n                </DataTableRow>\n              </DataTableHeader>\n              <DataTableBody>\n                <DataTableStateRows colSpan={4} rows={6} />\n                {!state &&\n                  products.map((product, index) => (\n                    <DataTableRow key={product.id}>\n                      <DataTableCell className='pe-0'>\n                        <LeaderboardRank rank={index + 1} />\n                      </DataTableCell>\n                      <DataTableRowHead primary truncate>\n                        <span className='block truncate'>{product.name}</span>\n                        <span className='text-muted-foreground block truncate text-xs font-normal'>\n                          {joinDetails(\n                            product.category,\n                            `${formatNumber(product.unitsSold)} sold`,\n                          )}\n                        </span>\n                      </DataTableRowHead>\n                      <DataTableCell align='end' className='font-medium'>\n                        {formatCurrency(product.revenue, { currency })}\n                      </DataTableCell>\n                      <DataTableCell align='end'>\n                        <LeaderboardRankChange change={product.change} />\n                      </DataTableCell>\n                    </DataTableRow>\n                  ))}\n              </DataTableBody>\n            </DataTable>\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport {\n  Leaderboard4,\n  exampleProps as leaderboard4ExampleProps,\n  type Leaderboard4Props,\n  type Product,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/leaderboard/leaderboard-04.tsx"
    }
  ],
  "meta": {
    "shows": "Products ranked by revenue with how far each moved in the ranking since the last period.",
    "size": "half",
    "usedWith": [
      "leaderboard-01",
      "breakdown-01",
      "chart-panel-01"
    ],
    "component": "Leaderboard4",
    "props": "{ currency?: string; description?: string; period?: string; products?: { category: string; change: number | \"new\"; id: number; name: string; revenue: number; unitsSold: number }[]; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
