|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect } from "react"; |
| 4 | + |
| 5 | +import { useTopNavStore } from "@/stores/top-nav"; |
| 6 | +import { usePlanStore } from "@/stores/plan"; |
| 7 | +import { Organization } from "@/types"; |
| 8 | +import { OrganizationUsageData } from "@/lib/supabase/operations/organizations"; |
| 9 | +import { |
| 10 | + Card, |
| 11 | + CardContent, |
| 12 | + CardDescription, |
| 13 | + CardHeader, |
| 14 | + CardTitle, |
| 15 | +} from "@/components/ui/card"; |
| 16 | +import { Badge } from "@/components/ui/badge"; |
| 17 | +import { PlanType } from "@/stores/plan"; |
| 18 | + |
| 19 | +interface UsagePageClientProps { |
| 20 | + currentOrganization: Organization; |
| 21 | + allOrganizations: Array<{ id: string; name: string; plan: PlanType }>; |
| 22 | + usageData: OrganizationUsageData; |
| 23 | +} |
| 24 | + |
| 25 | +function formatStorage(bytes: number): string { |
| 26 | + if (bytes === 0) return "0 B"; |
| 27 | + const units = ["B", "KB", "MB", "GB", "TB"]; |
| 28 | + const i = Math.floor(Math.log(bytes) / Math.log(1024)); |
| 29 | + const value = bytes / Math.pow(1024, i); |
| 30 | + return `${value.toFixed(value < 10 && i > 0 ? 1 : 0)} ${units[i]}`; |
| 31 | +} |
| 32 | + |
| 33 | +function getProgressColor(percentage: number): string { |
| 34 | + if (percentage >= 90) return "bg-red-500"; |
| 35 | + if (percentage >= 70) return "bg-amber-500"; |
| 36 | + return ""; |
| 37 | +} |
| 38 | + |
| 39 | +interface UsageMetric { |
| 40 | + label: string; |
| 41 | + description: string; |
| 42 | + current: number; |
| 43 | + max: number; |
| 44 | + formatValue?: (v: number) => string; |
| 45 | +} |
| 46 | + |
| 47 | +function UsageCard({ metric }: { metric: UsageMetric }) { |
| 48 | + const percentage = metric.max > 0 ? Math.min((metric.current / metric.max) * 100, 100) : 0; |
| 49 | + const format = metric.formatValue || ((v: number) => v.toLocaleString()); |
| 50 | + const colorClass = getProgressColor(percentage); |
| 51 | + |
| 52 | + return ( |
| 53 | + <Card> |
| 54 | + <CardHeader className="pb-3"> |
| 55 | + <div className="flex items-center justify-between"> |
| 56 | + <div> |
| 57 | + <CardTitle className="text-base">{metric.label}</CardTitle> |
| 58 | + <CardDescription className="text-xs mt-0.5"> |
| 59 | + {metric.description} |
| 60 | + </CardDescription> |
| 61 | + </div> |
| 62 | + <span className="text-sm text-muted-foreground tabular-nums"> |
| 63 | + {format(metric.current)} / {metric.max > 0 ? format(metric.max) : "∞"} |
| 64 | + </span> |
| 65 | + </div> |
| 66 | + </CardHeader> |
| 67 | + <CardContent> |
| 68 | + <div className="relative h-2 w-full overflow-hidden rounded-full bg-muted"> |
| 69 | + <div |
| 70 | + className={`h-full rounded-full transition-all ${colorClass || "bg-primary"}`} |
| 71 | + style={{ width: `${percentage}%` }} |
| 72 | + /> |
| 73 | + </div> |
| 74 | + {percentage >= 90 && metric.max > 0 && ( |
| 75 | + <p className="text-xs text-red-500 mt-2"> |
| 76 | + {percentage >= 100 |
| 77 | + ? "Quota exceeded. Upgrade your plan to continue." |
| 78 | + : "Approaching quota limit."} |
| 79 | + </p> |
| 80 | + )} |
| 81 | + </CardContent> |
| 82 | + </Card> |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +export function UsagePageClient({ |
| 87 | + currentOrganization, |
| 88 | + allOrganizations, |
| 89 | + usageData, |
| 90 | +}: UsagePageClientProps) { |
| 91 | + const { initialize, setHasSidebar } = useTopNavStore(); |
| 92 | + const { getPriceByProduct, getPlanDisplayName: getPlanDisplayNameFromPrice } = |
| 93 | + usePlanStore(); |
| 94 | + |
| 95 | + useEffect(() => { |
| 96 | + initialize({ |
| 97 | + title: "", |
| 98 | + organization: currentOrganization, |
| 99 | + project: null, |
| 100 | + organizations: allOrganizations, |
| 101 | + projects: [], |
| 102 | + hasSidebar: true, |
| 103 | + }); |
| 104 | + |
| 105 | + return () => { |
| 106 | + setHasSidebar(false); |
| 107 | + }; |
| 108 | + }, [currentOrganization, allOrganizations, initialize, setHasSidebar]); |
| 109 | + |
| 110 | + const getPlanDisplayName = (plan: string | undefined) => { |
| 111 | + if (!plan || plan === "free") return "Free Plan"; |
| 112 | + const priceInfo = getPriceByProduct(plan); |
| 113 | + if (priceInfo) { |
| 114 | + const displayName = getPlanDisplayNameFromPrice(priceInfo); |
| 115 | + return `${displayName} Plan`; |
| 116 | + } |
| 117 | + return `${plan.charAt(0).toUpperCase() + plan.slice(1)} Plan`; |
| 118 | + }; |
| 119 | + |
| 120 | + const { usage, limits, period_end } = usageData; |
| 121 | + |
| 122 | + const metrics: UsageMetric[] = [ |
| 123 | + { |
| 124 | + label: "Agent Tasks", |
| 125 | + description: "Total agent tasks created this billing period", |
| 126 | + current: usage.current_task, |
| 127 | + max: limits.max_task, |
| 128 | + }, |
| 129 | + { |
| 130 | + label: "Storage", |
| 131 | + description: "Total storage used across all projects", |
| 132 | + current: usage.current_storage, |
| 133 | + max: limits.max_storage, |
| 134 | + formatValue: formatStorage, |
| 135 | + }, |
| 136 | + ]; |
| 137 | + |
| 138 | + return ( |
| 139 | + <div className="container mx-auto py-8 px-4 max-w-6xl"> |
| 140 | + <div className="flex flex-col gap-6"> |
| 141 | + {/* Header */} |
| 142 | + <div> |
| 143 | + <h1 className="text-2xl font-semibold">Usage</h1> |
| 144 | + <p className="text-muted-foreground text-sm mt-1"> |
| 145 | + Resource usage for the current billing period. |
| 146 | + </p> |
| 147 | + </div> |
| 148 | + |
| 149 | + {/* Plan & Period */} |
| 150 | + <Card> |
| 151 | + <CardContent> |
| 152 | + <div className="flex items-center justify-between"> |
| 153 | + <div className="flex items-center gap-3"> |
| 154 | + <Badge variant="secondary" className="text-base px-4 py-1"> |
| 155 | + {getPlanDisplayName(currentOrganization.plan)} |
| 156 | + </Badge> |
| 157 | + </div> |
| 158 | + {period_end && ( |
| 159 | + <p className="text-sm text-muted-foreground"> |
| 160 | + Current period ends{" "} |
| 161 | + <span className="font-medium text-foreground"> |
| 162 | + {new Date(period_end).toLocaleDateString("en-US", { |
| 163 | + month: "short", |
| 164 | + day: "numeric", |
| 165 | + year: "numeric", |
| 166 | + })} |
| 167 | + </span> |
| 168 | + </p> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + </CardContent> |
| 172 | + </Card> |
| 173 | + |
| 174 | + {/* Usage Metrics */} |
| 175 | + {metrics.map((metric) => ( |
| 176 | + <UsageCard key={metric.label} metric={metric} /> |
| 177 | + ))} |
| 178 | + </div> |
| 179 | + </div> |
| 180 | + ); |
| 181 | +} |
0 commit comments