"use client"; import React, { useState, useEffect } from "react"; import { useTranslation } from "@/shared/lib/useTranslation"; import DashboardDemoContent from "@/shared/components/DashboardDemoContent"; import GroupList, { ResourceGroup } from "./GroupList"; import GroupServiceTable, { GroupService } from "./GroupServiceTable"; import { AddServerModal } from "./AddServerModal"; import { DeleteServerModal } from "./DeleteServerModal"; import { Divider, Select, SelectItem } from "@nextui-org/react"; import { useResourceGroups } from "@/hooks/useResourceGroups"; import { AddMcpServerModal } from "./AddMcpServerModal"; const ResourceGroupManagement: React.FC = () => { const { t } = useTranslation(); // 使用自定义 Hook 管理资源组 const { groups, simpleResourceGroups, loadSimpleResourceGroups, groupServices, serviceTablePagination, serviceTableParams, loading, servicesLoading, hasMore, shouldScrollToTop, setShouldScrollToTop, loadGroupServices, loadMoreGroups, handleServicePageChange, handleAddServiceToGroup, handleCreateGroup: createGroup, handleUpdateGroup: updateGroup, handleDeleteGroup: deleteGroup, handleRemoveService: removeService, } = useResourceGroups(); // 当前选中的资源组 const [selectedGroup, setSelectedGroup] = useState(null); // 删除组确认模态框 const [isDeleteGroupModalOpen, setIsDeleteGroupModalOpen] = useState(false); const [groupToDelete, setGroupToDelete] = useState(null); const [transferToGroupId, setTransferToGroupId] = useState(null); const [simpleResourceGroupsLoading, setSimpleResourceGroupsLoading] = useState(false); // 删除服务确认模态框 const [isDeleteServiceModalOpen, setIsDeleteServiceModalOpen] = useState(false); const [serviceToDelete, setServiceToDelete] = useState(null); // 创建/编辑组模态框 const [isGroupModalOpen, setIsGroupModalOpen] = useState(false); const [editingGroup, setEditingGroup] = useState(null); // 添加 MCP 服务模态框 const [isAddMcpServerModalOpen, setIsAddMcpServerModalOpen] = useState(false); // 刷新触发器,用于触发 GroupServiceTable 刷新 const [refreshTrigger, setRefreshTrigger] = useState(0); const [newGroupId, setNewGroupId] = useState(null); /** * 保存 MCP 服务 * @param serverIds 服务ID列表 * @returns 是否保存成功 */ const handleSaveMcpServer = async (serverIds: string[]) => { if (!selectedGroup) return; const success = await handleAddServiceToGroup(selectedGroup.id, serverIds); if (success) { setIsAddMcpServerModalOpen(false); await loadGroupServices(selectedGroup.id, 1, serviceTableParams.current.pageSize, serviceTableParams.current.keyword); } }; // 初始化选中第一个组,或在滚动到顶部时选中第一个 useEffect(() => { if (groups.length > 0 && (shouldScrollToTop || !selectedGroup)) { const index = shouldScrollToTop && newGroupId ? groups.findIndex((group) => group.id === newGroupId) : 0; setSelectedGroup(groups[index]); loadGroupServices(groups[index].id); } }, [groups, selectedGroup, loadGroupServices, shouldScrollToTop]); // 选择组 const handleSelectGroup = (group: ResourceGroup) => { setSelectedGroup(group); loadGroupServices(group.id); }; // 创建新组 const handleCreateGroup = () => { setEditingGroup(null); setIsGroupModalOpen(true); }; // 保存组(创建或编辑) const handleSaveGroup = async (name: string, isDefault: boolean) => { let success = false; if (editingGroup) { // 编辑现有组 success = await updateGroup(editingGroup.id, name, isDefault); if (success && selectedGroup?.id === editingGroup.id) { setSelectedGroup({ ...selectedGroup, name, isDefault }); // 触发 GroupServiceTable 刷新 setRefreshTrigger(prev => prev + 1); } } else { // 创建新组 const newGroupId = await createGroup(name, isDefault); success = !!newGroupId; if (newGroupId) { setNewGroupId(newGroupId); } } if (success) { setIsGroupModalOpen(false); setEditingGroup(null); } }; // 编辑组 const handleEditGroup = (groupId: string) => { const group = groups.find((g) => g.id === groupId); if (group) { setEditingGroup(group); setIsGroupModalOpen(true); } }; // 删除组 const handleDeleteGroup = async (groupId: string) => { const group = groups.find((g) => g.id === groupId); if (group) { setSimpleResourceGroupsLoading(true); const simpleResourceGroups = await loadSimpleResourceGroups(); setTransferToGroupId(simpleResourceGroups.filter((group) => group.id !== groupId)[0].id); setSimpleResourceGroupsLoading(false); setGroupToDelete(group); setIsDeleteGroupModalOpen(true); } }; // 确认删除组 const handleConfirmDeleteGroup = async () => { if (groupToDelete && transferToGroupId) { // 在删除前找到要选中的目标组 let targetGroup: ResourceGroup | null = null; if (selectedGroup?.id === groupToDelete.id) { const currentIndex = groups.findIndex((g) => g.id === groupToDelete.id); if (currentIndex !== -1) { // 优先选择上一个,如果没有上一个则选择下一个 if (currentIndex > 0) { targetGroup = groups[currentIndex - 1]; } else if (currentIndex < groups.length - 1) { targetGroup = groups[currentIndex + 1]; } // 如果既没有上一个也没有下一个(只有一个元素),targetGroup 保持为 null } } const success = await deleteGroup(groupToDelete.id, transferToGroupId); if (success) { // 等待列表刷新后选中目标组 setTimeout(() => { if (groups.length === 0) { // 删除后没有任何资源组了 setSelectedGroup(null); } else if (targetGroup) { // 在新列表中找到目标组(因为列表已经刷新了) const newTarget = groups.find((g) => g.id === targetGroup.id); if (newTarget) { setSelectedGroup(newTarget); loadGroupServices(newTarget.id); } else { // 如果目标组不存在了,选择第一个 setSelectedGroup(groups[0]); loadGroupServices(groups[0].id); } } else if (selectedGroup?.id === groupToDelete.id) { // 如果删除的是当前选中的组,且没有找到目标组(理论上不应该发生) setSelectedGroup(groups[0]); loadGroupServices(groups[0].id); } // 如果删除的不是当前选中的组,保持原有选中状态 }, 200); setIsDeleteGroupModalOpen(false); setGroupToDelete(null); setTransferToGroupId(null); } } }; // 添加服务到组 const handleAddService = () => { setIsAddMcpServerModalOpen(true); }; // 从组中移除服务 const handleRemoveService = (serviceId: string) => { const service = groupServices.find((s) => s.id === serviceId); if (service) { setServiceToDelete(service); setIsDeleteServiceModalOpen(true); } }; // 确认删除服务 const handleConfirmDeleteService = async () => { if (serviceToDelete && selectedGroup) { const success = await removeService(selectedGroup.id, serviceToDelete.id); if (success) { setIsDeleteServiceModalOpen(false); setServiceToDelete(null); await loadGroupServices(selectedGroup.id, 1, serviceTableParams.current.pageSize, serviceTableParams.current.keyword); } } }; return (
{/* 左侧组列表 */} setShouldScrollToTop(false)} /> {/* 右侧服务列表 */} {selectedGroup ? ( handleServicePageChange(selectedGroup.id, page)} onAddService={handleAddService} onRemoveService={handleRemoveService} onEditGroup={handleEditGroup} onDeleteGroup={handleDeleteGroup} refreshTrigger={refreshTrigger} /> ) : (
{t("Please select a group to view services")}
)}
{/* 删除资源组模态框 */} { setIsDeleteGroupModalOpen(false); setGroupToDelete(null); setTransferToGroupId(null); }} onConfirm={handleConfirmDeleteGroup} title={t("Confirm Remove")} description={t( 'Are you sure you want to remove this "{{name}}" Resource Group?', { name: groupToDelete?.name || "", } )} alertDescription={t("The bound users have been transferred to the following resource group")} customAlertContent={
} /> {/* 删除服务模态框 */} { setIsDeleteServiceModalOpen(false); setServiceToDelete(null); }} onConfirm={handleConfirmDeleteService} title={t("Confirm Remove")} description={t( 'Are you sure you want to remove this "{{name}}" mcp server from "{{groupName}}" resource group?', { name: serviceToDelete?.name || "", groupName: selectedGroup?.name || "", } )} alertDescription={t("Users associated with this resource group will not be able to call the removed MCP server")} /> {/* 创建/编辑组模态框 */} { setIsGroupModalOpen(false); setEditingGroup(null); }} name={editingGroup?.name || ""} id={editingGroup?.id || ""} isDefault={editingGroup?.isDefault || false} onSaveServer={handleSaveGroup} type={editingGroup ? "edit" : "add"} /> { setIsAddMcpServerModalOpen(false); }} onSaveMcpServer={handleSaveMcpServer} />
); }; export default ResourceGroupManagement;