2025-10-17 19:18:10 +08:00
|
|
|
|
import React from "react";
|
2025-11-05 15:48:19 +08:00
|
|
|
|
import { RefreshCw, AlertCircle, Clock } from "lucide-react";
|
2025-10-19 11:55:46 +08:00
|
|
|
|
import { useTranslation } from "react-i18next";
|
2025-10-30 14:59:15 +08:00
|
|
|
|
import { type AppId } from "@/lib/api";
|
2025-10-17 19:18:10 +08:00
|
|
|
|
import { useUsageQuery } from "@/lib/query/queries";
|
2025-11-05 21:40:06 +08:00
|
|
|
|
import { UsageData, Provider } from "@/types";
|
2025-10-15 09:15:25 +08:00
|
|
|
|
|
|
|
|
|
|
interface UsageFooterProps {
|
2025-11-05 15:48:19 +08:00
|
|
|
|
provider: Provider;
|
2025-10-15 09:15:25 +08:00
|
|
|
|
providerId: string;
|
2025-10-30 14:59:15 +08:00
|
|
|
|
appId: AppId;
|
2025-10-15 09:15:25 +08:00
|
|
|
|
usageEnabled: boolean; // 是否启用了用量查询
|
2025-11-05 15:48:19 +08:00
|
|
|
|
isCurrent: boolean; // 是否为当前激活的供应商
|
2025-11-05 22:46:30 +08:00
|
|
|
|
inline?: boolean; // 是否内联显示(在按钮左侧)
|
2025-10-15 09:15:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-30 15:31:08 +08:00
|
|
|
|
const UsageFooter: React.FC<UsageFooterProps> = ({
|
2025-11-05 15:48:19 +08:00
|
|
|
|
provider,
|
2025-10-30 15:31:08 +08:00
|
|
|
|
providerId,
|
|
|
|
|
|
appId,
|
|
|
|
|
|
usageEnabled,
|
2025-11-05 15:48:19 +08:00
|
|
|
|
isCurrent,
|
2025-11-05 22:46:30 +08:00
|
|
|
|
inline = false,
|
2025-10-30 15:31:08 +08:00
|
|
|
|
}) => {
|
2025-10-19 11:55:46 +08:00
|
|
|
|
const { t } = useTranslation();
|
2025-11-05 15:48:19 +08:00
|
|
|
|
|
2025-11-05 21:40:06 +08:00
|
|
|
|
// 统一的用量查询(自动查询仅对当前激活的供应商启用)
|
|
|
|
|
|
const autoQueryInterval = isCurrent
|
|
|
|
|
|
? provider.meta?.usage_script?.autoQueryInterval || 0
|
|
|
|
|
|
: 0;
|
|
|
|
|
|
|
2025-10-18 16:52:02 +08:00
|
|
|
|
const {
|
2025-11-05 21:40:06 +08:00
|
|
|
|
data: usage,
|
2025-11-03 23:13:30 +08:00
|
|
|
|
isFetching: loading,
|
2025-11-05 21:40:06 +08:00
|
|
|
|
lastQueriedAt,
|
2025-10-18 16:52:02 +08:00
|
|
|
|
refetch,
|
2025-11-05 21:40:06 +08:00
|
|
|
|
} = useUsageQuery(providerId, appId, {
|
|
|
|
|
|
enabled: usageEnabled,
|
|
|
|
|
|
autoQueryInterval,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 🆕 定期更新当前时间,用于刷新相对时间显示
|
|
|
|
|
|
const [now, setNow] = React.useState(Date.now());
|
2025-10-15 09:15:25 +08:00
|
|
|
|
|
2025-11-05 21:40:06 +08:00
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
|
if (!lastQueriedAt) return;
|
2025-11-05 15:48:19 +08:00
|
|
|
|
|
2025-11-05 21:40:06 +08:00
|
|
|
|
// 每30秒更新一次当前时间,触发相对时间显示的刷新
|
|
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
|
|
setNow(Date.now());
|
|
|
|
|
|
}, 30000); // 30秒
|
|
|
|
|
|
|
|
|
|
|
|
return () => clearInterval(interval);
|
|
|
|
|
|
}, [lastQueriedAt]);
|
2025-11-05 15:48:19 +08:00
|
|
|
|
|
2025-10-15 09:15:25 +08:00
|
|
|
|
// 只在启用用量查询且有数据时显示
|
|
|
|
|
|
if (!usageEnabled || !usage) return null;
|
|
|
|
|
|
|
|
|
|
|
|
// 错误状态
|
|
|
|
|
|
if (!usage.success) {
|
2025-11-05 22:46:30 +08:00
|
|
|
|
if (inline) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="flex items-center gap-2 text-xs">
|
|
|
|
|
|
<div className="flex items-center gap-1.5 text-red-500 dark:text-red-400">
|
|
|
|
|
|
<AlertCircle size={12} />
|
|
|
|
|
|
<span>{t("usage.queryFailed")}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={() => refetch()}
|
|
|
|
|
|
disabled={loading}
|
|
|
|
|
|
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50 flex-shrink-0"
|
|
|
|
|
|
title={t("usage.refreshUsage")}
|
|
|
|
|
|
>
|
|
|
|
|
|
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-15 09:15:25 +08:00
|
|
|
|
return (
|
refactor: implement unified border design system
- Define custom border utilities in @layer utilities for consistent theming
- Add border-default (1px gray), border-active (2px primary), border-hover (40% primary), and border-dragging (60% primary) classes
- Update all UI components (Input, Select, TextArea, Button, Dialog, Dropdown) to use unified border classes
- Replace hardcoded border colors (gray-200/300/600/700) with theme-responsive border-border-default
- Update provider cards, MCP components, settings, and forms with new border system
- Remove dark mode border overrides to simplify CSS and improve maintainability
- Ensure all borders automatically adapt to light/dark themes via CSS variables
2025-10-20 23:44:06 +08:00
|
|
|
|
<div className="mt-3 pt-3 border-t border-border-default ">
|
2025-10-15 09:15:25 +08:00
|
|
|
|
<div className="flex items-center justify-between gap-2 text-xs">
|
|
|
|
|
|
<div className="flex items-center gap-2 text-red-500 dark:text-red-400">
|
|
|
|
|
|
<AlertCircle size={14} />
|
2025-10-19 11:55:46 +08:00
|
|
|
|
<span>{usage.error || t("usage.queryFailed")}</span>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 刷新按钮 */}
|
|
|
|
|
|
<button
|
2025-10-17 19:18:10 +08:00
|
|
|
|
onClick={() => refetch()}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
disabled={loading}
|
|
|
|
|
|
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50 flex-shrink-0"
|
2025-10-19 11:55:46 +08:00
|
|
|
|
title={t("usage.refreshUsage")}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
>
|
|
|
|
|
|
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const usageDataList = usage.data || [];
|
|
|
|
|
|
|
|
|
|
|
|
// 无数据时不显示
|
|
|
|
|
|
if (usageDataList.length === 0) return null;
|
|
|
|
|
|
|
2025-11-05 22:46:30 +08:00
|
|
|
|
// 内联模式:仅显示第一个套餐的核心数据(分上下两行)
|
|
|
|
|
|
if (inline) {
|
|
|
|
|
|
const firstUsage = usageDataList[0];
|
|
|
|
|
|
const isExpired = firstUsage.isValid === false;
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="flex flex-col gap-1 text-xs flex-shrink-0">
|
|
|
|
|
|
{/* 第一行:刷新时间 + 刷新按钮 */}
|
|
|
|
|
|
<div className="flex items-center gap-2 justify-end">
|
|
|
|
|
|
{/* 上次查询时间 */}
|
|
|
|
|
|
{lastQueriedAt && (
|
|
|
|
|
|
<span className="text-[10px] text-gray-400 dark:text-gray-500 flex items-center gap-1">
|
|
|
|
|
|
<Clock size={10} />
|
|
|
|
|
|
{formatRelativeTime(lastQueriedAt, now, t)}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{/* 刷新按钮 */}
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={() => refetch()}
|
|
|
|
|
|
disabled={loading}
|
|
|
|
|
|
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50 flex-shrink-0"
|
|
|
|
|
|
title={t("usage.refreshUsage")}
|
|
|
|
|
|
>
|
|
|
|
|
|
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 第二行:已用 + 剩余 + 单位 */}
|
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
|
{/* 已用 */}
|
|
|
|
|
|
{firstUsage.used !== undefined && (
|
|
|
|
|
|
<div className="flex items-center gap-0.5">
|
|
|
|
|
|
<span className="text-gray-500 dark:text-gray-400">
|
|
|
|
|
|
{t("usage.used")}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<span className="tabular-nums text-gray-600 dark:text-gray-400 font-medium">
|
|
|
|
|
|
{firstUsage.used.toFixed(2)}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{/* 剩余 */}
|
|
|
|
|
|
{firstUsage.remaining !== undefined && (
|
|
|
|
|
|
<div className="flex items-center gap-0.5">
|
|
|
|
|
|
<span className="text-gray-500 dark:text-gray-400">
|
|
|
|
|
|
{t("usage.remaining")}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<span
|
|
|
|
|
|
className={`font-semibold tabular-nums ${
|
|
|
|
|
|
isExpired
|
|
|
|
|
|
? "text-red-500 dark:text-red-400"
|
|
|
|
|
|
: firstUsage.remaining <
|
|
|
|
|
|
(firstUsage.total || firstUsage.remaining) * 0.1
|
|
|
|
|
|
? "text-orange-500 dark:text-orange-400"
|
|
|
|
|
|
: "text-green-600 dark:text-green-400"
|
|
|
|
|
|
}`}
|
|
|
|
|
|
>
|
|
|
|
|
|
{firstUsage.remaining.toFixed(2)}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{/* 单位 */}
|
|
|
|
|
|
{firstUsage.unit && (
|
|
|
|
|
|
<span className="text-gray-500 dark:text-gray-400">
|
|
|
|
|
|
{firstUsage.unit}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-15 09:15:25 +08:00
|
|
|
|
return (
|
refactor: implement unified border design system
- Define custom border utilities in @layer utilities for consistent theming
- Add border-default (1px gray), border-active (2px primary), border-hover (40% primary), and border-dragging (60% primary) classes
- Update all UI components (Input, Select, TextArea, Button, Dialog, Dropdown) to use unified border classes
- Replace hardcoded border colors (gray-200/300/600/700) with theme-responsive border-border-default
- Update provider cards, MCP components, settings, and forms with new border system
- Remove dark mode border overrides to simplify CSS and improve maintainability
- Ensure all borders automatically adapt to light/dark themes via CSS variables
2025-10-20 23:44:06 +08:00
|
|
|
|
<div className="mt-3 pt-3 border-t border-border-default ">
|
2025-11-05 15:48:19 +08:00
|
|
|
|
{/* 标题行:包含刷新按钮和自动查询时间 */}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
<div className="flex items-center justify-between mb-2">
|
|
|
|
|
|
<span className="text-xs text-gray-500 dark:text-gray-400 font-medium">
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{t("usage.planUsage")}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</span>
|
2025-11-05 15:48:19 +08:00
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
|
{/* 自动查询时间提示 */}
|
|
|
|
|
|
{lastQueriedAt && (
|
|
|
|
|
|
<span className="text-[10px] text-gray-400 dark:text-gray-500 flex items-center gap-1">
|
|
|
|
|
|
<Clock size={10} />
|
2025-11-05 21:40:06 +08:00
|
|
|
|
{formatRelativeTime(lastQueriedAt, now, t)}
|
2025-11-05 15:48:19 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={() => refetch()}
|
2025-11-05 21:40:06 +08:00
|
|
|
|
disabled={loading}
|
2025-11-05 15:48:19 +08:00
|
|
|
|
className="p-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors disabled:opacity-50"
|
|
|
|
|
|
title={t("usage.refreshUsage")}
|
|
|
|
|
|
>
|
2025-11-05 22:46:30 +08:00
|
|
|
|
<RefreshCw size={12} className={loading ? "animate-spin" : ""} />
|
2025-11-05 15:48:19 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 套餐列表 */}
|
|
|
|
|
|
<div className="flex flex-col gap-3">
|
|
|
|
|
|
{usageDataList.map((usageData, index) => (
|
|
|
|
|
|
<UsagePlanItem key={index} data={usageData} />
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 单个套餐数据展示组件
|
|
|
|
|
|
const UsagePlanItem: React.FC<{ data: UsageData }> = ({ data }) => {
|
2025-10-19 11:55:46 +08:00
|
|
|
|
const { t } = useTranslation();
|
2025-10-16 12:13:51 +08:00
|
|
|
|
const {
|
|
|
|
|
|
planName,
|
|
|
|
|
|
extra,
|
|
|
|
|
|
isValid,
|
|
|
|
|
|
invalidMessage,
|
|
|
|
|
|
total,
|
|
|
|
|
|
used,
|
|
|
|
|
|
remaining,
|
|
|
|
|
|
unit,
|
|
|
|
|
|
} = data;
|
2025-10-15 09:15:25 +08:00
|
|
|
|
|
|
|
|
|
|
// 判断套餐是否失效(isValid 为 false 或未定义时视为有效)
|
|
|
|
|
|
const isExpired = isValid === false;
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
|
{/* 标题部分:25% */}
|
2025-10-16 12:13:51 +08:00
|
|
|
|
<div
|
|
|
|
|
|
className="text-xs text-gray-500 dark:text-gray-400 min-w-0"
|
|
|
|
|
|
style={{ width: "25%" }}
|
|
|
|
|
|
>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
{planName ? (
|
|
|
|
|
|
<span
|
|
|
|
|
|
className={`font-medium truncate block ${isExpired ? "text-red-500 dark:text-red-400" : ""}`}
|
|
|
|
|
|
title={planName}
|
|
|
|
|
|
>
|
|
|
|
|
|
💰 {planName}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<span className="opacity-50">—</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 扩展字段:30% */}
|
2025-10-16 12:13:51 +08:00
|
|
|
|
<div
|
|
|
|
|
|
className="text-xs text-gray-500 dark:text-gray-400 min-w-0 flex items-center gap-2"
|
|
|
|
|
|
style={{ width: "30%" }}
|
|
|
|
|
|
>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
{extra && (
|
|
|
|
|
|
<span
|
|
|
|
|
|
className={`truncate ${isExpired ? "text-red-500 dark:text-red-400" : ""}`}
|
|
|
|
|
|
title={extra}
|
|
|
|
|
|
>
|
|
|
|
|
|
{extra}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{isExpired && (
|
|
|
|
|
|
<span className="text-red-500 dark:text-red-400 font-medium text-[10px] px-1.5 py-0.5 bg-red-50 dark:bg-red-900/20 rounded flex-shrink-0">
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{invalidMessage || t("usage.invalid")}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 用量信息:45% */}
|
2025-10-16 12:13:51 +08:00
|
|
|
|
<div
|
|
|
|
|
|
className="flex items-center justify-end gap-2 text-xs flex-shrink-0"
|
|
|
|
|
|
style={{ width: "45%" }}
|
|
|
|
|
|
>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
{/* 总额度 */}
|
|
|
|
|
|
{total !== undefined && (
|
|
|
|
|
|
<>
|
2025-10-24 13:02:35 +08:00
|
|
|
|
<span className="text-gray-500 dark:text-gray-400">
|
|
|
|
|
|
{t("usage.total")}
|
|
|
|
|
|
</span>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
<span className="tabular-nums text-gray-600 dark:text-gray-400">
|
|
|
|
|
|
{total === -1 ? "∞" : total.toFixed(2)}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<span className="text-gray-400 dark:text-gray-600">|</span>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{/* 已用额度 */}
|
|
|
|
|
|
{used !== undefined && (
|
|
|
|
|
|
<>
|
2025-10-24 13:02:35 +08:00
|
|
|
|
<span className="text-gray-500 dark:text-gray-400">
|
|
|
|
|
|
{t("usage.used")}
|
|
|
|
|
|
</span>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
<span className="tabular-nums text-gray-600 dark:text-gray-400">
|
|
|
|
|
|
{used.toFixed(2)}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<span className="text-gray-400 dark:text-gray-600">|</span>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{/* 剩余额度 - 突出显示 */}
|
|
|
|
|
|
{remaining !== undefined && (
|
|
|
|
|
|
<>
|
2025-10-24 13:02:35 +08:00
|
|
|
|
<span className="text-gray-500 dark:text-gray-400">
|
|
|
|
|
|
{t("usage.remaining")}
|
|
|
|
|
|
</span>
|
2025-10-15 09:15:25 +08:00
|
|
|
|
<span
|
|
|
|
|
|
className={`font-semibold tabular-nums ${
|
|
|
|
|
|
isExpired
|
|
|
|
|
|
? "text-red-500 dark:text-red-400"
|
|
|
|
|
|
: remaining < (total || remaining) * 0.1
|
|
|
|
|
|
? "text-orange-500 dark:text-orange-400"
|
|
|
|
|
|
: "text-green-600 dark:text-green-400"
|
|
|
|
|
|
}`}
|
|
|
|
|
|
>
|
|
|
|
|
|
{remaining.toFixed(2)}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2025-10-16 12:13:51 +08:00
|
|
|
|
{unit && (
|
|
|
|
|
|
<span className="text-gray-500 dark:text-gray-400">{unit}</span>
|
|
|
|
|
|
)}
|
2025-10-15 09:15:25 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-11-05 15:48:19 +08:00
|
|
|
|
// 格式化相对时间
|
|
|
|
|
|
function formatRelativeTime(
|
|
|
|
|
|
timestamp: number,
|
2025-11-05 21:40:06 +08:00
|
|
|
|
now: number,
|
2025-11-05 15:48:19 +08:00
|
|
|
|
t: (key: string, options?: { count?: number }) => string
|
|
|
|
|
|
): string {
|
|
|
|
|
|
const diff = Math.floor((now - timestamp) / 1000); // 秒
|
|
|
|
|
|
|
|
|
|
|
|
if (diff < 60) {
|
|
|
|
|
|
return t("usage.justNow");
|
|
|
|
|
|
} else if (diff < 3600) {
|
|
|
|
|
|
const minutes = Math.floor(diff / 60);
|
|
|
|
|
|
return t("usage.minutesAgo", { count: minutes });
|
|
|
|
|
|
} else if (diff < 86400) {
|
|
|
|
|
|
const hours = Math.floor(diff / 3600);
|
|
|
|
|
|
return t("usage.hoursAgo", { count: hours });
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const days = Math.floor(diff / 86400);
|
|
|
|
|
|
return t("usage.daysAgo", { count: days });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-15 09:15:25 +08:00
|
|
|
|
export default UsageFooter;
|