{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "spend-01",
  "description": "Spend so far against the month's budget, with the projected month end and what is left to spend per day.",
  "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-01.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\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  getElapsed,\n} from '@/registry/components/dashboardblocks/spend'\nimport { formatCurrency, formatPercent } from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\nimport { cn } from '@/lib/utils'\n\ninterface Spend1Props extends BlockStateProps {\n  budget: number\n  /** @default 'USD' */\n  currency?: string\n  description: string\n  /** Pass a fixed date, so the block renders the same on the server and in the browser. */\n  now: Date\n  periodEnd: Date\n  periodStart: Date\n  /** Leave out to show the empty state. */\n  spent?: number\n  title: string\n}\n\nconst exampleProps: Spend1Props = {\n  budget: 48_000,\n  description: 'Cloud infrastructure for September 2026.',\n  now: new Date(Date.UTC(2026, 8, 26)),\n  periodEnd: new Date(Date.UTC(2026, 9, 1)),\n  periodStart: new Date(Date.UTC(2026, 8, 1)),\n  spent: 42_860,\n  title: 'Monthly budget',\n}\n\nconst DAY = 86_400_000\n\nconst Spend1 = (props: Spend1Props) => {\n  const {\n    budget,\n    currency = 'USD',\n    description,\n    now,\n    periodEnd,\n    periodStart,\n    spent = 0,\n    errorMessage,\n    status,\n    title,\n  } = props\n  const state = getBlockState(status, props.spent)\n  const pace = getBudgetPace({\n    budget,\n    elapsed: getElapsed(periodStart, periodEnd, now),\n    spent,\n  })\n  const daysLeft = Math.max(0, Math.ceil((periodEnd.getTime() - now.getTime()) / DAY))\n  const format = (value: number) => formatCurrency(value, { currency })\n  const perDay = daysLeft > 0 ? Math.max(0, pace.remaining) / daysLeft : 0\n  const share = budget > 0 ? spent / budget : 0\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 items-start justify-between gap-3'>\n              <div className='flex min-w-0 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              </div>\n              <BudgetStatusLabel className='mt-0.5' status={pace.status} />\n            </header>\n            <div className='flex flex-col gap-1 border-t pt-4'>\n              <KPIValue value={props.spent} format={format} />\n              {state === 'loading' ? (\n                <BlockStateText className='text-sm' />\n              ) : state ? null : (\n                <p className='text-muted-foreground text-sm'>\n                  Spent so far,{' '}\n                  <span className='text-foreground font-medium tabular-nums'>\n                    {formatPercent(share, { maximumFractionDigits: 0 })}\n                  </span>{' '}\n                  of the <span className='tabular-nums'>{format(budget)}</span> budget\n                  with {daysLeft} {daysLeft === 1 ? 'day' : 'days'} to go.\n                </p>\n              )}\n            </div>\n            {state !== 'empty' && state !== 'error' && (\n              <>\n                <div className='flex flex-col gap-2.5'>\n                  <BudgetBar budget={budget} projected={pace.projected} spent={spent} />\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='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                </div>\n                <dl className='grid grid-cols-2 gap-4 border-t pt-4'>\n                  <div className='flex flex-col gap-0.5'>\n                    <dt className='text-muted-foreground text-sm'>Projected month end</dt>\n                    <dd>\n                      <KPIValue\n                        className='text-2xl'\n                        value={pace.projected}\n                        format={format}\n                      />\n                    </dd>\n                    <dd\n                      className={cn(\n                        'text-xs tabular-nums',\n                        pace.projected > budget\n                          ? 'font-medium text-red-700 dark:text-red-400'\n                          : 'text-muted-foreground',\n                      )}\n                    >\n                      <BlockReady className='text-xs'>\n                        {pace.projected > budget\n                          ? `${format(pace.projected - budget)} over budget`\n                          : `${format(budget - pace.projected)} under budget`}\n                      </BlockReady>\n                    </dd>\n                  </div>\n                  <div className='flex flex-col gap-0.5'>\n                    <dt className='text-muted-foreground text-sm'>\n                      {pace.remaining >= 0 ? 'Left to spend' : 'Over budget by'}\n                    </dt>\n                    <dd>\n                      <KPIValue\n                        className='text-2xl'\n                        value={Math.abs(pace.remaining)}\n                        format={format}\n                      />\n                    </dd>\n                    <dd className='text-muted-foreground text-xs tabular-nums'>\n                      <BlockReady className='text-xs'>\n                        {pace.remaining > 0 && daysLeft > 0\n                          ? `${format(perDay)} a day to stay in budget`\n                          : `${daysLeft} ${daysLeft === 1 ? 'day' : 'days'} left`}\n                      </BlockReady>\n                    </dd>\n                  </div>\n                </dl>\n              </>\n            )}\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport { Spend1, exampleProps as spend1ExampleProps, type Spend1Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/spend/spend-01.tsx"
    }
  ],
  "meta": {
    "shows": "Spend so far against the month's budget, with the projected month end and what is left to spend per day.",
    "size": "half",
    "usedWith": [
      "spend-03",
      "forecast-03"
    ],
    "component": "Spend1",
    "props": "{ budget: number; currency?: string; description: string; now: Date; periodEnd: Date; periodStart: Date; spent?: number; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
