import React, { useState, useEffect, useCallback } from "react"; import { useTranslation } from "react-i18next"; import { Card, CardBody, CardHeader, Button, Divider, Spinner, } from "@nextui-org/react"; import { Edit2, Trash2, Copy, RefreshCw } from "lucide-react"; import { BarChart, Bar, XAxis, YAxis, CartesianGrid, ResponsiveContainer, Tooltip as RechartsTooltip, } from "recharts"; import toast from "react-hot-toast"; import { fetchAPI } from "@/shared/rpc/common-function"; import { APIKey } from "@/shared/types/api"; import { copyToClipboard } from "@/shared/utils/clipboard"; interface AuthKeyDetailsProps { selectedApiKey: APIKey | null; onEditKey: (key: APIKey) => void; onDeleteKey: (key: APIKey) => void; } interface ApiAnalyticsData { stats_day: string; call_tool_count: number; amount: number; } // Custom cursor to highlight the hovered bar with a lighter and narrower overlay const CustomCursor: React.FC<{ x?: number; y?: number; width?: number; height?: number; }> = ({ x = 0, y = 0, height = 0, width = 0 }) => { const barVisualWidth = 20; // Use the bar's visual width directly so the highlight precisely matches the bar const overlayWidth = barVisualWidth; // Center the overlay within the default cursor width const offsetX = x + (width - overlayWidth) / 2; return ( ); }; const AuthKeyDetails: React.FC = ({ selectedApiKey, onEditKey, onDeleteKey, }) => { const { t } = useTranslation(); const [analyticsData, setAnalyticsData] = useState< { name: string; value: number }[] >([]); const [isLoading, setIsLoading] = useState(false); const fetchAnalyticsData = useCallback(async () => { if (!selectedApiKey) return; try { setIsLoading(true); const response = await fetchAPI( `/api/stats/key_call_tool?apikey_id=${selectedApiKey.apikey_id}`, { method: "GET", } ); if (response.success && response.data?.days) { const formattedData = response.data.days.map( (day: ApiAnalyticsData) => ({ name: new Date(day.stats_day).toISOString().split("T")[0], value: day.amount, count: day.call_tool_count, }) ); setAnalyticsData(formattedData); } else { console.error("Analytics data fetch error:", response.error_message); } } catch (error) { console.error("Analytics data fetch error:", error); } finally { setIsLoading(false); } }, [selectedApiKey, t]); useEffect(() => { if (!selectedApiKey?.apikey_id) return; fetchAnalyticsData(); }, [selectedApiKey?.apikey_id]); const handleCopy = async (text: string) => { const result = await copyToClipboard(text); if (result.success) { toast.success(t("Copied to clipboard")); } else { toast.error(t("Copy failed, please copy manually")); } }; const getEncryptedKey = (key: string) => { return key.slice(0, 2) + "*".repeat(key.length - 7) + key.slice(-5); }; if (!selectedApiKey) { return <>>; } return ( {/* Selected Key Details */} {selectedApiKey.name} onEditKey(selectedApiKey)} > onDeleteKey(selectedApiKey)} > {/* MCP Config Section */} {t("Auth Key")} {getEncryptedKey(selectedApiKey.apikey)} handleCopy(selectedApiKey.apikey)} > {/* Analytics Section */} {t("Analytics")} fetchAnalyticsData()} > {analyticsData.length > 0 ? ( } formatter={(value: number) => [ value, t("Total Spending"), ]} contentStyle={{ backgroundColor: "#fff", border: "1px solid #e5e7eb", borderRadius: "6px", boxShadow: "0 4px 6px -1px rgba(0, 0, 0, 0.1)", fontSize: "12px", lineHeight: "1.5", padding: "5px", }} /> ) : ( {isLoading ? ( ) : ( {t("No analytics data available")} )} )} ); }; export default AuthKeyDetails;
{t("No analytics data available")}