mirror of
https://github.com/langgenius/dify.git
synced 2026-05-13 08:57:28 +08:00
Follow-up to SSR prefetch migration (2833965). Eliminates the Zustand
middleman that was syncing TanStack Query data into a separate store.
- Remove useGlobalPublicStore Zustand store entirely
- Create hooks/use-global-public.ts with useSystemFeatures,
useSystemFeaturesQuery, useIsSystemFeaturesPending, useSetupStatusQuery
- Migrate all 93 consumers to import from @/hooks/use-global-public
- Simplify global-public-context.tsx to a thin provider component
- Update 18 test files to mock the new hook interface
- Fix SetupStatusResponse.setup_at type from Date to string (JSON)
- Fix setup-status.spec.ts mock target to match consoleClient
BREAKING CHANGE: useGlobalPublicStore is removed. Use useSystemFeatures()
from @/hooks/use-global-public instead.
110 lines
3.2 KiB
TypeScript
110 lines
3.2 KiB
TypeScript
import type { App, AppCategory } from '@/models/explore'
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { useSystemFeatures } from '@/hooks/use-global-public'
|
|
import { AccessMode } from '@/models/access-control'
|
|
import { fetchAppList, fetchBanners, fetchInstalledAppList, 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),
|
|
}
|
|
},
|
|
})
|
|
}
|
|
|
|
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 = useSystemFeatures()
|
|
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)
|
|
},
|
|
})
|
|
}
|