{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "spend",
  "title": "Spend",
  "description": "Primitives for spend and budgets: pace against an even spend of the budget with a projected total and a status, runway at the current net burn, a budget bar with overspend and a striped projection, the status as a coloured word, and legend keys.",
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/status-label.json",
    "https://www.dashboardblocks.com/r/radix/use-reveal.json",
    "https://www.dashboardblocks.com/r/radix/block-state.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/spend.tsx",
      "content": "'use client'\n\nimport { useBlockState } from '@/registry/components/dashboardblocks/block-state'\nimport {\n  StatusLabel,\n  type StatusTone,\n} from '@/registry/components/dashboardblocks/status-label'\nimport { useReveal } from '@/registry/hooks/use-reveal'\n\nimport { cn } from '@/lib/utils'\n\ntype BudgetStatus = 'under' | 'on-track' | 'at-risk' | 'over'\n\ninterface BudgetPaceInput {\n  budget: number\n  /** Share of the period that has passed, 0–1. */\n  elapsed: number\n  /** Your own projection to the period's end. Leave out to project the pace so far. */\n  projected?: number\n  spent: number\n  /** How far over budget the projection can land and stay on track, and how far under counts as under budget, 0.05 by default. */\n  tolerance?: number\n}\n\ninterface BudgetPace {\n  budget: number\n  expected: number\n  projected: number\n  /** Budget left, negative once over. */\n  remaining: number\n  spent: number\n  status: BudgetStatus\n}\n\nfunction getBudgetPace({\n  budget,\n  elapsed,\n  projected: given,\n  spent,\n  tolerance = 0.05,\n}: BudgetPaceInput): BudgetPace {\n  const clamped = Math.min(1, Math.max(0, elapsed))\n  const projected = given ?? (clamped > 0 ? spent / clamped : spent)\n  let status: BudgetStatus = 'on-track'\n  if (spent > budget) status = 'over'\n  else if (projected > budget * (1 + tolerance)) status = 'at-risk'\n  else if (projected < budget * (1 - tolerance)) status = 'under'\n  return {\n    budget,\n    expected: budget * clamped,\n    projected,\n    remaining: budget - spent,\n    spent,\n    status,\n  }\n}\n\nfunction getElapsed(start: Date, end: Date, now: Date) {\n  const total = end.getTime() - start.getTime()\n  return total > 0 ? (now.getTime() - start.getTime()) / total : 1\n}\n\ninterface RunwayInput {\n  cash: number\n  /** Cash in minus cash out per month; negative while burning. */\n  monthlyNet: number\n  now: Date\n}\n\nfunction getRunway({ cash, monthlyNet, now }: RunwayInput) {\n  if (monthlyNet >= 0 || cash <= 0) {\n    return { date: null, months: cash <= 0 ? 0 : Infinity }\n  }\n  const months = cash / -monthlyNet\n  const date = new Date(now.getTime())\n  const whole = Math.floor(months)\n  date.setUTCMonth(date.getUTCMonth() + whole)\n  date.setUTCDate(date.getUTCDate() + Math.round((months - whole) * 30.4))\n  return { date, months }\n}\n\nconst budgetStatusConfig: Record<BudgetStatus, { label: string; tone: StatusTone }> = {\n  'at-risk': { label: 'At risk', tone: 'warning' },\n  'on-track': { label: 'On track', tone: 'good' },\n  over: { label: 'Over budget', tone: 'bad' },\n  under: { label: 'Under budget', tone: 'good' },\n}\n\nfunction BudgetStatusLabel({\n  className,\n  status,\n}: {\n  className?: string\n  status: BudgetStatus\n}) {\n  const state = useBlockState()\n  if (state) return null\n  const config = budgetStatusConfig[status]\n  return (\n    <StatusLabel className={className} tone={config.tone}>\n      {config.label}\n    </StatusLabel>\n  )\n}\n\nconst spendColors = {\n  over: 'var(--color-destructive)',\n  spent: 'var(--color-chart-2)',\n} as const\n\nfunction projectedFill(color: string) {\n  return `repeating-linear-gradient(-45deg, color-mix(in oklab, ${color} 55%, transparent) 0 2px, color-mix(in oklab, ${color} 18%, transparent) 2px 5px)`\n}\n\ninterface BudgetBarProps {\n  animated?: boolean\n  budget: number\n  className?: string\n  /** @default 'var(--color-chart-2)' */\n  color?: string\n  projected?: number\n  /** @default 'md' */\n  size?: 'sm' | 'md'\n  spent: number\n}\n\nconst SCALE_HEADROOM = 1.04\n\nfunction BudgetBar({\n  animated = true,\n  budget,\n  className,\n  color = spendColors.spent,\n  projected,\n  size = 'md',\n  spent,\n}: BudgetBarProps) {\n  const { ref, revealed } = useReveal(animated)\n  const state = useBlockState()\n\n  if (state) {\n    return (\n      <div\n        aria-hidden\n        className={cn(\n          'bg-muted w-full rounded-full',\n          size === 'md' ? 'h-3' : 'h-2',\n          state === 'loading' && 'animate-pulse motion-reduce:animate-none',\n          className,\n        )}\n      />\n    )\n  }\n\n  const scale = Math.max(budget, spent, projected ?? 0) * SCALE_HEADROOM || 1\n  const at = (value: number) => `${(Math.max(0, value) / scale) * 100}%`\n  const withinBudget = Math.min(spent, budget)\n  const overspend = Math.max(0, spent - budget)\n  const projectedExtra = projected !== undefined ? Math.max(0, projected - spent) : 0\n\n  return (\n    <div\n      ref={ref}\n      aria-hidden\n      className={cn('relative w-full', size === 'md' ? 'h-3' : 'h-2', className)}\n    >\n      <div\n        className='bg-muted flex h-full w-full origin-left overflow-hidden rounded-full transition-[clip-path] duration-1000 ease-[cubic-bezier(0.33,1,0.68,1)] motion-reduce:transition-none'\n        style={{ clipPath: `inset(0 ${revealed ? 0 : 100}% 0 0)` }}\n      >\n        <span\n          className='h-full'\n          style={{ backgroundColor: color, width: at(withinBudget) }}\n        />\n        {overspend > 0 && (\n          <span\n            className='h-full'\n            style={{ backgroundColor: spendColors.over, width: at(overspend) }}\n          />\n        )}\n        {projectedExtra > 0 && (\n          <span\n            className='h-full'\n            style={{\n              background: projectedFill(\n                spent + projectedExtra > budget ? spendColors.over : color,\n              ),\n              width: at(projectedExtra),\n            }}\n          />\n        )}\n      </div>\n      <span\n        className='bg-foreground ring-background absolute top-1/2 h-[calc(100%+8px)] w-0.5 -translate-x-1/2 -translate-y-1/2 rounded-full ring-2'\n        style={{ left: at(budget) }}\n      />\n    </div>\n  )\n}\n\ntype SpendKeyShape = 'spent' | 'projected' | 'over' | 'budget'\n\nfunction SpendKey({\n  className,\n  color = spendColors.spent,\n  shape,\n}: {\n  className?: string\n  color?: string\n  shape: SpendKeyShape\n}) {\n  if (shape === 'budget') {\n    return (\n      <span\n        aria-hidden\n        className={cn(\n          'bg-foreground inline-block h-3 w-0.5 shrink-0 rounded-full',\n          className,\n        )}\n      />\n    )\n  }\n  return (\n    <span\n      aria-hidden\n      className={cn('inline-block size-2.5 shrink-0 rounded-[3px]', className)}\n      style={{\n        background:\n          shape === 'projected'\n            ? projectedFill(color)\n            : shape === 'over'\n              ? spendColors.over\n              : color,\n      }}\n    />\n  )\n}\n\nexport {\n  BudgetBar,\n  BudgetStatusLabel,\n  SpendKey,\n  budgetStatusConfig,\n  getBudgetPace,\n  getElapsed,\n  getRunway,\n  projectedFill,\n  spendColors,\n}\n\nexport type {\n  BudgetBarProps,\n  BudgetPace,\n  BudgetPaceInput,\n  BudgetStatus,\n  RunwayInput,\n  SpendKeyShape,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/spend.tsx"
    }
  ],
  "type": "registry:component"
}
