import React from 'react'; import { useAdminUsage, useOrgQuotaViolations } from '@/lib/api-hooks'; import { Badge, Input } from '@/components/ui'; import { formatDate } from '@/lib/utils'; import { AlertTriangle, BellRing, ChevronDown, ChevronRight } from 'lucide-react'; function overageBadge(state: 'none' | 'warn' | 'over') { if (state === 'over') return Over; if (state === 'warn') return Warn; return ; } function AlertSentBadge({ lastAlertSentAt, alertThresholdsFired, }: { lastAlertSentAt: string | null; alertThresholdsFired: { meterKey: string; threshold: number; notifiedAt: string }[]; }) { if (!lastAlertSentAt) { return No alerts; } const thresholds = Array.from(new Set(alertThresholdsFired.map((a) => a.threshold))).sort( (a, b) => b - a, ); const visibleThresholds = thresholds.filter((t) => t >= 80); const labelThresholds = visibleThresholds.length > 0 ? visibleThresholds : thresholds; const tooltip = alertThresholdsFired .map((a) => `${a.threshold}% on ${a.meterKey} — ${formatDate(a.notifiedAt)}`) .join('\n'); return (
{labelThresholds.map((t) => `${t}%`).join(' · ')} · {formatDate(lastAlertSentAt)}
); } function QuotaViolationsPanel({ orgId }: { orgId: number }) { const { data, isLoading, error } = useOrgQuotaViolations(orgId, { limit: 50 }); if (isLoading) { return (
Loading violations…
); } if (error) { return (
Failed to load violations: {(error as Error).message}
); } if (!data || data.rows.length === 0) { return (
No quota violations recorded for this tenant.
); } return (

Quota Violations

{data.rows.length} of {data.pagination.total}
{data.rows.map((v) => ( ))}
When Type Feature Usage Limit Action
{formatDate(v.occurredAt)} {v.violationType === 'hard' ? ( Hard ) : ( Soft )} {v.featureKey} {v.currentUsage != null ? v.currentUsage.toLocaleString() : '—'} {v.limitValue != null ? v.limitValue.toLocaleString() : '—'} {v.action}
); } export default function AdminUsagePage() { const [orgSearch, setOrgSearch] = React.useState(''); const [plan, setPlan] = React.useState(''); const [expandedOrg, setExpandedOrg] = React.useState(null); const { data, isLoading, error } = useAdminUsage({ org: orgSearch || undefined, plan: plan || undefined, limit: 100, }); return (

Admin · Cross-Tenant Usage

Per-org usage, quota status, and overage alert history for the current billing period.

{data?.totals ? (
Orgs: {data.totals.orgs} Over: {data.totals.overageCount} Warn: {data.totals.warnCount}
) : null}
setOrgSearch(e.target.value)} className="max-w-xs" />
{error ? (
Failed to load usage data: {(error as Error).message}
) : null}
{isLoading ? ( ) : data?.rows.length === 0 ? ( ) : ( data?.rows.map((row) => { const isExpanded = expandedOrg === row.orgId; return ( setExpandedOrg(isExpanded ? null : row.orgId)} > {isExpanded ? ( ) : null} ); }) )}
Org Plan API calls Members Storage (MB) Overage Alert status
Loading…
No orgs match the current filters.
{row.orgName}
{row.orgSlug}
{row.plan} {row.apiCalls.toLocaleString()} {row.members} {row.storageMB.toLocaleString()}
{overageBadge(row.overages.apiCalls)} {overageBadge(row.overages.members)} {overageBadge(row.overages.storage)}
); }