import { useState } from "react"; import { App, Button, Input, Modal, Popconfirm, Switch } from "antd"; import { AlertTriangle, Puzzle, RefreshCw, Trash2 } from "lucide-react"; import { canvasThemes } from "@/lib/canvas-theme"; import { installPluginFromUrl, setPluginEnabled, uninstallPlugin, updatePlugin } from "@/lib/canvas/plugin-loader"; import { useThemeStore } from "@/stores/use-theme-store"; import { usePluginStore, type InstalledPlugin } from "@/stores/canvas/use-plugin-store"; export function CanvasPluginManagerModal({ open, onClose }: { open: boolean; onClose: () => void }) { const theme = canvasThemes[useThemeStore((state) => state.theme)]; const { message } = App.useApp(); const plugins = usePluginStore((state) => state.plugins); const [url, setUrl] = useState(""); const [installing, setInstalling] = useState(false); const [busyId, setBusyId] = useState(null); const handleInstall = async () => { const target = url.trim(); if (!target) return; setInstalling(true); try { const plugin = await installPluginFromUrl(target); message.success(`已安装插件 ${plugin.name}`); setUrl(""); } catch (error) { message.error(`安装失败:${error instanceof Error ? error.message : String(error)}`); } finally { setInstalling(false); } }; const runOnPlugin = async (record: InstalledPlugin, action: () => Promise, successText: string) => { setBusyId(record.id); try { await action(); message.success(successText); } catch (error) { message.error(`${error instanceof Error ? error.message : String(error)}`); } finally { setBusyId(null); } }; return (
插件代码会在当前页面内直接执行,可访问本地数据(包含 AI API Key)。请仅安装你信任来源的插件。
setUrl(event.target.value)} onPressEnter={handleInstall} allowClear />
{plugins.length === 0 ? (
还没有安装任何插件
) : ( plugins.map((record) => (
{record.name} v{record.version}
{record.description || record.url}
runOnPlugin(record, () => setPluginEnabled(record, checked), checked ? "已启用" : "已禁用")} />
)) )}
); }