{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "spend-04",
  "description": "The cash balance, the average net burn and the months of runway left, with the balance projected to the month it runs out.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/chart.json",
    "https://www.dashboardblocks.com/r/radix/chart-panel.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json",
    "https://www.dashboardblocks.com/r/radix/spend.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/spend/spend-04.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  ChartPlaceholder,\n  chartActiveDot,\n  chartAxisProps,\n  chartGridProps,\n  chartLastDot,\n  chartLineCursor,\n  chartReferenceLabel,\n  chartReferenceProps,\n  ChartTooltipContent,\n  chartValueAxisWidth,\n  useChartId,\n} from '@/registry/components/dashboardblocks/chart'\nimport {\n  ChartPanelFigure,\n  ChartPanelLegend,\n  ChartPanelTable,\n} from '@/registry/components/dashboardblocks/chart-panel'\nimport { KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport { getRunway } from '@/registry/components/dashboardblocks/spend'\nimport {\n  formatCurrency,\n  formatDate,\n  formatNumber,\n} from '@/registry/lib/dashboardblocks/format'\nimport {\n  Area,\n  CartesianGrid,\n  ComposedChart,\n  Line,\n  ReferenceLine,\n  ResponsiveContainer,\n  Tooltip,\n  XAxis,\n  YAxis,\n} from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface Spend4Props extends BlockStateProps {\n  /** Month-end cash balances, oldest first, one month apart. Leave out, or pass none, to show the empty state. */\n  balances?: number[]\n  /** @default 'USD' */\n  currency?: string\n  description: string\n  lastMonth: Date\n  /** Months of change to average for the burn. */\n  trailing?: number\n  title: string\n}\n\nconst exampleProps: Spend4Props = {\n  balances: [4_820_000, 4_610_000, 4_430_000, 4_190_000, 3_980_000, 3_740_000],\n  description: 'Cash balance, with the runway at the average burn of the last 3 months.',\n  lastMonth: new Date(Date.UTC(2026, 7, 1)),\n  title: 'Burn and runway',\n}\n\nconst formatMonth = (date: Date) =>\n  `${formatDate(date, 'month')} '${String(date.getUTCFullYear()).slice(2)}`\n\nconst Spend4 = (props: Spend4Props) => {\n  const {\n    balances = [],\n    currency = 'USD',\n    description,\n    lastMonth,\n    errorMessage,\n    status,\n    title,\n    trailing = 3,\n  } = props\n  const state = getBlockState(status, balances)\n  const gradient = useChartId()\n  const format = (value: number) => formatCurrency(value, { currency })\n  const compact = (value: number) => formatCurrency(value, { compact: true, currency })\n\n  const cash = balances[balances.length - 1] ?? 0\n  const span = Math.min(trailing, balances.length - 1)\n  const monthlyNet = span > 0 ? (cash - balances[balances.length - 1 - span]) / span : 0\n  const runway = getRunway({ cash, monthlyNet, now: lastMonth })\n\n  const monthAt = (offset: number) => {\n    const date = new Date(lastMonth.getTime())\n    date.setUTCMonth(date.getUTCMonth() + offset)\n    return date\n  }\n  const ahead = Number.isFinite(runway.months)\n    ? Math.min(24, Math.ceil(runway.months))\n    : 6\n  const rows = [\n    ...balances.map((balance, index) => ({\n      actual: balance,\n      label: formatMonth(monthAt(index - balances.length + 1)),\n      projected: index === balances.length - 1 ? balance : null,\n    })),\n    ...Array.from({ length: ahead }, (_, index) => ({\n      actual: null,\n      label: formatMonth(monthAt(index + 1)),\n      projected: Math.max(0, cash + monthlyNet * (index + 1)),\n    })),\n  ]\n\n  const today = rows[balances.length - 1]\n  const empty = rows.find((row) => row.projected === 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 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            <dl className='grid grid-cols-2 gap-x-6 gap-y-4 border-t pt-4 @lg:grid-cols-3'>\n              <div className='col-span-2 flex flex-col gap-1 @lg:col-span-1'>\n                <dt className='text-muted-foreground text-sm'>Cash</dt>\n                <dd>\n                  <KPIValue className='whitespace-nowrap' value={cash} format={compact} />\n                </dd>\n              </div>\n              {state !== 'empty' && state !== 'error' && (\n                <>\n                  <div className='flex flex-col gap-1'>\n                    <dt className='text-muted-foreground text-sm'>\n                      {monthlyNet < 0 ? 'Net burn' : 'Net gain'} a month\n                    </dt>\n                    <dd>\n                      <KPIValue\n                        className='text-2xl'\n                        value={Math.abs(monthlyNet)}\n                        format={compact}\n                      />\n                    </dd>\n                  </div>\n                  <div className='flex flex-col gap-1'>\n                    <dt className='text-muted-foreground text-sm'>Runway</dt>\n                    <dd>\n                      <KPIValue\n                        className='text-2xl'\n                        value={runway.months}\n                        format={(months) =>\n                          Number.isFinite(months)\n                            ? `${formatNumber(months, { maximumFractionDigits: 1, minimumFractionDigits: 1 })} months`\n                            : 'Not burning'\n                        }\n                      />\n                    </dd>\n                    {runway.date && (\n                      <dd className='text-muted-foreground text-xs'>\n                        <BlockReady className='text-xs'>\n                          Cash runs out around{' '}\n                          {formatDate(runway.date, 'month-long-year')}\n                        </BlockReady>\n                      </dd>\n                    )}\n                  </div>\n                </>\n              )}\n            </dl>\n            {state !== 'empty' && state !== 'error' && (\n              <ChartPanelLegend\n                items={[\n                  { color: 'var(--color-chart-2)', label: 'Cash balance', shape: 'line' },\n                  {\n                    color: 'var(--color-muted-foreground)',\n                    label: 'At the current burn',\n                    shape: 'line',\n                  },\n                ]}\n              />\n            )}\n            {state ? (\n              <ChartPlaceholder height={240} />\n            ) : (\n              <ChartPanelFigure className='h-60'>\n                <ResponsiveContainer width='100%' height='100%'>\n                  <ComposedChart\n                    aria-label={`${title} chart`}\n                    data={rows}\n                    margin={{ top: 20, right: 16, bottom: 0, left: 0 }}\n                  >\n                    <defs>\n                      <linearGradient id={gradient} x1='0' x2='0' y1='0' y2='1'>\n                        <stop\n                          offset='0%'\n                          stopColor='var(--color-chart-2)'\n                          stopOpacity={0.3}\n                        />\n                        <stop\n                          offset='100%'\n                          stopColor='var(--color-chart-2)'\n                          stopOpacity={0}\n                        />\n                      </linearGradient>\n                    </defs>\n                    <CartesianGrid {...chartGridProps} />\n                    <XAxis\n                      {...chartAxisProps}\n                      dataKey='label'\n                      interval='preserveStartEnd'\n                      minTickGap={20}\n                    />\n                    <YAxis\n                      {...chartAxisProps}\n                      domain={[0, 'auto']}\n                      tickFormatter={compact}\n                      width={chartValueAxisWidth}\n                    />\n                    <Tooltip\n                      content={(tooltipProps) => (\n                        <ChartTooltipContent\n                          {...tooltipProps}\n                          payload={tooltipProps.payload\n                            ?.filter(\n                              (item) => item.value !== null && item.value !== undefined,\n                            )\n                            .slice(0, 1)}\n                          valueFormatter={format}\n                        />\n                      )}\n                      cursor={chartLineCursor}\n                    />\n                    {today && (\n                      <ReferenceLine\n                        x={today.label}\n                        stroke='var(--color-border)'\n                        label={{ ...chartReferenceLabel, position: 'top', value: 'Now' }}\n                      />\n                    )}\n                    <Area\n                      activeDot={chartActiveDot}\n                      dataKey='actual'\n                      dot={chartLastDot(balances.length, 'var(--color-chart-2)')}\n                      fill={`url(#${gradient})`}\n                      name='Cash balance'\n                      stroke='var(--color-chart-2)'\n                      strokeWidth={2}\n                      type='monotone'\n                    />\n                    <Line\n                      activeDot={chartActiveDot}\n                      dataKey='projected'\n                      dot={false}\n                      name='At the current burn'\n                      stroke='var(--color-muted-foreground)'\n                      strokeDasharray='5 4'\n                      strokeWidth={2}\n                      type='linear'\n                    />\n                    {empty && (\n                      <ReferenceLine\n                        {...chartReferenceProps}\n                        x={empty.label}\n                        stroke='var(--color-red-500)'\n                        strokeOpacity={1}\n                        label={{\n                          ...chartReferenceLabel,\n                          fill: 'var(--color-foreground)',\n                          fontWeight: 500,\n                          position: 'insideTopRight',\n                          value: 'Out of cash',\n                        }}\n                      />\n                    )}\n                  </ComposedChart>\n                </ResponsiveContainer>\n              </ChartPanelFigure>\n            )}\n            {!state && (\n              <ChartPanelTable\n                caption={`${title}: ${description}`}\n                columns={[\n                  { key: 'label', label: 'Month' },\n                  { key: 'actual', label: 'Cash balance' },\n                  { key: 'projected', label: 'Projected' },\n                ]}\n                rows={rows.map((row, index) => ({\n                  actual: row.actual === null ? '—' : format(row.actual),\n                  label: row.label,\n                  projected:\n                    row.projected === null || index < balances.length\n                      ? '—'\n                      : format(row.projected),\n                }))}\n              />\n            )}\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport { Spend4, exampleProps as spend4ExampleProps, type Spend4Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/spend/spend-04.tsx"
    }
  ],
  "meta": {
    "shows": "The cash balance, the average net burn and the months of runway left, with the balance projected to the month it runs out.",
    "size": "half",
    "usedWith": [
      "forecast-02",
      "billing-04"
    ],
    "component": "Spend4",
    "props": "{ balances?: number[]; currency?: string; description: string; lastMonth: Date; trailing?: number; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
