import React, { useState, useEffect } from "react"; import { Card, CardBody, CardHeader, Button, Divider, Spinner, } from "@nextui-org/react"; import { Plus, InboxIcon } from "lucide-react"; import toast from "react-hot-toast"; import { APIKey } from "@/shared/types/api"; import { fetchAPI } from "@/shared/rpc/common-function"; import AuthKeysList from "./AuthKeysList"; import AuthKeyDetails from "./AuthKeyDetails"; import { APIKeyModal } from "./APIKeyModal"; import { DeleteConfirmModal } from "@/shared/components/modal/DeleteConfirmModal"; import DashboardDemoContent from "@/shared/components/DashboardDemoContent"; import { useTranslation } from "@/shared/lib/useTranslation"; const OverviewPage: React.FC = () => { const { t } = useTranslation(); // Auth Keys related states const [isKeyModalOpen, setIsKeyModalOpen] = useState(false); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [selectedApiKey, setSelectedApiKey] = useState(null); const [generatedKey, setGeneratedKey] = useState(); const [isLoading, setIsLoading] = useState(false); const [apiKeys, setApiKeys] = useState(null); const [editingApiKey, setEditingApiKey] = useState(null); // fetch auth keys const fetchApiKeys = async () => { setIsLoading(true); try { const response = await fetchAPI("/api/apikey/list"); if (!response.success) { return; } setApiKeys(response.data || []); // if there are auth keys, select the first one as default if (response.data && response.data.length > 0) { setSelectedApiKey(response.data[0]); } } catch (error) { console.error("Error fetching auth keys:", error); } finally { setIsLoading(false); } }; useEffect(() => { fetchApiKeys(); if (sessionStorage.getItem('create_new_authorization')) { handleNewKey(); sessionStorage.removeItem('create_new_authorization'); } }, []); // create new auth key const handleNewKey = () => { openEditModal(); }; // edit auth key const handleEditKey = (key: APIKey) => { openEditModal(key); }; // delete auth key const handleDeleteKey = async () => { if (!selectedApiKey) { return; } const apikey_id = selectedApiKey.apikey_id; const response = await fetchAPI("/api/apikey/info", { method: "DELETE", body: { apikey_id, } as unknown as BodyInit, }); if (!response.success) { console.error("Error deleting key:", response.error_message); toast.error(t("Failed to delete")); return; } toast.success(t("Deleted successfully")); setIsDeleteModalOpen(false); const updatedKeys = apiKeys?.filter((key) => key.apikey_id !== apikey_id) || []; setApiKeys(updatedKeys); if (updatedKeys.length > 0) { setSelectedApiKey(updatedKeys[0]); } else { setSelectedApiKey(null); } }; // select auth key const handleSelectKey = (key: APIKey) => { setSelectedApiKey(key); }; // open edit auth key modal const openEditModal = (keyData?: APIKey) => { setEditingApiKey(keyData || null); setGeneratedKey(undefined); setIsKeyModalOpen(true); }; // open delete auth key modal const openDeleteModal = (keyData: APIKey) => { setSelectedApiKey(keyData); setIsDeleteModalOpen(true); }; // create auth key success callback const handleCreateKeySuccess = (keyData: APIKey) => { const newKeys = [...(apiKeys || []), keyData]; setApiKeys(newKeys); setGeneratedKey(keyData.apikey); setSelectedApiKey(keyData); }; // edit key success callback const handleEditKeySuccess = ({ name }: APIKey) => { if (!editingApiKey) return; const id = editingApiKey.apikey_id; const updatedKeys = apiKeys?.map((key) => key.apikey_id === id ? { ...key, name, } : key ) || []; setApiKeys(updatedKeys); setSelectedApiKey({ ...editingApiKey, name }); setEditingApiKey(null); }; return ( {/* Main Content */} {isLoading ? ( <>
) : !apiKeys || apiKeys.length === 0 ? ( <>

{t("Auth Key")}

) : (
)} {/* modal */} { setIsKeyModalOpen(false); if (!apiKeys?.length) { setSelectedApiKey(null); } setGeneratedKey(undefined); setEditingApiKey(null); }} onSubmitSuccess={ editingApiKey && apiKeys?.some((k) => k.apikey_id === editingApiKey.apikey_id) ? handleEditKeySuccess : handleCreateKeySuccess } initialName={editingApiKey?.name} generatedKey={generatedKey} mode={ editingApiKey && apiKeys?.some((k) => k.apikey_id === editingApiKey.apikey_id) ? "edit" : "create" } id={editingApiKey?.apikey_id} /> { setIsDeleteModalOpen(false); }} onConfirm={handleDeleteKey} title={t("Delete Auth Key")} description={t('Are you sure you want to delete the "{{keyName}}" Auth key?', { keyName: selectedApiKey?.name || "", })} />
); }; export default OverviewPage;