2024-11-12 17:58:14 +08:00
|
|
|
import { useCallback, useState } from 'react'
|
2024-11-08 18:21:39 +08:00
|
|
|
import type {
|
|
|
|
DebugInfo as DebugInfoTypes,
|
2024-11-13 15:48:06 +08:00
|
|
|
Dependency,
|
2024-11-19 16:55:36 +08:00
|
|
|
GitHubItemAndMarketPlaceDependency,
|
2024-11-08 18:25:15 +08:00
|
|
|
InstallPackageResponse,
|
2024-11-08 18:21:39 +08:00
|
|
|
InstalledPluginListResponse,
|
2024-11-19 16:55:36 +08:00
|
|
|
PackageDependency,
|
2024-11-08 18:21:39 +08:00
|
|
|
Permissions,
|
2024-11-12 17:58:14 +08:00
|
|
|
PluginTask,
|
2024-11-08 18:21:39 +08:00
|
|
|
PluginsFromMarketplaceResponse,
|
2024-11-17 12:57:34 +08:00
|
|
|
VersionListResponse,
|
2024-11-14 16:54:23 +08:00
|
|
|
uploadGitHubResponse,
|
2024-11-08 18:21:39 +08:00
|
|
|
} from '@/app/components/plugins/types'
|
2024-11-15 15:50:14 +08:00
|
|
|
import { TaskStatus } from '@/app/components/plugins/types'
|
2024-11-08 18:21:39 +08:00
|
|
|
import type {
|
|
|
|
PluginsSearchParams,
|
|
|
|
} from '@/app/components/plugins/marketplace/types'
|
2024-11-17 12:57:34 +08:00
|
|
|
import { get, getMarketplace, post, postMarketplace } from './base'
|
2024-11-07 16:52:22 +08:00
|
|
|
import {
|
2024-11-08 16:11:50 +08:00
|
|
|
useMutation,
|
2024-11-07 16:52:22 +08:00
|
|
|
useQuery,
|
2024-11-08 18:21:39 +08:00
|
|
|
useQueryClient,
|
2024-11-07 16:52:22 +08:00
|
|
|
} from '@tanstack/react-query'
|
2024-11-13 15:48:06 +08:00
|
|
|
import { useStore as usePluginDependencyStore } from '@/app/components/workflow/plugin-dependency/store'
|
2024-11-13 16:22:09 +08:00
|
|
|
import { useInvalidateAllBuiltInTools } from './use-tools'
|
2024-11-07 16:52:22 +08:00
|
|
|
|
|
|
|
const NAME_SPACE = 'plugins'
|
|
|
|
|
|
|
|
const useInstalledPluginListKey = [NAME_SPACE, 'installedPluginList']
|
|
|
|
export const useInstalledPluginList = () => {
|
|
|
|
return useQuery<InstalledPluginListResponse>({
|
|
|
|
queryKey: useInstalledPluginListKey,
|
|
|
|
queryFn: () => get<InstalledPluginListResponse>('/workspaces/current/plugin/list'),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useInvalidateInstalledPluginList = () => {
|
|
|
|
const queryClient = useQueryClient()
|
2024-11-13 16:22:09 +08:00
|
|
|
const invalidateAllBuiltInTools = useInvalidateAllBuiltInTools()
|
2024-11-07 16:52:22 +08:00
|
|
|
return () => {
|
|
|
|
queryClient.invalidateQueries(
|
|
|
|
{
|
|
|
|
queryKey: useInstalledPluginListKey,
|
|
|
|
})
|
2024-11-13 16:22:09 +08:00
|
|
|
invalidateAllBuiltInTools()
|
2024-11-07 16:52:22 +08:00
|
|
|
}
|
|
|
|
}
|
2024-11-08 15:10:06 +08:00
|
|
|
|
2024-11-08 18:25:15 +08:00
|
|
|
export const useInstallPackageFromMarketPlace = () => {
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: (uniqueIdentifier: string) => {
|
|
|
|
return post<InstallPackageResponse>('/workspaces/current/plugin/install/marketplace', { body: { plugin_unique_identifiers: [uniqueIdentifier] } })
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-17 12:57:34 +08:00
|
|
|
export const useVersionListOfPlugin = (pluginID: string) => {
|
|
|
|
return useQuery<{ data: VersionListResponse }>({
|
|
|
|
queryKey: [NAME_SPACE, 'versions', pluginID],
|
|
|
|
queryFn: () => getMarketplace<{ data: VersionListResponse }>(`/plugins/${pluginID}/versions`, { params: { page: 1, page_size: 100 } }),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
export const useInvalidateVersionListOfPlugin = () => {
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
return (pluginID: string) => {
|
|
|
|
queryClient.invalidateQueries({ queryKey: [NAME_SPACE, 'versions', pluginID] })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-11 13:00:41 +08:00
|
|
|
export const useInstallPackageFromLocal = () => {
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: (uniqueIdentifier: string) => {
|
|
|
|
return post<InstallPackageResponse>('/workspaces/current/plugin/install/pkg', {
|
|
|
|
body: { plugin_unique_identifiers: [uniqueIdentifier] },
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-14 18:26:16 +08:00
|
|
|
export const useInstallPackageFromGitHub = () => {
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: ({ repoUrl, selectedVersion, selectedPackage, uniqueIdentifier }: {
|
|
|
|
repoUrl: string
|
|
|
|
selectedVersion: string
|
|
|
|
selectedPackage: string
|
|
|
|
uniqueIdentifier: string
|
|
|
|
}) => {
|
|
|
|
return post<InstallPackageResponse>('/workspaces/current/plugin/install/github', {
|
|
|
|
body: {
|
|
|
|
repo: repoUrl,
|
|
|
|
version: selectedVersion,
|
|
|
|
package: selectedPackage,
|
|
|
|
plugin_unique_identifier: uniqueIdentifier,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-14 16:54:23 +08:00
|
|
|
export const useUploadGitHub = (payload: {
|
|
|
|
repo: string
|
|
|
|
version: string
|
|
|
|
package: string
|
|
|
|
}) => {
|
|
|
|
return useQuery({
|
|
|
|
queryKey: [NAME_SPACE, 'uploadGitHub', payload],
|
|
|
|
queryFn: () => post<uploadGitHubResponse>('/workspaces/current/plugin/upload/github', {
|
|
|
|
body: payload,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-15 12:07:20 +08:00
|
|
|
export const useInstallFromMarketplaceAndGitHub = ({
|
|
|
|
onSuccess,
|
|
|
|
}: {
|
2024-11-15 13:09:19 +08:00
|
|
|
onSuccess?: (res: { success: boolean }[]) => void
|
2024-11-15 12:07:20 +08:00
|
|
|
}) => {
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: (payload: Dependency[]) => {
|
|
|
|
return Promise.all(payload.map(async (item) => {
|
|
|
|
try {
|
|
|
|
if (item.type === 'github') {
|
2024-11-19 16:55:36 +08:00
|
|
|
const data = item as GitHubItemAndMarketPlaceDependency
|
2024-11-15 12:07:20 +08:00
|
|
|
await post<InstallPackageResponse>('/workspaces/current/plugin/install/github', {
|
|
|
|
body: {
|
2024-11-19 16:55:36 +08:00
|
|
|
repo: data.value.repo!,
|
2024-11-20 15:05:17 +08:00
|
|
|
version: data.value.release! || data.value.version!,
|
|
|
|
package: data.value.packages! || data.value.package!,
|
2024-11-19 16:55:36 +08:00
|
|
|
plugin_unique_identifier: data.value.github_plugin_unique_identifier!,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (item.type === 'marketplace') {
|
|
|
|
const data = item as GitHubItemAndMarketPlaceDependency
|
|
|
|
|
|
|
|
await post<InstallPackageResponse>('/workspaces/current/plugin/install/marketplace', {
|
|
|
|
body: {
|
|
|
|
plugin_unique_identifiers: [data.value.plugin_unique_identifier!],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (item.type === 'package') {
|
|
|
|
const data = item as PackageDependency
|
|
|
|
await post<InstallPackageResponse>('/workspaces/current/plugin/install/pkg', {
|
|
|
|
body: {
|
|
|
|
plugin_unique_identifiers: [data.value.unique_identifier],
|
2024-11-15 12:07:20 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return ({ success: true })
|
|
|
|
}
|
2024-11-15 14:40:04 +08:00
|
|
|
// eslint-disable-next-line unused-imports/no-unused-vars
|
2024-11-15 12:07:20 +08:00
|
|
|
catch (e) {
|
|
|
|
return Promise.resolve({ success: false })
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
onSuccess,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-08 15:10:06 +08:00
|
|
|
export const useDebugKey = () => {
|
|
|
|
return useQuery({
|
|
|
|
queryKey: [NAME_SPACE, 'debugKey'],
|
|
|
|
queryFn: () => get<DebugInfoTypes>('/workspaces/current/plugin/debugging-key'),
|
|
|
|
})
|
|
|
|
}
|
2024-11-08 16:11:50 +08:00
|
|
|
|
|
|
|
const usePermissionsKey = [NAME_SPACE, 'permissions']
|
|
|
|
export const usePermissions = () => {
|
|
|
|
return useQuery({
|
|
|
|
queryKey: usePermissionsKey,
|
|
|
|
queryFn: () => get<Permissions>('/workspaces/current/plugin/permission/fetch'),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useInvalidatePermissions = () => {
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
return () => {
|
|
|
|
queryClient.invalidateQueries(
|
|
|
|
{
|
|
|
|
queryKey: usePermissionsKey,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useMutationPermissions = ({
|
|
|
|
onSuccess,
|
|
|
|
}: {
|
|
|
|
onSuccess?: () => void
|
|
|
|
}) => {
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: (payload: Permissions) => {
|
|
|
|
return post('/workspaces/current/plugin/permission/change', { body: payload })
|
|
|
|
},
|
|
|
|
onSuccess,
|
|
|
|
})
|
|
|
|
}
|
2024-11-08 18:21:39 +08:00
|
|
|
|
|
|
|
export const useMutationPluginsFromMarketplace = () => {
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: (pluginsSearchParams: PluginsSearchParams) => {
|
|
|
|
const {
|
|
|
|
query,
|
|
|
|
sortBy,
|
|
|
|
sortOrder,
|
|
|
|
category,
|
|
|
|
tags,
|
|
|
|
} = pluginsSearchParams
|
|
|
|
return postMarketplace<{ data: PluginsFromMarketplaceResponse }>('/plugins/search/basic', {
|
|
|
|
body: {
|
|
|
|
page: 1,
|
|
|
|
page_size: 10,
|
|
|
|
query,
|
|
|
|
sort_by: sortBy,
|
|
|
|
sort_order: sortOrder,
|
|
|
|
category: category !== 'all' ? category : '',
|
|
|
|
tags,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2024-11-12 17:58:14 +08:00
|
|
|
|
2024-11-14 16:54:23 +08:00
|
|
|
export const useFetchPluginsInMarketPlaceByIds = (unique_identifiers: string[]) => {
|
|
|
|
return useQuery({
|
|
|
|
queryKey: [NAME_SPACE, 'fetchPluginsInMarketPlaceByIds', unique_identifiers],
|
|
|
|
queryFn: () => postMarketplace<{ data: PluginsFromMarketplaceResponse }>('/plugins/identifier/batch', {
|
|
|
|
body: {
|
|
|
|
unique_identifiers,
|
|
|
|
},
|
|
|
|
}),
|
2024-11-19 18:32:17 +08:00
|
|
|
enabled: unique_identifiers.filter(i => !!i).length > 0, // loaded then fetch
|
2024-11-14 16:54:23 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-12 17:58:14 +08:00
|
|
|
const usePluginTaskListKey = [NAME_SPACE, 'pluginTaskList']
|
|
|
|
export const usePluginTaskList = () => {
|
2024-11-13 11:33:39 +08:00
|
|
|
const [enabled, setEnabled] = useState(true)
|
2024-11-12 17:58:14 +08:00
|
|
|
const {
|
|
|
|
data,
|
|
|
|
isFetched,
|
|
|
|
refetch,
|
|
|
|
...rest
|
|
|
|
} = useQuery({
|
|
|
|
queryKey: usePluginTaskListKey,
|
|
|
|
queryFn: async () => {
|
|
|
|
const currentData = await get<{ tasks: PluginTask[] }>('/workspaces/current/plugin/tasks?page=1&page_size=100')
|
2024-11-15 15:50:14 +08:00
|
|
|
const taskDone = currentData.tasks.every(task => task.status === TaskStatus.success)
|
2024-11-12 17:58:14 +08:00
|
|
|
|
|
|
|
if (taskDone)
|
|
|
|
setEnabled(false)
|
|
|
|
|
|
|
|
return currentData
|
|
|
|
},
|
2024-11-15 15:50:14 +08:00
|
|
|
refetchInterval: 5000,
|
2024-11-12 17:58:14 +08:00
|
|
|
enabled,
|
|
|
|
})
|
|
|
|
const handleRefetch = useCallback(() => {
|
|
|
|
setEnabled(true)
|
|
|
|
refetch()
|
|
|
|
}, [refetch])
|
|
|
|
|
|
|
|
return {
|
|
|
|
data,
|
|
|
|
pluginTasks: data?.tasks || [],
|
|
|
|
isFetched,
|
|
|
|
handleRefetch,
|
|
|
|
...rest,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useMutationClearTaskPlugin = () => {
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: ({ taskId, pluginId }: { taskId: string; pluginId: string }) => {
|
2024-11-13 11:33:39 +08:00
|
|
|
return post<{ success: boolean }>(`/workspaces/current/plugin/tasks/${taskId}/delete/${pluginId}`)
|
2024-11-12 17:58:14 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2024-11-13 15:48:06 +08:00
|
|
|
|
|
|
|
export const useMutationCheckDependenciesBeforeImportDSL = () => {
|
|
|
|
const mutation = useMutation({
|
|
|
|
mutationFn: ({ dslString, url }: { dslString?: string, url?: string }) => {
|
|
|
|
if (url) {
|
|
|
|
return post<{ leaked: Dependency[] }>(
|
|
|
|
'/apps/import/url/dependencies/check',
|
|
|
|
{
|
|
|
|
body: {
|
|
|
|
url,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return post<{ leaked: Dependency[] }>(
|
|
|
|
'/apps/import/dependencies/check',
|
|
|
|
{
|
|
|
|
body: {
|
|
|
|
data: dslString,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
onSuccess: (data) => {
|
|
|
|
const { setDependencies } = usePluginDependencyStore.getState()
|
|
|
|
setDependencies(data.leaked || [])
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return mutation
|
|
|
|
}
|