{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "usage-meter-03",
  "description": "Usage against what the plan includes, with the overage past the limit and what it will cost.",
  "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/usage-meter.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/usage-meter/usage-meter-03.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 {\n  UsageBar,\n  UsageKey,\n  UsageMeterValue,\n  formatUsage,\n} from '@/registry/components/dashboardblocks/usage-meter'\nimport { formatCurrency } from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface MeteredItem {\n  included: number\n  name: string\n  /** Price of each block of `per` units past what's included. */\n  price: number\n  /** @default 1 */\n  per?: number\n  unit: string\n  used: number\n}\n\ninterface UsageMeter3Props extends BlockStateProps {\n  /** When the overage is billed, such as `Oct 1`. */\n  billedOn: string\n  /** Defaults to a sentence naming the meters past what the plan includes. */\n  description?: string\n  /** Leave out, or pass none, to show the empty state. */\n  items?: MeteredItem[]\n  title: string\n}\n\nconst exampleProps: UsageMeter3Props = {\n  billedOn: 'Oct 1',\n  items: [\n    {\n      included: 1_000_000,\n      name: 'Events',\n      per: 1_000,\n      price: 0.5,\n      unit: 'events',\n      used: 1_240_000,\n    },\n    { included: 10, name: 'Seats', price: 15, unit: 'seats', used: 12 },\n    { included: 500, name: 'Bandwidth', price: 0.08, unit: 'GB', used: 312 },\n  ],\n  title: 'Metered usage',\n}\n\nconst OVERAGE_COLOR = 'var(--color-orange-500)'\n\nconst formatMoney = (value: number) => formatCurrency(value, { precision: 'exact' })\n\nfunction formatPrice(item: MeteredItem) {\n  const per = item.per ?? 1\n  const unit = per === 1 ? item.unit.replace(/s$/, '') : item.unit\n  return `${formatMoney(item.price)} per ${per === 1 ? '' : `${formatUsage(per, undefined, { compact: true })} `}${unit}`\n}\n\nfunction getOverageCost(item: MeteredItem) {\n  const over = Math.max(0, item.used - item.included)\n  return Math.ceil(over / (item.per ?? 1)) * item.price\n}\n\nfunction joinNames(names: string[]) {\n  if (names.length < 2) return names.join('')\n  return `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`\n}\n\nfunction describe(items: MeteredItem[]) {\n  const over = items.filter((item) => item.used > item.included).map((item) => item.name)\n  if (over.length === 0) return 'Every meter is within what your plan includes.'\n  return `${joinNames(over)} ${over.length === 1 ? 'is' : 'are'} past what your plan includes.`\n}\n\nconst UsageMeter3 = (props: UsageMeter3Props) => {\n  const { billedOn, items = [], errorMessage, status, title } = props\n  const { description = describe(items) } = props\n  const state = getBlockState(status, items)\n  const total = items.reduce((sum, item) => sum + getOverageCost(item), 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              <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              <UsageMeterValue>{formatMoney(total)}</UsageMeterValue>\n              {state === 'loading' ? (\n                <BlockStateText className='text-sm' />\n              ) : state ? null : (\n                <p className='text-muted-foreground text-sm'>\n                  Estimated overage, billed {billedOn}\n                </p>\n              )}\n            </div>\n            {state === 'loading' && <BlockStateRows className='border-t' rows={3} />}\n            {!state && (\n              <>\n                <ul className='flex flex-col border-t'>\n                  {items.map((item) => {\n                    const over = Math.max(0, item.used - item.included)\n                    const cost = getOverageCost(item)\n                    return (\n                      <li key={item.name} className='flex flex-col gap-2 border-b py-3'>\n                        <div className='flex items-baseline justify-between gap-3 text-sm'>\n                          <span className='font-medium'>{item.name}</span>\n                          <span className='text-muted-foreground tabular-nums'>\n                            <span className='text-foreground font-medium'>\n                              {formatUsage(item.used, undefined, { compact: true })}\n                            </span>{' '}\n                            of {formatUsage(item.included, item.unit, { compact: true })}\n                          </span>\n                        </div>\n                        <UsageBar\n                          color='var(--color-chart-2)'\n                          limit={item.included}\n                          overColor={OVERAGE_COLOR}\n                          size='sm'\n                          used={item.used}\n                        />\n                        <div className='text-muted-foreground flex items-baseline justify-between gap-3 text-xs'>\n                          <span>{formatPrice(item)}</span>\n                          {over > 0 ? (\n                            <span className='flex items-baseline gap-3 tabular-nums'>\n                              <span className='font-medium text-orange-700 dark:text-orange-400'>\n                                {formatUsage(over, item.unit, { compact: true })} over\n                              </span>\n                              <span className='text-foreground min-w-16 text-right font-medium'>\n                                {formatMoney(cost)}\n                              </span>\n                            </span>\n                          ) : (\n                            <span className='tabular-nums'>\n                              {formatUsage(item.included - item.used, item.unit, {\n                                compact: true,\n                              })}{' '}\n                              left\n                            </span>\n                          )}\n                        </div>\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='inline-flex items-center gap-1.5'>\n                  <UsageKey shape='used' />\n                  Included\n                </li>\n                <li className='inline-flex items-center gap-1.5'>\n                  <UsageKey color={OVERAGE_COLOR} shape='over' />\n                  Overage\n                </li>\n                <li className='inline-flex items-center gap-1.5'>\n                  <UsageKey shape='limit' />\n                  Plan limit\n                </li>\n              </ul>\n            )}\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport {\n  UsageMeter3,\n  exampleProps as usageMeter3ExampleProps,\n  type MeteredItem,\n  type UsageMeter3Props,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/usage-meter/usage-meter-03.tsx"
    }
  ],
  "meta": {
    "shows": "Usage against what the plan includes, with the overage past the limit and what it will cost.",
    "size": "half",
    "usedWith": [
      "billing-01",
      "usage-meter-06",
      "spend-01"
    ],
    "component": "UsageMeter3",
    "props": "{ billedOn: string; description?: string; items?: { included: number; name: string; price: number; per?: number; unit: string; used: number }[]; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
