import { Button, Modal, ModalBody, ModalContent, ModalFooter, ModalHeader, Table, TableHeader, TableColumn, TableBody, TableRow, TableCell, Spinner, } from "@nextui-org/react"; import { useState, useEffect, useCallback } from "react"; import { useTranslation } from "@/shared/lib/useTranslation"; import { fetchUnboundResourceGroups } from "@/services/mcpService"; interface McpAddGroupModalProps { isOpen: boolean; onClose: () => void; id: string; onSaveMcpServer: (serverIds: string[]) => Promise; } interface ResourceGroup { id: string; name: string; } export const McpAddGroupModal: React.FC = ({ isOpen, onClose, id, onSaveMcpServer, }) => { const { t } = useTranslation(); const [isLoading, setIsLoading] = useState(false); const [loading, setLoading] = useState(false); const [resourceGroups, setResourceGroups] = useState([]); const [selectedKeys, setSelectedKeys] = useState>(new Set()); // 加载服务列表 const loadServices = useCallback(async () => { setLoading(true); try { const response = await fetchUnboundResourceGroups(id); setResourceGroups(response); } catch (error) { console.error("Failed to load resource groups:", error); } finally { setLoading(false); } }, []); // 当模态框打开时加载数据 useEffect(() => { if (isOpen) { setSelectedKeys(new Set()); loadServices(); } }, [isOpen, loadServices]); // 处理保存 const handleSave = async () => { const serverIds = typeof selectedKeys === 'string' && selectedKeys === 'all' ? resourceGroups.map((group) => group.id) : Array.from(selectedKeys); if (serverIds.length === 0) { return; } setIsLoading(true); try { await onSaveMcpServer(serverIds); onClose(); } finally { setIsLoading(false); } }; return ( {() => ( <> {t("Add to Groups")} setSelectedKeys(keys as Set)} removeWrapper classNames={{ wrapper: "min-h-[400px]", }} className="[&_td:first-child]:w-[30px] [&_th:first-child]:w-[30px]" > {t("Group Name")} } emptyContent={t("No resource groups available")} > {(resourceGroup) => ( {resourceGroup.name} )}
)}
); };