{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "usage-meter-04",
  "description": "A credit balance with the days left, the date it runs out and recent daily use against the average.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "card",
    "button",
    "https://www.dashboardblocks.com/r/radix/animated-number.json",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/chart.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json",
    "https://www.dashboardblocks.com/r/radix/usage-meter.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/usage-meter/usage-meter-04.tsx",
      "content": "'use client'\n\nimport { AnimatedNumber } from '@/registry/components/dashboardblocks/animated-number'\nimport {\n  BlockReady,\n  BlockStateProvider,\n  BlockStateText,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { TinyBarChart } from '@/registry/components/dashboardblocks/chart'\nimport { KPIChart } from '@/registry/components/dashboardblocks/kpi'\nimport {\n  UsageBar,\n  UsageMeterLimit,\n  UsageMeterValue,\n  UsageStatusLabel,\n  type UsageStatus,\n  formatUsage,\n  getDaysLeft,\n} from '@/registry/components/dashboardblocks/usage-meter'\nimport { formatDate } from '@/registry/lib/dashboardblocks/format'\n\nimport { Button } from '@/components/ui/button'\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface UsageMeter4Props extends BlockStateProps {\n  /** Leave out to show the empty state. */\n  balance?: number\n  /** Credits used each day, oldest first; the last is today. */\n  dailyUsage?: number[]\n  /** Pass a fixed date, so the block renders the same on the server and in the browser. */\n  now: Date\n  onTopUp?: () => void\n  purchased: number\n  timeZone?: string\n  title: string\n}\n\nconst DAY = 86_400_000\n\nconst exampleProps: UsageMeter4Props = {\n  balance: 45_750,\n  dailyUsage: [\n    1_080, 1_210, 990, 1_340, 1_420, 1_150, 870, 1_260, 1_390, 1_310, 1_480, 1_220, 1_160,\n    1_430,\n  ],\n  now: new Date(Date.UTC(2026, 8, 26)),\n  onTopUp: () => {},\n  purchased: 100_000,\n  title: 'Credits',\n}\n\nconst CRITICAL_RUNWAY_DAYS = 7\nconst WARNING_RUNWAY_DAYS = 14\n\nfunction getRunwayStatus(days: number): UsageStatus {\n  if (days < CRITICAL_RUNWAY_DAYS) return 'critical'\n  if (days < WARNING_RUNWAY_DAYS) return 'warning'\n  return 'ok'\n}\n\nconst UsageMeter4 = (props: UsageMeter4Props) => {\n  const {\n    balance = 0,\n    dailyUsage = [],\n    now,\n    onTopUp,\n    purchased,\n    errorMessage,\n    status: blockStatus,\n    timeZone = 'UTC',\n    title,\n  } = props\n  const state = getBlockState(blockStatus, props.balance)\n  const burn =\n    dailyUsage.length > 0\n      ? dailyUsage.reduce((sum, value) => sum + value, 0) / dailyUsage.length\n      : 0\n  const daysLeft = getDaysLeft(balance, burn)\n  const status = getRunwayStatus(daysLeft)\n  const runsOut = Number.isFinite(daysLeft)\n    ? new Date(now.getTime() + daysLeft * DAY)\n    : null\n  const days = dailyUsage.map((value, index) => ({\n    label: formatDate(\n      new Date(now.getTime() - (dailyUsage.length - 1 - index) * DAY),\n      'day',\n      timeZone,\n    ),\n    value,\n  }))\n  const latest = days[days.length - 1]\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={state}>\n      <Card>\n        <CardContent>\n          <section className='@container flex flex-col gap-3 border-t pt-3 in-data-[slot=card]:first:border-t-0 in-data-[slot=card]:first:pt-0'>\n            <header className='flex min-h-8 items-center justify-between gap-3'>\n              <p className='text-muted-foreground text-sm'>{title}</p>\n              {onTopUp && (\n                <Button variant='outline' size='sm' onClick={onTopUp}>\n                  Buy credits\n                </Button>\n              )}\n            </header>\n            <div className='flex flex-col gap-1'>\n              <div className='flex flex-wrap items-baseline gap-x-2'>\n                <UsageMeterValue>\n                  <span aria-hidden>\n                    <AnimatedNumber\n                      value={balance}\n                      formatter={(value) => formatUsage(Math.round(value))}\n                    />\n                  </span>\n                  <span className='sr-only'>{formatUsage(balance)}</span>\n                </UsageMeterValue>\n                <UsageMeterLimit>of {formatUsage(purchased)} left</UsageMeterLimit>\n              </div>\n              {state === 'loading' ? (\n                <BlockStateText className='text-sm' />\n              ) : state ? null : (\n                <p className='text-muted-foreground text-sm'>\n                  {runsOut ? (\n                    <>\n                      <UsageStatusLabel\n                        variant='text'\n                        label={`${Math.floor(daysLeft)} ${Math.floor(daysLeft) === 1 ? 'day' : 'days'} left`}\n                        status={status}\n                      />\n                      , running out around{' '}\n                      <time dateTime={runsOut.toISOString()}>\n                        {formatDate(runsOut, 'day', timeZone)}\n                      </time>\n                    </>\n                  ) : (\n                    'No recent use, so they won’t run out at this rate'\n                  )}\n                </p>\n              )}\n            </div>\n            <UsageBar\n              color='var(--color-chart-2)'\n              limit={purchased}\n              size='sm'\n              used={balance}\n            />\n            {(state || latest) && (\n              <KPIChart\n                className='mt-2'\n                label={\n                  latest\n                    ? `Credits used per day, last ${days.length} days: from ${formatUsage(days[0].value)} on ${days[0].label} to ${formatUsage(latest.value)} on ${latest.label}, ${formatUsage(Math.round(burn))} a day on average`\n                    : 'Credits used per day'\n                }\n              >\n                <TinyBarChart\n                  accessibilityLayer={false}\n                  bars={[{ dataKey: 'value', fill: 'var(--color-chart-2)' }]}\n                  data={days}\n                  formatter={(value) => formatUsage(value, undefined, { compact: true })}\n                  height={88}\n                  highlightIndex={days.length - 1}\n                  reference={{\n                    label: `avg ${formatUsage(Math.round(burn))}`,\n                    value: burn,\n                  }}\n                />\n              </KPIChart>\n            )}\n            {latest && (\n              <BlockReady className='text-xs'>\n                <p className='text-muted-foreground text-xs'>\n                  Today used {formatUsage(latest.value)},{' '}\n                  {latest.value > burn ? 'above' : 'below'} the {days.length}-day average\n                  of {formatUsage(Math.round(burn))}.\n                </p>\n              </BlockReady>\n            )}\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport { UsageMeter4, exampleProps as usageMeter4ExampleProps, type UsageMeter4Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/usage-meter/usage-meter-04.tsx"
    }
  ],
  "meta": {
    "shows": "A credit balance with the days left, the date it runs out and recent daily use against the average.",
    "size": "compact",
    "usedWith": [
      "billing-02",
      "usage-meter-01",
      "ai-usage-02"
    ],
    "component": "UsageMeter4",
    "props": "{ balance?: number; dailyUsage?: number[]; now: Date; onTopUp?: () => void; purchased: number; timeZone?: string; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
