Spaces:
Runtime error
Runtime error
File size: 5,827 Bytes
cd8bd0a | 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 | "use client";
import { useState } from "react";
import Image from "next/image";
import { useTranslations } from "next-intl";
import Card from "@/shared/components/Card";
import Badge from "@/shared/components/Badge";
import QuotaProgressBar from "./QuotaProgressBar";
import { calculatePercentage, shouldShowQuotaUsageCount } from "./utils";
import ProviderIcon from "@/shared/components/ProviderIcon";
const planVariants = {
free: "default",
lite: "primary",
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 [imgError, setImgError] = useState(false);
const t = useTranslations("usage");
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",
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` }}
>
{imgError ? (
<span className="text-sm font-bold" style={{ color: providerColor }}>
{provider?.slice(0, 2).toUpperCase() || "PR"}
</span>
) : (
<ProviderIcon providerId={provider} size={40} />
)}
</div>
<div>
<h3 className="font-semibold text-text-primary">{name || provider}</h3>
{plan && (
<Badge
variant={(planVariants as any)[plan?.toLowerCase()] || "default"}
size={"xs" as any}
>
{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={t("refreshQuota")}
>
<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) => {
const percentage =
quota.remainingPercentage !== undefined
? Math.round(quota.remainingPercentage)
: 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}
staleAfterReset={quota.staleAfterReset === true}
showUsageCount={shouldShowQuotaUsageCount(quota)}
/>
);
})}
</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">{t("noQuotaDataAvailable")}</p>
</div>
)}
</Card>
);
}
|