diff --git a/web/app/components/plugins/hooks.ts b/web/app/components/plugins/hooks.ts deleted file mode 100644 index ebee3a6331..0000000000 --- a/web/app/components/plugins/hooks.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { useRequest } from 'ahooks' - -export const useCheckInstallStatus = () => { - const { data, run, cancel } = useRequest(async () => {}, { - manual: true, - pollingInterval: 5000, - pollingErrorRetryCount: 2, - }) - - return { - data, - run, - cancel, - } -} diff --git a/web/app/components/plugins/plugin-page/hooks.ts b/web/app/components/plugins/plugin-page/hooks.ts new file mode 100644 index 0000000000..395d90164a --- /dev/null +++ b/web/app/components/plugins/plugin-page/hooks.ts @@ -0,0 +1,37 @@ +import { + useCallback, + useEffect, + useState, +} from 'react' +import { useRequest } from 'ahooks' +import type { PluginTask } from '../types' +import { fetchPluginTasks } from '@/service/plugins' + +export const usePluginTasks = () => { + const [pluginTasks, setPluginTasks] = useState([]) + + const handleUpdatePluginTasks = async (callback: (tasks: PluginTask[]) => void) => { + const { tasks } = await fetchPluginTasks() + setPluginTasks(tasks) + callback(tasks) + } + + const { run, cancel } = useRequest(handleUpdatePluginTasks, { + manual: true, + pollingInterval: 3000, + pollingErrorRetryCount: 2, + }) + + const checkHasPluginTasks = useCallback((tasks: PluginTask[]) => { + if (!tasks.length) + cancel() + }, [cancel]) + + useEffect(() => { + run(checkHasPluginTasks) + }, [run, checkHasPluginTasks]) + + return { + pluginTasks, + } +} diff --git a/web/app/components/plugins/plugin-page/index.tsx b/web/app/components/plugins/plugin-page/index.tsx index 93eb3dcabf..d8b7d649d8 100644 --- a/web/app/components/plugins/plugin-page/index.tsx +++ b/web/app/components/plugins/plugin-page/index.tsx @@ -17,6 +17,7 @@ import InstallPluginDropdown from './install-plugin-dropdown' import { useUploader } from './use-uploader' import usePermission from './use-permission' import DebugInfo from './debug-info' +import { usePluginTasks } from './hooks' import { useTabSearchParams } from '@/hooks/use-tab-searchparams' import Button from '@/app/components/base/button' import TabSlider from '@/app/components/base/tab-slider' @@ -124,6 +125,8 @@ const PluginPage = ({ const { dragging, fileUploader, fileChangeHandle, removeFile } = uploaderProps + const { pluginTasks } = usePluginTasks() + return (
(url) } +export const fetchPluginTasks = async () => { + return get('/workspaces/current/plugin/tasks?page=1&page_size=255') +} + export const checkTaskStatus = async (taskId: string) => { return get(`/workspaces/current/plugin/tasks/${taskId}`) }