import React from "react"; import { RefreshCw, AlertCircle } from "lucide-react"; import { useTranslation } from "react-i18next"; import { type AppId } from "@/lib/api"; import { useUsageQuery } from "@/lib/query/queries"; import { UsageData } from "../types"; interface UsageFooterProps { providerId: string; appId: AppId; usageEnabled: boolean; // 是否启用了用量查询 } const UsageFooter: React.FC = ({ providerId, appId, usageEnabled }) => { const { t } = useTranslation(); const { data: usage, isLoading: loading, refetch, } = useUsageQuery(providerId, appId, usageEnabled); // 只在启用用量查询且有数据时显示 if (!usageEnabled || !usage) return null; // 错误状态 if (!usage.success) { return (
{usage.error || t("usage.queryFailed")}
{/* 刷新按钮 */}
); } const usageDataList = usage.data || []; // 无数据时不显示 if (usageDataList.length === 0) return null; return (
{/* 标题行:包含刷新按钮 */}
{t("usage.planUsage")}
{/* 套餐列表 */}
{usageDataList.map((usageData, index) => ( ))}
); }; // 单个套餐数据展示组件 const UsagePlanItem: React.FC<{ data: UsageData }> = ({ data }) => { const { t } = useTranslation(); const { planName, extra, isValid, invalidMessage, total, used, remaining, unit, } = data; // 判断套餐是否失效(isValid 为 false 或未定义时视为有效) const isExpired = isValid === false; return (
{/* 标题部分:25% */}
{planName ? ( 💰 {planName} ) : ( )}
{/* 扩展字段:30% */}
{extra && ( {extra} )} {isExpired && ( {invalidMessage || t("usage.invalid")} )}
{/* 用量信息:45% */}
{/* 总额度 */} {total !== undefined && ( <> {t("usage.total")} {total === -1 ? "∞" : total.toFixed(2)} | )} {/* 已用额度 */} {used !== undefined && ( <> {t("usage.used")} {used.toFixed(2)} | )} {/* 剩余额度 - 突出显示 */} {remaining !== undefined && ( <> {t("usage.remaining")} {remaining.toFixed(2)} )} {unit && ( {unit} )}
); }; export default UsageFooter;