import { Button } from '@heroui/button'; import { useLocalStorage } from '@uidotdev/usehooks'; import clsx from 'clsx'; import { useEffect, useState } from 'react'; import { IoClose } from 'react-icons/io5'; import { TbSquareRoundedChevronLeftFilled } from 'react-icons/tb'; import key from '@/const/key'; import oneBotHttpApi from '@/const/ob_api'; import type { OneBotHttpApiPath } from '@/const/ob_api'; import OneBotApiDebug from '@/components/onebot/api/debug'; import OneBotApiNavList from '@/components/onebot/api/nav_list'; export default function HttpDebug () { const [activeApi, setActiveApi] = useState('/set_qq_profile'); const [openApis, setOpenApis] = useState(['/set_qq_profile']); const [openSideBar, setOpenSideBar] = useState(true); const [backgroundImage] = useLocalStorage(key.backgroundImage, ''); const hasBackground = !!backgroundImage; const [adapterName, setAdapterName] = useState(''); // Auto-collapse sidebar on mobile initial load useEffect(() => { if (window.innerWidth < 768) { setOpenSideBar(false); } }, []); // Initialize Debug Adapter useEffect(() => { let currentAdapterName = ''; const initAdapter = async () => { try { const response = await fetch('/api/Debug/create', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` } }); const data = await response.json(); if (data.code === 0) { currentAdapterName = data.data.adapterName; setAdapterName(currentAdapterName); } } catch (error) { console.error('Failed to create debug adapter:', error); } }; initAdapter(); return () => { // 不再主动关闭 adapter,由后端自动管理活跃状态 }; }, []); const handleSelectApi = (api: OneBotHttpApiPath) => { if (!openApis.includes(api)) { setOpenApis([...openApis, api]); } setActiveApi(api); if (window.innerWidth < 768) { setOpenSideBar(false); } }; const handleCloseTab = (e: React.MouseEvent, apiToRemove: OneBotHttpApiPath) => { e.stopPropagation(); const newOpenApis = openApis.filter((api) => api !== apiToRemove); setOpenApis(newOpenApis); if (activeApi === apiToRemove) { if (newOpenApis.length > 0) { // Switch to the last opened tab or the previous one? // Usually the one to the right or left. Let's pick the last one for simplicity or neighbor. // Finding index of removed api to pick neighbor is better UX, but last one is acceptable. setActiveApi(newOpenApis[newOpenApis.length - 1]); } else { setActiveApi(null); } } }; return ( <> HTTP调试 - NapCat WebUI
{/* Unifed Header */}

接口调试

{/* Tab Bar */}
{openApis.map((api) => { const isActive = api === activeApi; const item = oneBotHttpApi[api]; return (
setActiveApi(api)} className={clsx( 'group flex items-center gap-2 px-4 h-9 cursor-pointer border-r border-white/5 select-none transition-all min-w-[120px] max-w-[200px]', isActive ? (hasBackground ? 'bg-white/10 text-white' : 'bg-white/40 dark:bg-white/5 text-primary font-medium') : 'opacity-50 hover:opacity-100 hover:bg-white/5' )} > POST {item?.description || api}
handleCloseTab(e, api)} >
); })}
{/* Content Panels */}
{activeApi === null && (
选择一个接口开始调试
)} {openApis.map((api) => (
))}
); }