mirror of
https://github.com/langgenius/dify.git
synced 2026-07-26 06:08:38 +08:00
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: GareArc <garethcxy@dify.ai>
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
'use client'
|
|
|
|
import type { QueryClient } from '@tanstack/react-query'
|
|
import type { ReactNode } from 'react'
|
|
import { QueryClientProvider, QueryErrorResetBoundary } from '@tanstack/react-query'
|
|
import { queryClientAtom } from 'jotai-tanstack-query'
|
|
import { useHydrateAtoms } from 'jotai/react/utils'
|
|
import { isServer } from '@/utils/client'
|
|
import { makeQueryClient } from './query-client-server'
|
|
|
|
let browserQueryClient: QueryClient | undefined
|
|
|
|
function getQueryClient() {
|
|
if (isServer) {
|
|
return makeQueryClient()
|
|
}
|
|
if (!browserQueryClient) browserQueryClient = makeQueryClient()
|
|
return browserQueryClient
|
|
}
|
|
|
|
export function TanStackQueryProvider({ children }: { children: ReactNode }) {
|
|
const queryClient = getQueryClient()
|
|
return (
|
|
<QueryClientProvider client={queryClient}>
|
|
<QueryErrorResetBoundary>
|
|
<JotaiQueryClientHydrator queryClient={queryClient}>{children}</JotaiQueryClientHydrator>
|
|
</QueryErrorResetBoundary>
|
|
</QueryClientProvider>
|
|
)
|
|
}
|
|
|
|
function JotaiQueryClientHydrator({
|
|
children,
|
|
queryClient,
|
|
}: {
|
|
children: ReactNode
|
|
queryClient: QueryClient
|
|
}) {
|
|
useHydrateAtoms(new Map([[queryClientAtom, queryClient]]))
|
|
|
|
return children
|
|
}
|