Usage and Spending Limits
Progress meters for API requests, storage and build minutes with overage rates, an estimated total and a spending cap switch.
"use client"
import { useState } from "react"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import {
Card,
CardAction,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
import { Input } from "@/components/ui/input"
import {
Progress,
ProgressLabel,
ProgressValue,
} from "@/components/ui/progress"
import { Switch } from "@/components/ui/switch"
const COPY = {
title: "Usage this cycle",
description: "Nov 1 to Nov 30, 2026. Billed in arrears.",
badge: "18 days left",
base: "Pro plan base",
estimate: "Estimated total",
cap: (amount: number) => `Hard spending cap at $${amount}`,
manage: "Manage limits",
capNote: (amount: number) =>
`Capped. Usage stops at $${amount}.00; the rest is not billed.`,
overNote: (amount: number) =>
`Cap is off. $${amount.toFixed(2)} of usage would be billed beyond the cap.`,
dialogTitle: "Manage limits",
dialogBody: "Set the most this workspace can be billed in one cycle.",
capLabel: "Spending cap (USD)",
save: "Save cap",
cancel: "Cancel",
} as const
const METERS = [
{
label: "API requests",
used: 7.4,
included: 10,
unit: "M",
rate: 0.4,
overage: "$0.40 per 1M over",
},
{
label: "Storage",
used: 212,
included: 200,
unit: "GB",
rate: 0.12,
overage: "$0.12 per GB over",
},
{
label: "Build minutes",
used: 3120,
included: 6000,
unit: "min",
rate: 0.01,
overage: "$0.01 per min over",
},
] as const
export default function BillingUsageMeter() {
const [capped, setCapped] = useState(true)
const [cap, setCap] = useState(500)
const [draft, setDraft] = useState("500")
const [open, setOpen] = useState(false)
const extra = METERS.reduce(
(sum, m) => sum + Math.max(0, m.used - m.included) * m.rate,
0
)
const estimate = 99 + extra
const total = capped ? Math.min(estimate, cap) : estimate
const beyond = Math.max(0, estimate - cap)
const draftValue = Number(draft)
const draftValid = Number.isFinite(draftValue) && draftValue > 0
return (
<Card className="mx-auto w-full max-w-lg">
<CardHeader>
<CardTitle>{COPY.title}</CardTitle>
<CardDescription>{COPY.description}</CardDescription>
<CardAction>
<Badge variant="secondary">{COPY.badge}</Badge>
</CardAction>
</CardHeader>
<CardContent className="flex flex-col gap-5">
{METERS.map((meter) => {
const over = meter.used > meter.included
return (
<div key={meter.label} className="flex flex-col gap-1.5">
<Progress
value={Math.min(100, (meter.used / meter.included) * 100)}
>
<ProgressLabel>{meter.label}</ProgressLabel>
<ProgressValue>
{() =>
`${meter.used}${meter.unit} of ${meter.included}${meter.unit}`
}
</ProgressValue>
</Progress>
<p className="text-xs text-muted-foreground">
{over
? `Over the included amount. ${meter.overage}.`
: `Included. ${meter.overage}.`}
</p>
</div>
)
})}
<dl className="flex flex-col gap-2 rounded-2xl bg-muted/50 p-4 text-sm">
<div className="flex justify-between">
<dt className="text-muted-foreground">{COPY.base}</dt>
<dd className="tabular-nums">$99.00</dd>
</div>
<div className="flex justify-between">
<dt className="text-muted-foreground">Overage</dt>
<dd className="tabular-nums">${extra.toFixed(2)}</dd>
</div>
<div className="flex justify-between border-t pt-2 font-medium">
<dt>{COPY.estimate}</dt>
<dd className="tabular-nums">${total.toFixed(2)}</dd>
</div>
</dl>
<p aria-live="polite" className="text-xs text-muted-foreground">
{capped ? COPY.capNote(cap) : COPY.overNote(beyond)}
</p>
</CardContent>
<CardFooter className="justify-between gap-3">
<label className="flex items-center gap-2 text-sm">
<Switch size="sm" checked={capped} onCheckedChange={setCapped} />
{COPY.cap(cap)}
</label>
<Dialog
open={open}
onOpenChange={(next) => {
setOpen(next)
if (next) setDraft(String(cap))
}}
>
<DialogTrigger render={<Button variant="outline" size="sm" />}>
{COPY.manage}
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>{COPY.dialogTitle}</DialogTitle>
<DialogDescription>{COPY.dialogBody}</DialogDescription>
</DialogHeader>
<label className="flex flex-col gap-2 text-sm">
{COPY.capLabel}
<Input
type="number"
min={1}
inputMode="numeric"
value={draft}
onChange={(e) => setDraft(e.target.value)}
/>
</label>
<DialogFooter>
<DialogClose render={<Button variant="outline" />}>
{COPY.cancel}
</DialogClose>
<Button
disabled={!draftValid}
onClick={() => {
setCap(Math.round(draftValue))
setOpen(false)
}}
>
{COPY.save}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</CardFooter>
</Card>
)
}
Installation
pnpm dlx shadcn@latest add @01orbital/usage-meter-01npx shadcn@latest add @01orbital/usage-meter-01yarn dlx shadcn@latest add @01orbital/usage-meter-01bunx --bun shadcn@latest add @01orbital/usage-meter-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/billing/usage-meter-01/index.tsx: change them there, not in the markup. METERS, also at the top of the file, holds its lists and settings.
Built from
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on