{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "usage-meter",
  "title": "Usage Meter",
  "description": "Primitives for usage against a limit: within limit, nearing it, critical and over limit, each with a label (a coloured word, or a badge with an icon), the share used, usage projected to the end of the period, days left at a daily rate, en-US number formatting, and a usage bar with overage, a striped projection and a limit tick.",
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/format.json",
    "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/usage-meter.tsx",
      "content": "'use client'\n\nimport {\n  BlockStateText,\n  SkeletonText,\n  useBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  StatusLabel,\n  type StatusLabelProps,\n  type StatusTone,\n} from '@/registry/components/dashboardblocks/status-label'\nimport { useReveal } from '@/registry/hooks/use-reveal'\nimport {\n  formatCompact,\n  formatNumber,\n  formatPercent,\n} from '@/registry/lib/dashboardblocks/format'\nimport type { ReactNode } from 'react'\n\nimport { cn } from '@/lib/utils'\n\ntype UsageStatus = 'ok' | 'warning' | 'critical' | 'over'\n\ninterface UsageThresholds {\n  /** Share of the limit, in percent, from which usage is critical. */\n  critical?: number\n  /** Share of the limit, in percent, from which usage is nearing it. */\n  warning?: number\n}\n\nfunction getUsageShare(used: number, limit: number) {\n  if (!Number.isFinite(used) || !Number.isFinite(limit) || limit <= 0) return 0\n  return (Math.max(0, used) / limit) * 100\n}\n\nfunction getUsageStatus(\n  used: number,\n  limit: number,\n  { critical = 95, warning = 80 }: UsageThresholds = {},\n): UsageStatus {\n  const share = getUsageShare(used, limit)\n  if (share > 100) return 'over'\n  if (share >= critical) return 'critical'\n  if (share >= warning) return 'warning'\n  return 'ok'\n}\n\n/** Where a plan's allowance turns orange and red: a sidebar's meter and a usage page's windows. */\nconst planUsageThresholds = { critical: 90, warning: 75 } satisfies UsageThresholds\n\n/** The share of a plan's allowance used, as `42% used`, orange from 75% and red from 90%. */\nfunction PlanUsageShare({\n  className,\n  limit,\n  used,\n}: {\n  className?: string\n  limit: number\n  used: number\n}) {\n  const status = getUsageStatus(Math.min(used, limit), limit, planUsageThresholds)\n  const share = Math.min(100, getUsageShare(used, limit))\n  return (\n    <StatusLabel\n      variant='text'\n      tone={status === 'ok' ? 'neutral' : status === 'warning' ? 'warning' : 'bad'}\n      className={cn('tabular-nums', className)}\n    >\n      {`${formatPercent(share / 100, { maximumFractionDigits: 0 })} used`}\n    </StatusLabel>\n  )\n}\n\ninterface UsageProjectionInput {\n  daysElapsed: number\n  daysInPeriod: number\n  used: number\n}\n\nfunction projectUsage({ daysElapsed, daysInPeriod, used }: UsageProjectionInput) {\n  if (daysElapsed <= 0) return used\n  return (used / daysElapsed) * daysInPeriod\n}\n\n/** Days until a balance runs out at a daily burn; `Infinity` with no burn. */\nfunction getDaysLeft(remaining: number, dailyBurn: number) {\n  return dailyBurn > 0 ? Math.max(0, remaining) / dailyBurn : Infinity\n}\n\nfunction formatUsage(value: number, unit?: string, { compact = false } = {}) {\n  const number = compact ? formatCompact(value) : formatNumber(value)\n  return unit ? `${number} ${unit}` : number\n}\n\ninterface UsageStatusConfig {\n  color: string\n  label: string\n  tone: StatusTone\n}\n\nconst usageStatusConfig: Record<UsageStatus, UsageStatusConfig> = {\n  critical: {\n    color: 'var(--color-destructive)',\n    label: 'Critical',\n    tone: 'bad',\n  },\n  ok: {\n    color: 'var(--color-chart-2)',\n    label: 'Within limit',\n    tone: 'good',\n  },\n  over: {\n    color: 'var(--color-destructive)',\n    label: 'Over limit',\n    tone: 'bad',\n  },\n  warning: {\n    color: 'var(--color-orange-500)',\n    label: 'Nearing limit',\n    tone: 'warning',\n  },\n}\n\nfunction UsageStatusLabel({\n  className,\n  label,\n  status,\n  variant,\n}: {\n  className?: string\n  label?: string\n  status: UsageStatus\n  variant?: StatusLabelProps['variant']\n}) {\n  const state = useBlockState()\n  if (state) return null\n  const config = usageStatusConfig[status]\n  return (\n    <StatusLabel className={className} tone={config.tone} variant={variant}>\n      {label ?? config.label}\n    </StatusLabel>\n  )\n}\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 UsageBarProps extends UsageThresholds {\n  animated?: boolean\n  className?: string\n  color?: string\n  limit: number\n  /**\n   * A value on the same scale drawn as a tick across the bar, such as where spending would be if it\n   * kept pace with time: the time gone times the limit. Say what it means in text beside the bar.\n   */\n  mark?: number\n  overColor?: string\n  projected?: number\n  /** @default 'md' */\n  size?: 'sm' | 'md'\n  used: number\n}\n\nfunction UsageBar({\n  animated = true,\n  className,\n  color,\n  critical,\n  limit,\n  mark,\n  overColor = 'var(--color-destructive)',\n  projected,\n  size = 'md',\n  used,\n  warning,\n}: UsageBarProps) {\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-2.5' : 'h-1.5',\n          state === 'loading' && 'animate-pulse motion-reduce:animate-none',\n          className,\n        )}\n      />\n    )\n  }\n\n  const status = getUsageStatus(Math.min(used, limit), limit, { critical, warning })\n  const fill = color ?? usageStatusConfig[status].color\n  const top = Math.max(limit, used, projected ?? 0)\n  const pastLimit = top > limit\n  const scale = (pastLimit ? top * 1.04 : top) || 1\n  const at = (value: number) => `${(Math.max(0, value) / scale) * 100}%`\n  const withinLimit = Math.min(used, limit)\n  const overage = Math.max(0, used - limit)\n  const projectedExtra = projected !== undefined ? Math.max(0, projected - used) : 0\n\n  return (\n    <div\n      ref={ref}\n      aria-hidden\n      className={cn('relative w-full', size === 'md' ? 'h-2.5' : 'h-1.5', className)}\n    >\n      <div\n        className='bg-muted flex h-full w-full 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 transition-[width] duration-500 ease-out motion-reduce:transition-none'\n          style={{ backgroundColor: fill, width: at(withinLimit) }}\n        />\n        {overage > 0 && (\n          <span\n            className='h-full'\n            style={{ backgroundColor: overColor, width: at(overage) }}\n          />\n        )}\n        {projectedExtra > 0 && (\n          <span\n            className='h-full'\n            style={{\n              background: projectedFill(used + projectedExtra > limit ? overColor : fill),\n              width: at(projectedExtra),\n            }}\n          />\n        )}\n      </div>\n      {pastLimit && <UsageTick at={at(limit)} />}\n      {mark !== undefined && <UsageTick at={at(Math.min(mark, scale))} />}\n    </div>\n  )\n}\n\nfunction UsageTick({ at }: { at: string }) {\n  return (\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 }}\n    />\n  )\n}\n\ntype UsageKeyShape = 'used' | 'projected' | 'over' | 'limit'\n\nfunction UsageKey({\n  className,\n  color = 'var(--color-chart-2)',\n  shape,\n}: {\n  className?: string\n  color?: string\n  shape: UsageKeyShape\n}) {\n  if (shape === 'limit') {\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={{ background: shape === 'projected' ? projectedFill(color) : color }}\n    />\n  )\n}\n\nfunction UsageMeterValue({\n  children,\n  className,\n}: {\n  children?: ReactNode\n  className?: string\n}) {\n  const state = useBlockState()\n\n  return (\n    <span className={cn('text-4xl font-light tracking-tight tabular-nums', className)}>\n      {state === 'loading' ? (\n        <SkeletonText className='w-[4.5em]' />\n      ) : state ? (\n        <BlockStateText className='inline' variant='short' />\n      ) : (\n        children\n      )}\n    </span>\n  )\n}\n\nfunction UsageMeterLimit({\n  children,\n  className,\n}: {\n  children: ReactNode\n  className?: string\n}) {\n  const state = useBlockState()\n  if (state) return null\n  return (\n    <span className={cn('text-muted-foreground text-sm', className)}>{children}</span>\n  )\n}\n\nexport {\n  PlanUsageShare,\n  UsageBar,\n  UsageKey,\n  UsageMeterLimit,\n  UsageMeterValue,\n  UsageStatusLabel,\n  formatUsage,\n  getDaysLeft,\n  getUsageShare,\n  getUsageStatus,\n  planUsageThresholds,\n  projectUsage,\n  projectedFill,\n  usageStatusConfig,\n}\n\nexport type {\n  UsageBarProps,\n  UsageKeyShape,\n  UsageProjectionInput,\n  UsageStatus,\n  UsageStatusConfig,\n  UsageThresholds,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/usage-meter.tsx"
    }
  ],
  "type": "registry:component"
}
