Files
dify-plus/web/service/use-explore.ts
T
npc0-hue 062c8a5b22 fix: 升级依赖并修复相关问题
- 升级web依赖到最新版本
- 修复1.11.4版本兼容性问题
- 解决google字体dockerfile访问问题
- 恢复丢失的相关文件
- 添加多语言支持
- 调整docker版本及worker镜像配置
2026-01-28 09:40:42 +08:00

134 lines
3.8 KiB
TypeScript

import type { App, AppCategory } from '@/models/explore'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { useGlobalPublicStore } from '@/context/global-public-context'
import { AccessMode } from '@/models/access-control'
import {
fetchAppList,
fetchBanners,
fetchInstalledAppList,
fetchOpenInstalledAppList,
getAppAccessModeByAppId,
uninstallApp,
updatePinStatus,
} from './explore'
import { AppSourceType, fetchAppMeta, fetchAppParams } from './share'
const NAME_SPACE = 'explore'
type ExploreAppListData = {
categories: AppCategory[]
allList: App[]
}
export const useExploreAppList = () => {
return useQuery<ExploreAppListData>({
queryKey: [NAME_SPACE, 'appList'],
queryFn: async () => {
const { categories, recommended_apps } = await fetchAppList()
return {
categories,
allList: [...recommended_apps].sort((a, b) => a.position - b.position),
}
},
})
}
// Extend: start Installed app list sorted by usage
export const useInstalledAppList = () => {
return useQuery<ExploreAppListData>({
queryKey: [NAME_SPACE, 'installedAppList'],
queryFn: async () => {
const { categories, recommended_apps } = await fetchOpenInstalledAppList()
// Backend already sorts by AppStatisticsExtend.number.desc(), so we keep the order
return {
categories,
allList: recommended_apps,
}
},
})
}
// Extend: stop Installed app list sorted by usage
export const useGetInstalledApps = () => {
return useQuery({
queryKey: [NAME_SPACE, 'installedApps'],
queryFn: () => {
return fetchInstalledAppList()
},
})
}
export const useUninstallApp = () => {
const client = useQueryClient()
return useMutation({
mutationKey: [NAME_SPACE, 'uninstallApp'],
mutationFn: (appId: string) => uninstallApp(appId),
onSuccess: () => {
client.invalidateQueries({ queryKey: [NAME_SPACE, 'installedApps'] })
},
})
}
export const useUpdateAppPinStatus = () => {
const client = useQueryClient()
return useMutation({
mutationKey: [NAME_SPACE, 'updateAppPinStatus'],
mutationFn: ({ appId, isPinned }: { appId: string, isPinned: boolean }) => updatePinStatus(appId, isPinned),
onSuccess: () => {
client.invalidateQueries({ queryKey: [NAME_SPACE, 'installedApps'] })
},
})
}
export const useGetInstalledAppAccessModeByAppId = (appId: string | null) => {
const systemFeatures = useGlobalPublicStore(s => s.systemFeatures)
return useQuery({
queryKey: [NAME_SPACE, 'appAccessMode', appId, systemFeatures.webapp_auth.enabled],
queryFn: () => {
if (systemFeatures.webapp_auth.enabled === false) {
return {
accessMode: AccessMode.PUBLIC,
}
}
if (!appId || appId.length === 0)
return Promise.reject(new Error('App code is required to get access mode'))
return getAppAccessModeByAppId(appId)
},
enabled: !!appId,
})
}
export const useGetInstalledAppParams = (appId: string | null) => {
return useQuery({
queryKey: [NAME_SPACE, 'appParams', appId],
queryFn: () => {
if (!appId || appId.length === 0)
return Promise.reject(new Error('App ID is required to get app params'))
return fetchAppParams(AppSourceType.installedApp, appId)
},
enabled: !!appId,
})
}
export const useGetInstalledAppMeta = (appId: string | null) => {
return useQuery({
queryKey: [NAME_SPACE, 'appMeta', appId],
queryFn: () => {
if (!appId || appId.length === 0)
return Promise.reject(new Error('App ID is required to get app meta'))
return fetchAppMeta(AppSourceType.installedApp, appId)
},
enabled: !!appId,
})
}
export const useGetBanners = (locale?: string) => {
return useQuery({
queryKey: [NAME_SPACE, 'banners', locale],
queryFn: () => {
return fetchBanners(locale)
},
})
}