mirror of
https://github.com/langgenius/dify.git
synced 2026-01-13 21:57:48 +08:00
refactor(web): replace useQueries with reusable useQuery hooks
Extract query logic into reusable hooks (useSystemFeaturesQuery, useSetupStatusQuery) instead of using useQueries array. This improves code reusability and allows other components to share the same query cache.
This commit is contained in:
parent
1126cf17bf
commit
a04f8fa2c5
@ -1,7 +1,7 @@
|
||||
'use client'
|
||||
import type { FC, PropsWithChildren } from 'react'
|
||||
import type { SystemFeatures } from '@/types/feature'
|
||||
import { useQueries, useQuery } from '@tanstack/react-query'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { create } from 'zustand'
|
||||
import Loading from '@/app/components/base/loading'
|
||||
import { getSystemFeatures } from '@/service/common'
|
||||
@ -19,6 +19,7 @@ export const useGlobalPublicStore = create<GlobalPublicStore>(set => ({
|
||||
}))
|
||||
|
||||
const systemFeaturesQueryKey = ['systemFeatures'] as const
|
||||
const setupStatusQueryKey = ['setupStatus'] as const
|
||||
|
||||
async function fetchSystemFeatures() {
|
||||
const data = await getSystemFeatures()
|
||||
@ -27,36 +28,37 @@ async function fetchSystemFeatures() {
|
||||
return data
|
||||
}
|
||||
|
||||
export function useIsSystemFeaturesPending() {
|
||||
const { isPending } = useQuery({
|
||||
export function useSystemFeaturesQuery() {
|
||||
return useQuery({
|
||||
queryKey: systemFeaturesQueryKey,
|
||||
queryFn: fetchSystemFeatures,
|
||||
})
|
||||
}
|
||||
|
||||
export function useIsSystemFeaturesPending() {
|
||||
const { isPending } = useSystemFeaturesQuery()
|
||||
return isPending
|
||||
}
|
||||
|
||||
export function useSetupStatusQuery() {
|
||||
return useQuery({
|
||||
queryKey: setupStatusQueryKey,
|
||||
queryFn: fetchSetupStatusWithCache,
|
||||
staleTime: Infinity,
|
||||
})
|
||||
}
|
||||
|
||||
const GlobalPublicStoreProvider: FC<PropsWithChildren> = ({
|
||||
children,
|
||||
}) => {
|
||||
// Fetch systemFeatures and setupStatus in parallel to reduce waterfall.
|
||||
// setupStatus is prefetched here and cached in localStorage for AppInitializer.
|
||||
// We only destructure featuresQuery since setupStatus result is not used directly.
|
||||
const [featuresQuery] = useQueries({
|
||||
queries: [
|
||||
{
|
||||
queryKey: systemFeaturesQueryKey,
|
||||
queryFn: fetchSystemFeatures,
|
||||
},
|
||||
{
|
||||
queryKey: ['setupStatus'],
|
||||
queryFn: fetchSetupStatusWithCache,
|
||||
staleTime: Infinity, // Once fetched, no need to refetch
|
||||
},
|
||||
],
|
||||
})
|
||||
const { isPending } = useSystemFeaturesQuery()
|
||||
|
||||
// Only block on systemFeatures, setupStatus is prefetched for AppInitializer
|
||||
if (featuresQuery.isPending)
|
||||
// Prefetch setupStatus for AppInitializer (result not needed here)
|
||||
useSetupStatusQuery()
|
||||
|
||||
if (isPending)
|
||||
return <div className="flex h-screen w-screen items-center justify-center"><Loading /></div>
|
||||
return <>{children}</>
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user