dify/web/service/use-explore.ts
Jingyi 9b74df21d0
feat(web): refine onboarding UI (#37433)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: yyh <yuanyouhuilyz@gmail.com>
Co-authored-by: Joel <iamjoel007@gmail.com>
Co-authored-by: hjlarry <hjlarry@163.com>
Co-authored-by: fatelei <fatelei@gmail.com>
Co-authored-by: Asuka Minato <i@asukaminato.eu.org>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
Co-authored-by: gigglewang <gigglewang@dify.ai>
Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai>
Co-authored-by: chariri <w@chariri.moe>
Co-authored-by: Evan <2869018789@qq.com>
Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
2026-06-15 08:47:15 +00:00

152 lines
5.0 KiB
TypeScript

import type { App, AppCategory } from '@/models/explore'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { useLocale } from '@/context/i18n'
import { systemFeaturesQueryOptions } from '@/features/system-features/client'
import { AccessMode } from '@/models/access-control'
import { consoleQuery } from './client'
import {
fetchAppList,
fetchInstalledAppList,
fetchInstalledAppMeta,
fetchInstalledAppParams,
fetchLearnDifyAppList,
getAppAccessModeByAppId,
uninstallApp,
updatePinStatus,
} from './explore'
type ExploreAppListData = {
categories: AppCategory[]
allList: App[]
}
export const useExploreAppList = () => {
const locale = useLocale()
const exploreAppsInput = locale
? { query: { language: locale } }
: {}
const exploreAppsLanguage = exploreAppsInput?.query?.language
return useQuery<ExploreAppListData>({
queryKey: [...consoleQuery.explore.apps.queryKey({ input: exploreAppsInput }), exploreAppsLanguage],
queryFn: async () => {
const { categories, recommended_apps } = await fetchAppList(exploreAppsLanguage)
return {
categories,
allList: [...recommended_apps].sort((a, b) => a.position - b.position),
}
},
})
}
export const useLearnDifyAppList = () => {
const locale = useLocale()
const learnDifyAppsInput = locale
? { query: { language: locale } }
: {}
const learnDifyAppsLanguage = learnDifyAppsInput?.query?.language
return useQuery({
queryKey: [...consoleQuery.explore.learnDifyApps.queryKey({ input: learnDifyAppsInput }), learnDifyAppsLanguage],
queryFn: async () => {
const { recommended_apps } = await fetchLearnDifyAppList(learnDifyAppsLanguage)
return [...recommended_apps].sort((a, b) => a.position - b.position)
},
})
}
export const useGetInstalledApps = () => {
return useQuery({
queryKey: consoleQuery.explore.installedApps.queryKey({ input: {} }),
queryFn: () => {
return fetchInstalledAppList()
},
})
}
export const useUninstallApp = () => {
const client = useQueryClient()
return useMutation({
mutationKey: consoleQuery.explore.uninstallInstalledApp.mutationKey(),
mutationFn: (appId: string) => uninstallApp(appId),
onSuccess: () => {
client.invalidateQueries({
queryKey: consoleQuery.explore.installedApps.queryKey({ input: {} }),
})
},
})
}
export const useUpdateAppPinStatus = () => {
const client = useQueryClient()
return useMutation({
mutationKey: consoleQuery.explore.updateInstalledApp.mutationKey(),
mutationFn: ({ appId, isPinned }: { appId: string, isPinned: boolean }) => updatePinStatus(appId, isPinned),
onSuccess: () => {
client.invalidateQueries({
queryKey: consoleQuery.explore.installedApps.queryKey({ input: {} }),
})
},
})
}
export const useGetInstalledAppAccessModeByAppId = (appId: string | null) => {
// useQuery (not useSuspenseQuery) to keep this service hook's call contract
// unchanged from the zustand era: callers should not need a Suspense boundary.
// First-fetch undefined is bridged via `?? false` so the inner queryKey is stable.
const { data: systemFeatures } = useQuery(systemFeaturesQueryOptions())
const webappAuthEnabled = systemFeatures?.webapp_auth.enabled ?? false
const appAccessModeInput = { query: { appId: appId ?? '' } }
const installedAppId = appAccessModeInput.query.appId
return useQuery({
queryKey: [
...consoleQuery.explore.appAccessMode.queryKey({ input: appAccessModeInput }),
webappAuthEnabled,
installedAppId,
],
queryFn: () => {
if (webappAuthEnabled === false) {
return {
accessMode: AccessMode.PUBLIC,
}
}
if (!installedAppId)
return Promise.reject(new Error('App ID is required to get access mode'))
return getAppAccessModeByAppId(installedAppId)
},
enabled: !!installedAppId,
})
}
export const useGetInstalledAppParams = (appId: string | null) => {
const installedAppParamsInput = { params: { appId: appId ?? '' } }
const installedAppId = installedAppParamsInput.params.appId
return useQuery({
queryKey: [...consoleQuery.explore.installedAppParameters.queryKey({ input: installedAppParamsInput }), installedAppId],
queryFn: () => {
if (!installedAppId)
return Promise.reject(new Error('App ID is required to get app params'))
return fetchInstalledAppParams(installedAppId)
},
enabled: !!installedAppId,
})
}
export const useGetInstalledAppMeta = (appId: string | null) => {
const installedAppMetaInput = { params: { appId: appId ?? '' } }
const installedAppId = installedAppMetaInput.params.appId
return useQuery({
queryKey: [...consoleQuery.explore.installedAppMeta.queryKey({ input: installedAppMetaInput }), installedAppId],
queryFn: () => {
if (!installedAppId)
return Promise.reject(new Error('App ID is required to get app meta'))
return fetchInstalledAppMeta(installedAppId)
},
enabled: !!installedAppId,
})
}