File size: 5,767 Bytes
88c4c60 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | "use client";
import { useState } from "react";
import Card from "@/shared/components/Card";
import ProviderIcon from "@/shared/components/ProviderIcon";
import Badge from "@/shared/components/Badge";
import QuotaProgressBar from "./QuotaProgressBar";
import { calculatePercentage } from "./utils";
const planVariants = {
free: "default",
pro: "primary",
ultra: "success",
enterprise: "info",
};
export default function ProviderLimitCard({
provider,
name,
plan,
quotas = [],
message = null,
loading = false,
error = null,
onRefresh,
}) {
const [refreshing, setRefreshing] = useState(false);
const handleRefresh = async () => {
if (!onRefresh || refreshing) return;
setRefreshing(true);
try {
await onRefresh();
} finally {
setRefreshing(false);
}
};
// Get provider info from config
const getProviderColor = () => {
const colors = {
github: "#000000",
antigravity: "#4285F4",
codex: "#10A37F",
kiro: "#FF9900",
qoder: "#EC4899",
claude: "#D97757",
};
return colors[provider?.toLowerCase()] || "#6B7280";
};
const providerColor = getProviderColor();
const planVariant = planVariants[plan?.toLowerCase()] || "default";
return (
<Card padding="md" className="flex flex-col gap-4">
{/* Header */}
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
{/* Provider Logo */}
<div
className="size-10 rounded-lg flex items-center justify-center p-1.5"
style={{ backgroundColor: `${providerColor}15` }}
>
<ProviderIcon
src={`/providers/${provider}.png`}
alt={provider || "Provider"}
size={40}
className="object-contain rounded-lg"
fallbackText={provider?.slice(0, 2).toUpperCase() || "PR"}
fallbackColor={providerColor}
/>
</div>
<div>
<h3 className="font-semibold text-text-primary">
{name || provider}
</h3>
{plan && (
<Badge
variant={planVariants[plan?.toLowerCase()] || "default"}
size="xs"
>
{plan}
</Badge>
)}
</div>
</div>
{/* Refresh Button */}
<button
onClick={handleRefresh}
disabled={refreshing || loading}
className="p-2 rounded-lg hover:bg-black/5 dark:hover:bg-white/5 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
title="Refresh quota"
>
<span
className={`material-symbols-outlined text-[20px] text-text-muted ${
refreshing || loading ? "animate-spin" : ""
}`}
>
refresh
</span>
</button>
</div>
{/* Loading State */}
{loading && (
<div className="space-y-4">
<div className="space-y-2">
<div className="h-4 bg-black/5 dark:bg-white/5 rounded animate-pulse" />
<div className="h-2 bg-black/5 dark:bg-white/5 rounded animate-pulse" />
</div>
<div className="space-y-2">
<div className="h-4 bg-black/5 dark:bg-white/5 rounded animate-pulse" />
<div className="h-2 bg-black/5 dark:bg-white/5 rounded animate-pulse" />
</div>
</div>
)}
{/* Error State */}
{!loading && error && (
<div className="p-4 rounded-lg bg-red-500/10 border border-red-500/20">
<div className="flex items-start gap-2">
<span className="material-symbols-outlined text-red-500 text-[20px]">
error
</span>
<p className="text-sm text-red-600 dark:text-red-400">{error}</p>
</div>
</div>
)}
{/* Info Message (for providers without API) */}
{!loading && !error && message && (
<div className="p-4 rounded-lg bg-blue-500/10 border border-blue-500/20">
<div className="flex items-start gap-2">
<span className="material-symbols-outlined text-blue-500 text-[20px]">
info
</span>
<p className="text-sm text-blue-600 dark:text-blue-400">
{message}
</p>
</div>
</div>
)}
{/* Quota Progress Bars */}
{!loading && !error && !message && quotas?.length > 0 && (
<div className="space-y-4">
{quotas.map((quota, index) => {
// For Antigravity, use remainingPercentage if available, otherwise calculate
const percentage =
quota.remainingPercentage !== undefined
? Math.round(((quota.total - quota.used) / quota.total) * 100)
: calculatePercentage(quota.used, quota.total);
const unlimited = quota.total === 0 || quota.total === null;
return (
<QuotaProgressBar
key={`${quota.name}-${index}`}
label={quota.name}
used={quota.used}
total={quota.total}
percentage={percentage}
unlimited={unlimited}
resetTime={quota.resetAt}
/>
);
})}
</div>
)}
{/* Empty State */}
{!loading && !error && !message && quotas?.length === 0 && (
<div className="text-center py-8 text-text-muted">
<span className="material-symbols-outlined text-[48px] opacity-20">
data_usage
</span>
<p className="text-sm mt-2">No quota data available</p>
</div>
)}
</Card>
);
}
|