'use client'; import PluginInstalledComponent, { PluginInstalledComponentRef } from '@/app/home/plugins/plugin-installed/PluginInstalledComponent'; import PluginMarketComponent from '@/app/home/plugins/plugin-market/PluginMarketComponent'; import styles from './plugins.module.css'; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Button } from "@/components/ui/button"; import { PlusIcon } from "lucide-react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { GithubIcon } from "lucide-react"; import { useState, useRef, useEffect } from 'react'; import { httpClient } from '@/app/infra/http/HttpClient'; enum PluginInstallStatus { WAIT_INPUT = 'wait_input', INSTALLING = 'installing', ERROR = 'error', } export default function PluginConfigPage() { const [modalOpen, setModalOpen] = useState(false); const [pluginInstallStatus, setPluginInstallStatus] = useState(PluginInstallStatus.WAIT_INPUT); const [installError, setInstallError] = useState(null); const [githubURL, setGithubURL] = useState(''); const pluginInstalledRef = useRef(null); function handleModalConfirm() { installPlugin(githubURL); } function installPlugin(url: string) { setPluginInstallStatus(PluginInstallStatus.INSTALLING); httpClient .installPluginFromGithub(url) .then((resp) => { const taskId = resp.task_id; // 每秒拉取一次任务状态 const interval = setInterval(() => { httpClient.getAsyncTask(taskId).then((resp) => { console.log('task status:', resp); if (resp.runtime.done) { clearInterval(interval); if (resp.runtime.exception) { setInstallError(resp.runtime.exception); setPluginInstallStatus(PluginInstallStatus.ERROR); } else { // success setGithubURL(''); setModalOpen(false); pluginInstalledRef.current?.refreshPluginList(); } } }); }, 1000); }) .catch((err) => { console.log('error when install plugin:', err); setInstallError(err.message); setPluginInstallStatus(PluginInstallStatus.ERROR); }); } return (
已安装 插件市场
{ setGithubURL(githubURL); setModalOpen(true); setPluginInstallStatus(PluginInstallStatus.WAIT_INPUT); setInstallError(null); }} />
从 GitHub 安装插件 {pluginInstallStatus === PluginInstallStatus.WAIT_INPUT && (

目前仅支持从 GitHub 安装

setGithubURL(e.target.value)} className="mb-4" />
)} {pluginInstallStatus === PluginInstallStatus.INSTALLING && (

正在安装插件...

)} {pluginInstallStatus === PluginInstallStatus.ERROR && (

插件安装失败:

{installError}

)} {pluginInstallStatus === PluginInstallStatus.WAIT_INPUT && ( <> )} {pluginInstallStatus === PluginInstallStatus.ERROR && ( )}
); }