import React, { useState } from "react"; import { BarChart } from "@tremor/react"; import KeyInfoView from "./key_info_view"; import { keyInfoV1Call } from "./networking"; import { transformKeyInfo } from "../components/key_team_helpers/transform_key_info"; import { DataTable } from "./view_logs/table"; import { Tooltip } from "antd"; import { Button } from "@tremor/react"; interface TopKeyViewProps { topKeys: any[]; accessToken: string | null; userID: string | null; userRole: string | null; teams: any[] | null; } const TopKeyView: React.FC = ({ topKeys, accessToken, userID, userRole, teams }) => { const [isModalOpen, setIsModalOpen] = useState(false); const [selectedKey, setSelectedKey] = useState(null); const [keyData, setKeyData] = useState(undefined); const [viewMode, setViewMode] = useState<'chart' | 'table'>('table'); const handleKeyClick = async (item: any) => { if (!accessToken) return; try { const keyInfo = await keyInfoV1Call(accessToken, item.api_key); const transformedKeyData = transformKeyInfo(keyInfo); setKeyData(transformedKeyData); setSelectedKey(item.api_key); setIsModalOpen(true); // Open modal when key is clicked } catch (error) { console.error("Error fetching key info:", error); } }; const handleClose = () => { setIsModalOpen(false); setSelectedKey(null); setKeyData(undefined); }; // Handle clicking outside the modal const handleOutsideClick = (e: React.MouseEvent) => { if (e.target === e.currentTarget) { handleClose(); } }; // Handle escape key React.useEffect(() => { const handleEscapeKey = (e: KeyboardEvent) => { if (e.key === 'Escape' && isModalOpen) { handleClose(); } }; document.addEventListener('keydown', handleEscapeKey); return () => document.removeEventListener('keydown', handleEscapeKey); }, [isModalOpen]); // Define columns for the table view const columns = [ { header: "Key ID", accessorKey: "api_key", cell: (info: any) => (
), }, { header: "Key Alias", accessorKey: "key_alias", cell: (info: any) => info.getValue() || "-", }, { header: "Spend (USD)", accessorKey: "spend", cell: (info: any) => `$${Number(info.getValue()).toFixed(2)}`, }, ]; return ( <>
{viewMode === 'chart' ? (
value ? `$${value.toFixed(2)}` : "No Key Alias"} onValueChange={(item) => handleKeyClick(item)} showTooltip={true} customTooltip={(props) => { const item = props.payload?.[0]?.payload; return (
Key: {item?.api_key?.slice(0, 10)}...
Spend: ${item?.spend.toFixed(2)}
); }} />
) : (
<>} getRowCanExpand={() => false} isLoading={false} />
)} {isModalOpen && selectedKey && keyData && ( console.log('Rendering modal with:', { isModalOpen, selectedKey, keyData }),
{/* Close button */} {/* Content */}
)} ); }; export default TopKeyView;