import { Button } from '@heroui/button'; import { Input } from '@heroui/input'; import { Select, SelectItem } from '@heroui/select'; import { Tab, Tabs } from '@heroui/tabs'; import { useEffect, useMemo, useState } from 'react'; import toast from 'react-hot-toast'; import { IoMdRefresh, IoMdSearch } from 'react-icons/io'; import clsx from 'clsx'; import { useRequest } from 'ahooks'; import { EventSourcePolyfill } from 'event-source-polyfill'; import PageLoading from '@/components/page_loading'; import PluginStoreCard from '@/components/display_card/plugin_store_card'; import PluginManager from '@/controllers/plugin_manager'; import WebUIManager from '@/controllers/webui_manager'; import { PluginStoreItem } from '@/types/plugin-store'; import useDialog from '@/hooks/use-dialog'; import key from '@/const/key'; interface EmptySectionProps { isEmpty: boolean; } const EmptySection: React.FC = ({ isEmpty }) => { return (
暂时没有可用的插件
); }; export default function PluginStorePage () { const [plugins, setPlugins] = useState([]); const [loading, setLoading] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const [activeTab, setActiveTab] = useState('all'); const dialog = useDialog(); // 获取镜像列表 const { data: mirrorsData } = useRequest(WebUIManager.getMirrors, { cacheKey: 'napcat-mirrors', staleTime: 60 * 60 * 1000, }); const mirrors = mirrorsData?.mirrors || []; const loadPlugins = async () => { setLoading(true); try { const data = await PluginManager.getPluginStoreList(); setPlugins(data.plugins); } catch (e: any) { toast.error(e.message); } finally { setLoading(false); } }; useEffect(() => { loadPlugins(); }, []); // 按标签分类和搜索 const categorizedPlugins = useMemo(() => { let filtered = plugins; // 搜索过滤 if (searchQuery) { filtered = filtered.filter( (p) => p.name.toLowerCase().includes(searchQuery.toLowerCase()) || p.description.toLowerCase().includes(searchQuery.toLowerCase()) || p.author.toLowerCase().includes(searchQuery.toLowerCase()) || p.tags?.some((tag) => tag.toLowerCase().includes(searchQuery.toLowerCase())) ); } // 定义主要分类 const categories: Record = { all: filtered, official: filtered.filter(p => p.tags?.includes('官方')), tools: filtered.filter(p => p.tags?.includes('工具')), entertainment: filtered.filter(p => p.tags?.includes('娱乐')), other: filtered.filter(p => !p.tags?.some(t => ['官方', '工具', '娱乐'].includes(t))), }; return categories; }, [plugins, searchQuery]); const tabs = useMemo(() => { return [ { key: 'all', title: '全部', count: categorizedPlugins.all?.length || 0 }, { key: 'official', title: '官方', count: categorizedPlugins.official?.length || 0 }, { key: 'tools', title: '工具', count: categorizedPlugins.tools?.length || 0 }, { key: 'entertainment', title: '娱乐', count: categorizedPlugins.entertainment?.length || 0 }, { key: 'other', title: '其它', count: categorizedPlugins.other?.length || 0 }, ]; }, [categorizedPlugins]); const handleInstall = async (plugin: PluginStoreItem) => { // 检测是否是 GitHub 下载链接 const githubPattern = /^https:\/\/github\.com\//; const isGitHubUrl = githubPattern.test(plugin.downloadUrl); // 如果是 GitHub 链接,弹出镜像选择对话框 if (isGitHubUrl) { let selectedMirror: string | undefined = undefined; dialog.confirm({ title: '安装插件', content: (

插件名称: {plugin.name}

版本: v{plugin.version}

{plugin.description}

), confirmText: '开始安装', cancelText: '取消', onConfirm: async () => { await installPluginWithSSE(plugin.id, selectedMirror); }, }); } else { // 非 GitHub 链接,直接安装 await installPluginWithSSE(plugin.id); } }; const installPluginWithSSE = async (pluginId: string, mirror?: string) => { const loadingToast = toast.loading('正在准备安装...'); try { // 获取认证 token const token = localStorage.getItem(key.token); if (!token) { toast.error('未登录,请先登录', { id: loadingToast }); return; } const _token = JSON.parse(token); const params = new URLSearchParams({ id: pluginId }); if (mirror) { params.append('mirror', mirror); } const eventSource = new EventSourcePolyfill( `/api/Plugin/Store/Install/SSE?${params.toString()}`, { headers: { Authorization: `Bearer ${_token}`, Accept: 'text/event-stream', }, withCredentials: true, } ); eventSource.onmessage = (event) => { try { const data = JSON.parse(event.data); if (data.error) { toast.error(`安装失败: ${data.error}`, { id: loadingToast }); eventSource.close(); } else if (data.success) { toast.success('插件安装成功!', { id: loadingToast }); eventSource.close(); } else if (data.message) { toast.loading(data.message, { id: loadingToast }); } } catch (e) { console.error('Failed to parse SSE message:', e); } }; eventSource.onerror = (error) => { console.error('SSE连接出错:', error); toast.error('连接中断,安装失败', { id: loadingToast }); eventSource.close(); }; } catch (error: any) { toast.error(`安装失败: ${error.message || '未知错误'}`, { id: loadingToast }); } }; return ( <> 插件商店 - NapCat WebUI
{/* 头部 */}

插件商店

{/* 搜索框 */}
} value={searchQuery} onValueChange={setSearchQuery} className="max-w-md" />
{/* 标签页 */} setActiveTab(String(key))} classNames={{ tabList: 'bg-white/40 dark:bg-black/20 backdrop-blur-md', cursor: 'bg-white/80 dark:bg-white/10 backdrop-blur-md shadow-sm', }} > {tabs.map((tab) => (
{categorizedPlugins[tab.key]?.map((plugin) => ( handleInstall(plugin)} /> ))}
))}
); }