{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "spend-02",
  "description": "The total spent and where it is heading, then one budget bar per service, naming the ones over budget.",
  "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/kpi.json",
    "https://www.dashboardblocks.com/r/radix/spend.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/spend/spend-02.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  BlockStateRows,\n  BlockStateText,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport {\n  BudgetBar,\n  BudgetStatusLabel,\n  SpendKey,\n  getBudgetPace,\n} from '@/registry/components/dashboardblocks/spend'\nimport { formatCurrency } from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface SpendLine {\n  budget: number\n  label: string\n  spent: number\n}\n\ninterface Spend2Props extends BlockStateProps {\n  /** @default 'USD' */\n  currency?: string\n  /** Defaults to a sentence naming the services over budget, or on pace to go over. */\n  description?: string\n  /** Share of the period that has passed, 0–1. */\n  elapsed: number\n  /** Leave out, or pass none, to show the empty state. */\n  lines?: SpendLine[]\n  title: string\n}\n\nconst exampleProps: Spend2Props = {\n  elapsed: 25 / 30,\n  lines: [\n    { budget: 18_000, label: 'Compute', spent: 16_940 },\n    { budget: 9_500, label: 'Database', spent: 8_120 },\n    { budget: 6_000, label: 'Storage', spent: 4_310 },\n    { budget: 4_500, label: 'Network', spent: 4_870 },\n    { budget: 3_000, label: 'Observability', spent: 2_280 },\n  ],\n  title: 'Spend by service',\n}\n\nconst joinNames = (names: string[]) =>\n  names.length < 2\n    ? names.join('')\n    : `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`\n\nconst describe = (lines: SpendLine[], elapsed: number) => {\n  const over = lines\n    .filter((line) => getBudgetPace({ ...line, elapsed }).status === 'over')\n    .map((line) => line.label)\n  const atRisk = lines\n    .filter((line) => getBudgetPace({ ...line, elapsed }).status === 'at-risk')\n    .map((line) => line.label)\n  const parts = [\n    over.length > 0 &&\n      `${joinNames(over)} ${over.length === 1 ? 'is' : 'are'} over budget`,\n    atRisk.length > 0 &&\n      `${joinNames(atRisk)} ${atRisk.length === 1 ? 'is' : 'are'} on pace to go over`,\n  ].filter(Boolean)\n  if (parts.length > 0) return `${parts.join(', and ')}.`\n  return 'Every service is on pace to stay within budget.'\n}\n\nconst Spend2 = (props: Spend2Props) => {\n  const { currency = 'USD', elapsed, lines = [], errorMessage, status, title } = props\n  const { description = describe(lines, elapsed) } = props\n  const state = getBlockState(status, lines)\n  const format = (value: number) =>\n    formatCurrency(value, { compact: value >= 10_000, currency })\n  const totals = lines.reduce(\n    (sum, line) => ({ budget: sum.budget + line.budget, spent: sum.spent + line.spent }),\n    { budget: 0, spent: 0 },\n  )\n  const total = getBudgetPace({ ...totals, elapsed })\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              <BlockReady className='text-sm'>\n                <p className='text-muted-foreground text-sm'>{description}</p>\n              </BlockReady>\n            </header>\n            <div className='flex flex-col gap-1 border-t pt-4'>\n              <KPIValue\n                value={totals.spent}\n                format={(value) => formatCurrency(value, { currency })}\n              />\n              {state === 'loading' ? (\n                <BlockStateText className='text-sm' />\n              ) : state ? null : (\n                <p className='text-muted-foreground text-sm'>\n                  Spent of <span className='tabular-nums'>{format(totals.budget)}</span>,\n                  on pace for{' '}\n                  <span className='text-foreground font-medium tabular-nums'>\n                    {format(total.projected)}\n                  </span>{' '}\n                  by the end of the month.\n                </p>\n              )}\n            </div>\n            {state === 'loading' && <BlockStateRows className='border-t' rows={5} />}\n            {!state && (\n              <>\n                <ul className='flex flex-col border-t'>\n                  {lines.map((line) => {\n                    const pace = getBudgetPace({\n                      budget: line.budget,\n                      elapsed,\n                      spent: line.spent,\n                    })\n                    return (\n                      <li key={line.label} className='flex flex-col gap-2 border-b py-3'>\n                        <div className='flex flex-wrap items-baseline justify-between gap-x-3 gap-y-1'>\n                          <span className='flex items-baseline gap-2 text-sm'>\n                            <span className='font-medium'>{line.label}</span>\n                            {pace.status !== 'on-track' && pace.status !== 'under' && (\n                              <BudgetStatusLabel\n                                className='text-xs'\n                                status={pace.status}\n                              />\n                            )}\n                          </span>\n                          <span className='text-muted-foreground text-sm tabular-nums'>\n                            <span className='text-foreground font-medium'>\n                              {format(line.spent)}\n                            </span>{' '}\n                            of {format(line.budget)}\n                          </span>\n                        </div>\n                        <BudgetBar\n                          budget={line.budget}\n                          projected={pace.projected}\n                          size='sm'\n                          spent={line.spent}\n                        />\n                      </li>\n                    )\n                  })}\n                </ul>\n              </>\n            )}\n            {state !== 'empty' && state !== 'error' && (\n              <ul className='text-muted-foreground flex flex-wrap items-center gap-x-4 gap-y-1 text-xs'>\n                <li className='flex items-center gap-1.5'>\n                  <SpendKey shape='spent' />\n                  Spent\n                </li>\n                <li className='flex items-center gap-1.5'>\n                  <SpendKey shape='over' />\n                  Over budget\n                </li>\n                <li className='flex items-center gap-1.5'>\n                  <SpendKey shape='projected' />\n                  Projected\n                </li>\n                <li className='flex items-center gap-1.5'>\n                  <SpendKey shape='budget' />\n                  Budget\n                </li>\n              </ul>\n            )}\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport { Spend2, exampleProps as spend2ExampleProps, type Spend2Props, type SpendLine }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/spend/spend-02.tsx"
    }
  ],
  "meta": {
    "shows": "The total spent and where it is heading, then one budget bar per service, naming the ones over budget.",
    "size": "half",
    "usedWith": [
      "spend-01",
      "data-table-04"
    ],
    "component": "Spend2",
    "props": "{ currency?: string; description?: string; elapsed: number; lines?: { budget: number; label: string; spent: number }[]; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
