{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "usage-meters",
  "title": "Usage meters",
  "description": "Gauges for bookings, SMS credits, seats and storage that warn near the limit, forecast usage and top up through a live-priced dialog.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "button",
    "dialog",
    "input",
    "label",
    "switch"
  ],
  "files": [
    {
      "path": "registry/blocks/billing/usage-meters.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { BellRing, CalendarCheck, Check, HardDrive, Loader2, MessageSquareText, Minus, Plus, RefreshCw, Users } from \"lucide-react\"\nimport { Button } from \"@/components/ui/button\"\nimport { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from \"@/components/ui/dialog\"\nimport { Input } from \"@/components/ui/input\"\nimport { Label } from \"@/components/ui/label\"\nimport { Switch } from \"@/components/ui/switch\"\nimport { cn } from \"@/lib/utils\"\n\ntype MeterId = \"bookings\" | \"sms\" | \"seats\" | \"storage\"\ntype Meter = {\n  id: MeterId\n  label: string\n  unit: string\n  used: number\n  limit: number\n  icon: React.ComponentType<{ className?: string }>\n  resets: boolean\n  daily: number[]\n  pack: { size: number; price: number; noun: string; recurring?: boolean }\n  cta: string\n}\n\nconst PERIOD = { start: \"Oct 18\", end: \"Nov 18\", days: 31, elapsed: 14 }\n\nconst INITIAL: Meter[] = [\n  { id: \"bookings\", label: \"Online bookings\", unit: \"bookings\", used: 1842, limit: 2000, icon: CalendarCheck, resets: true, daily: [118, 124, 131, 140, 122, 156, 171, 98, 120, 133, 129, 145, 162, 143], pack: { size: 250, price: 15, noun: \"booking pack\" }, cta: \"Buy bookings\" },\n  { id: \"sms\", label: \"SMS credits\", unit: \"credits\", used: 1036, limit: 1000, icon: MessageSquareText, resets: true, daily: [64, 71, 69, 80, 75, 92, 88, 61, 70, 74, 66, 79, 85, 62], pack: { size: 500, price: 20, noun: \"credit pack\" }, cta: \"Buy credits\" },\n  { id: \"seats\", label: \"Staff seats\", unit: \"seats\", used: 6, limit: 10, icon: Users, resets: false, daily: [5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6], pack: { size: 1, price: 12, noun: \"seat\", recurring: true }, cta: \"Add seats\" },\n  { id: \"storage\", label: \"File storage\", unit: \"GB\", used: 3.2, limit: 5, icon: HardDrive, resets: false, daily: [2.6, 2.6, 2.7, 2.7, 2.8, 2.8, 2.9, 2.9, 3, 3, 3.1, 3.1, 3.2, 3.2], pack: { size: 10, price: 4, noun: \"10 GB block\", recurring: true }, cta: \"Add storage\" },\n]\n\nconst LEVEL = {\n  ok: { label: \"Healthy\", stroke: \"stroke-emerald-500\", bg: \"bg-emerald-500\", pill: \"bg-emerald-500/10 text-emerald-700 dark:text-emerald-400\" },\n  warn: { label: \"Running low\", stroke: \"stroke-amber-500\", bg: \"bg-amber-500\", pill: \"bg-amber-500/10 text-amber-700 dark:text-amber-400\" },\n  over: { label: \"Over limit\", stroke: \"stroke-rose-500\", bg: \"bg-rose-500\", pill: \"bg-rose-500/10 text-rose-700 dark:text-rose-400\" },\n} as const\nconst levelOf = (pct: number) => (pct >= 100 ? \"over\" : pct >= 75 ? \"warn\" : \"ok\")\nconst fmt = (n: number, unit: string) => (unit === \"GB\" ? `${n.toFixed(1)} GB` : n.toLocaleString(\"en-US\"))\n\nconst R = 50\nconst polar = (deg: number, r = R) => [60 + r * Math.cos((deg * Math.PI) / 180), 60 + r * Math.sin((deg * Math.PI) / 180)]\nconst [SX, SY] = polar(135)\nconst [EX, EY] = polar(45)\nconst ARC = `M ${SX} ${SY} A ${R} ${R} 0 1 1 ${EX} ${EY}`\n\nfunction Gauge({ pct, alertAt, className }: { pct: number; alertAt: number | null; className?: string }) {\n  const level = levelOf(pct)\n  const tick = alertAt ? 135 + (270 * alertAt) / 100 : null\n  const [ax, ay] = tick ? polar(tick, 41) : [0, 0]\n  const [bx, by] = tick ? polar(tick, 59) : [0, 0]\n  return (\n    <svg viewBox=\"0 0 120 108\" className={className} aria-hidden>\n      <path d={ARC} pathLength={100} fill=\"none\" strokeWidth={10} strokeLinecap=\"round\" className=\"stroke-muted\" />\n      <path\n        d={ARC}\n        pathLength={100}\n        fill=\"none\"\n        strokeWidth={10}\n        strokeLinecap=\"round\"\n        strokeDasharray={`${Math.min(100, Math.max(0.5, pct))} 100`}\n        className={cn(LEVEL[level].stroke, \"transition-[stroke-dasharray] duration-700 ease-out\")}\n      />\n      {tick && <line x1={ax} y1={ay} x2={bx} y2={by} strokeWidth={2} strokeLinecap=\"round\" className=\"stroke-foreground/50\" />}\n    </svg>\n  )\n}\n\nexport default function UsageMeters() {\n  const [meters, setMeters] = React.useState(INITIAL)\n  const [alerts, setAlerts] = React.useState(true)\n  const [buyFor, setBuyFor] = React.useState<MeterId | null>(null)\n  const [qty, setQty] = React.useState(1)\n  const [phase, setPhase] = React.useState<\"form\" | \"processing\" | \"done\">(\"form\")\n  const [justBought, setJustBought] = React.useState<MeterId | null>(null)\n\n  const open = (id: MeterId) => {\n    setBuyFor(id)\n    setQty(id === \"seats\" ? 2 : 1)\n    setPhase(\"form\")\n  }\n  const sel = meters.find((m) => m.id === buyFor) ?? null\n  const discount = qty >= 5 ? 0.1 : 0\n  const subtotal = sel ? sel.pack.price * qty : 0\n  const total = subtotal * (1 - discount)\n  const newLimit = sel ? sel.limit + sel.pack.size * qty : 0\n  const newPct = sel ? (sel.used / newLimit) * 100 : 0\n\n  const purchase = () => {\n    if (!sel || qty < 1) return\n    setPhase(\"processing\")\n    setTimeout(() => {\n      setMeters((list) => list.map((m) => (m.id === sel.id ? { ...m, limit: m.limit + m.pack.size * qty } : m)))\n      setPhase(\"done\")\n      setJustBought(sel.id)\n      setTimeout(() => setJustBought(null), 2500)\n    }, 1000)\n  }\n\n  const elapsedPct = (PERIOD.elapsed / PERIOD.days) * 100\n  const needsAttention = meters.filter((m) => m.used / m.limit >= 0.75).length\n\n  return (\n    <div className=\"p-4 md:p-6\">\n      <div className=\"flex flex-col gap-4\">\n        <div className=\"relative overflow-hidden rounded-2xl border bg-card p-5\">\n          <div className=\"pointer-events-none absolute -top-20 -right-20 size-64 rounded-full bg-[var(--chart-2)] opacity-15 blur-3xl\" />\n          <div className=\"relative flex flex-wrap items-start justify-between gap-4\">\n            <div>\n              <div className=\"text-xs font-medium tracking-wide text-muted-foreground uppercase\">Usage this period</div>\n              <h2 className=\"mt-1 text-2xl font-semibold tracking-tight\">\n                {needsAttention ? `${needsAttention} of ${meters.length} limits need attention` : \"All limits look healthy\"}\n              </h2>\n              <p className=\"mt-1 text-sm text-muted-foreground\">Studio plan · {PERIOD.start} – {PERIOD.end}</p>\n            </div>\n            <label className=\"flex items-center gap-3 rounded-xl border bg-background/70 px-3 py-2 text-sm backdrop-blur\">\n              <BellRing className=\"size-4 text-muted-foreground\" />\n              <span>\n                <span className=\"font-medium\">Alert me at 80%</span>\n                <span className=\"block text-xs text-muted-foreground\">{alerts ? \"Email to owner@lumensalon.com\" : \"Alerts are off\"}</span>\n              </span>\n              <Switch checked={alerts} onCheckedChange={setAlerts} aria-label=\"Usage alerts at 80%\" />\n            </label>\n          </div>\n          <div className=\"relative mt-5\">\n            <div className=\"flex justify-between text-xs text-muted-foreground\">\n              <span>{PERIOD.start}</span>\n              <span className=\"inline-flex items-center gap-1 font-medium text-foreground\"><RefreshCw className=\"size-3\" /> Resets in {PERIOD.days - PERIOD.elapsed} days</span>\n              <span>{PERIOD.end}</span>\n            </div>\n            <div className=\"mt-2 h-2 overflow-hidden rounded-full bg-muted\">\n              <div className=\"h-full rounded-full bg-foreground/80\" style={{ width: `${elapsedPct}%` }} />\n            </div>\n          </div>\n        </div>\n\n        <div className=\"grid gap-4 sm:grid-cols-2 xl:grid-cols-4\">\n          {meters.map((m) => {\n            const pct = (m.used / m.limit) * 100\n            const level = levelOf(pct)\n            const projected = m.resets ? Math.round(m.used / (PERIOD.elapsed / PERIOD.days)) : null\n            const projectedOver = projected !== null && projected > m.limit\n            const maxDaily = Math.max(...m.daily)\n            const Icon = m.icon\n            return (\n              <div key={m.id} className={cn(\"flex flex-col rounded-2xl border bg-card p-4 transition-shadow\", justBought === m.id && \"ring-2 ring-emerald-500\")}>\n                <div className=\"flex items-center gap-2\">\n                  <span className=\"inline-flex size-8 items-center justify-center rounded-lg bg-muted\"><Icon className=\"size-4\" /></span>\n                  <span className=\"text-sm font-medium\">{m.label}</span>\n                  <span className={cn(\"ml-auto rounded-full px-2 py-0.5 text-[11px] font-semibold\", LEVEL[level].pill)}>{LEVEL[level].label}</span>\n                </div>\n                <div className=\"relative mx-auto mt-3 w-40\">\n                  <Gauge pct={pct} alertAt={alerts ? 80 : null} className=\"w-full\" />\n                  <div className=\"absolute inset-x-0 top-[34%] text-center\">\n                    <div className=\"text-3xl font-semibold tracking-tight tabular-nums\">{Math.round(pct)}%</div>\n                    <div className=\"text-xs text-muted-foreground tabular-nums\">\n                      {fmt(m.used, m.unit)} / {fmt(m.limit, m.unit)}\n                    </div>\n                  </div>\n                </div>\n                <div className=\"mt-1 flex h-8 items-end gap-0.5\" aria-label=\"Last 14 days\">\n                  {m.daily.map((d, i) => (\n                    <div key={i} className={cn(\"flex-1 rounded-t-sm\", i === m.daily.length - 1 ? LEVEL[level].bg : \"bg-muted-foreground/25\")} style={{ height: `${Math.max(12, (d / maxDaily) * 100)}%` }} />\n                  ))}\n                </div>\n                <div className=\"mt-3 min-h-10 text-xs text-muted-foreground\">\n                  {level === \"over\" ? (\n                    <span className=\"text-rose-700 dark:text-rose-400\">{fmt(m.used - m.limit, m.unit)} {m.unit === \"GB\" ? \"\" : m.unit} over — extra usage is billed at overage rates.</span>\n                  ) : projected !== null ? (\n                    <>\n                      On pace for <span className={cn(\"font-medium\", projectedOver ? \"text-amber-700 dark:text-amber-400\" : \"text-foreground\")}>{fmt(projected, m.unit)}</span> by {PERIOD.end}\n                      {projectedOver ? ` — ${Math.round((projected / m.limit - 1) * 100)}% over limit.` : \".\"}\n                    </>\n                  ) : (\n                    <>{fmt(m.limit - m.used, m.unit)} {m.unit === \"GB\" ? \"free\" : `${m.unit} available`} · doesn't reset</>\n                  )}\n                </div>\n                <Button className=\"mt-3 w-full\" variant={level === \"ok\" ? \"outline\" : \"default\"} onClick={() => open(m.id)}>\n                  <Plus /> {m.cta}\n                </Button>\n              </div>\n            )\n          })}\n        </div>\n      </div>\n\n      <Dialog open={!!buyFor} onOpenChange={(o) => !o && phase !== \"processing\" && setBuyFor(null)}>\n        <DialogContent className=\"sm:max-w-md\">\n          {sel && phase === \"done\" ? (\n            <div className=\"flex flex-col items-center gap-3 py-4 text-center\">\n              <div className=\"inline-flex size-14 items-center justify-center rounded-full bg-emerald-500/15 text-emerald-600 dark:text-emerald-400\"><Check className=\"size-7\" /></div>\n              <DialogTitle className=\"text-xl\">Limit increased</DialogTitle>\n              <DialogDescription>\n                {sel.label} now has {fmt(sel.limit, sel.unit)} {sel.unit === \"GB\" ? \"\" : sel.unit} available. ${total.toFixed(2)} was charged to Visa •••• 4242.\n              </DialogDescription>\n              <Button className=\"mt-2 w-full\" onClick={() => setBuyFor(null)}>Back to usage</Button>\n            </div>\n          ) : sel ? (\n            <>\n              <DialogHeader>\n                <DialogTitle>Add more capacity</DialogTitle>\n                <DialogDescription>Applies instantly. {sel.pack.recurring ? \"Added to your monthly bill.\" : \"Unused credits roll over for 12 months.\"}</DialogDescription>\n              </DialogHeader>\n\n              <div role=\"radiogroup\" aria-label=\"What to add\" className=\"grid grid-cols-2 gap-2\">\n                {meters.map((m) => {\n                  const Icon = m.icon\n                  const on = m.id === sel.id\n                  return (\n                    <button\n                      key={m.id}\n                      type=\"button\"\n                      role=\"radio\"\n                      aria-checked={on}\n                      disabled={phase === \"processing\"}\n                      onClick={() => {\n                        setBuyFor(m.id)\n                        setQty(1)\n                      }}\n                      className={cn(\"flex flex-col items-start gap-1 rounded-xl border p-3 text-left transition-colors hover:bg-muted/50\", on && \"border-primary bg-primary/5 ring-1 ring-primary\")}\n                    >\n                      <Icon className=\"size-4 text-muted-foreground\" />\n                      <span className=\"text-sm font-medium\">{m.label}</span>\n                      <span className=\"text-xs text-muted-foreground\">${m.pack.price} / {m.pack.size === 1 ? m.pack.noun : `${m.pack.size.toLocaleString(\"en-US\")} ${m.unit === \"GB\" ? \"GB\" : m.unit}`}{m.pack.recurring ? \" / mo\" : \"\"}</span>\n                    </button>\n                  )\n                })}\n              </div>\n\n              <div className=\"flex items-center justify-between gap-4\">\n                <Label htmlFor=\"buy-qty\" className=\"flex flex-col items-start gap-0.5\">\n                  Quantity\n                  <span className=\"text-xs font-normal text-muted-foreground\">10% off 5 or more</span>\n                </Label>\n                <div className=\"flex items-center rounded-lg border\">\n                  <Button variant=\"ghost\" size=\"icon-sm\" aria-label=\"Decrease quantity\" disabled={qty <= 1 || phase === \"processing\"} onClick={() => setQty((q) => Math.max(1, q - 1))}><Minus /></Button>\n                  <Input\n                    id=\"buy-qty\"\n                    inputMode=\"numeric\"\n                    value={qty}\n                    onChange={(e) => setQty(Math.max(1, Math.min(50, Number(e.target.value.replace(/\\D/g, \"\")) || 1)))}\n                    className=\"h-7 w-12 border-0 text-center tabular-nums shadow-none focus-visible:ring-0 dark:bg-transparent\"\n                  />\n                  <Button variant=\"ghost\" size=\"icon-sm\" aria-label=\"Increase quantity\" disabled={qty >= 50 || phase === \"processing\"} onClick={() => setQty((q) => Math.min(50, q + 1))}><Plus /></Button>\n                </div>\n              </div>\n\n              <div className=\"rounded-xl bg-muted/60 p-3\">\n                <div className=\"flex items-center justify-between text-xs text-muted-foreground\">\n                  <span>New limit</span>\n                  <span className=\"tabular-nums\">{fmt(sel.limit, sel.unit)} → <span className=\"font-semibold text-foreground\">{fmt(newLimit, sel.unit)}</span></span>\n                </div>\n                <div className=\"mt-2 h-2 overflow-hidden rounded-full bg-background\">\n                  <div className={cn(\"h-full rounded-full transition-all duration-500\", LEVEL[levelOf(newPct)].bg)} style={{ width: `${Math.min(100, newPct)}%` }} />\n                </div>\n                <div className=\"mt-1 text-right text-xs text-muted-foreground tabular-nums\">{Math.round(newPct)}% used after purchase</div>\n              </div>\n\n              <dl className=\"flex flex-col gap-1.5 text-sm\">\n                <div className=\"flex justify-between\"><dt className=\"text-muted-foreground\">{qty} × {sel.pack.noun}</dt><dd className=\"tabular-nums\">${subtotal.toFixed(2)}</dd></div>\n                {discount > 0 && <div className=\"flex justify-between text-emerald-700 dark:text-emerald-400\"><dt>Volume discount (10%)</dt><dd className=\"tabular-nums\">−${(subtotal * discount).toFixed(2)}</dd></div>}\n                <div className=\"flex items-baseline justify-between border-t pt-2\"><dt className=\"font-medium\">Total{sel.pack.recurring ? \" / month\" : \"\"}</dt><dd className=\"text-2xl font-semibold tracking-tight tabular-nums\">${total.toFixed(2)}</dd></div>\n              </dl>\n\n              <DialogFooter>\n                <Button variant=\"outline\" onClick={() => setBuyFor(null)} disabled={phase === \"processing\"}>Cancel</Button>\n                <Button onClick={purchase} disabled={phase === \"processing\"}>\n                  {phase === \"processing\" && <Loader2 className=\"animate-spin\" />}\n                  {phase === \"processing\" ? \"Processing…\" : `Pay $${total.toFixed(2)}`}\n                </Button>\n              </DialogFooter>\n            </>\n          ) : null}\n        </DialogContent>\n      </Dialog>\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/usage-meters.tsx"
    }
  ],
  "categories": [
    "billing"
  ],
  "type": "registry:block"
}