"use client";
import React from "react";
import { trpc } from "@/lib/trpc";
import { AlertTriangle, CheckCircle, XCircle } from "lucide-react";
interface UsageMeterProps {
/** The workspace ID is needed only to gate the query (enabled). */
workspaceId: string;
}
type StatRow = {
label: string;
current: number;
limit: number | null;
barColor: string;
};
function isAtLimit(current: number, limit: number | null): "ok" | "warn" | "exceeded" {
if (limit === null) return "ok";
const ratio = current / limit;
if (ratio >= 1) return "exceeded";
if (ratio >= 0.8) return "warn";
return "ok";
}
export default function UsageMeter({ workspaceId }: UsageMeterProps) {
const { data, isLoading, error } = trpc.billing.getUsage.useQuery(
undefined,
{ enabled: !!workspaceId, refetchInterval: 60_000 },
);
if (isLoading) {
return (
);
}
if (error || !data) return null;
const { usage, planId, status } = data;
const rows: StatRow[] = [];
if (usage.guestsPerMonth.limit !== 0) {
rows.push({
label: "Guests (this month)",
current: usage.guestsPerMonth.current,
limit: usage.guestsPerMonth.limit,
barColor: "var(--color-primary)",
});
}
if (usage.episodesPerMonth.limit !== 0) {
rows.push({
label: "Episodes (this month)",
current: usage.episodesPerMonth.current,
limit: usage.episodesPerMonth.limit,
barColor: "var(--color-secondary)",
});
}
if (usage.clipsPerMonth.limit !== 0) {
rows.push({
label: "AI Clips (this month)",
current: usage.clipsPerMonth.current,
limit: usage.clipsPerMonth.limit,
barColor: "var(--color-accent)",
});
}
if (usage.exportMinutesPerMonth.limit !== 0) {
rows.push({
label: "Export Minutes (this month)",
current: usage.exportMinutesPerMonth.current,
limit: usage.exportMinutesPerMonth.limit,
barColor: "#8B5CF6",
});
}
if (rows.length === 0) return null;
return (
Usage
{planId} ยท {status}
{rows.map((row) => {
const state = isAtLimit(row.current, row.limit);
const pct =
row.limit !== null && row.limit > 0
? Math.min(100, Math.round((row.current / row.limit) * 100))
: 0;
return (
{row.label}
{state === "exceeded" && (
)}
{state === "warn" && (
)}
{state === "ok" && row.limit !== null && (
)}
{row.limit === null ? "Unlimited" : `${row.current} / ${row.limit}`}
{row.limit !== null && (
)}
);
})}
);
}