dify/web/app/components/workflow/hooks-store/provider.tsx
Stephen Zhou a84c2d36a3
style: format with vp fmt (#38803)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-12 15:57:46 +00:00

34 lines
1.2 KiB
TypeScript

import type { Shape } from './store'
import { createContext, useEffect, useRef } from 'react'
import { useStore } from 'reactflow'
import { createHooksStore } from './store'
type HooksStore = ReturnType<typeof createHooksStore>
export const HooksStoreContext = createContext<HooksStore | null | undefined>(null)
type HooksStoreContextProviderProps = Partial<Shape> & {
children: React.ReactNode
}
export const HooksStoreContextProvider = ({
children,
...restProps
}: HooksStoreContextProviderProps) => {
const storeRef = useRef<HooksStore | undefined>(undefined)
const d3Selection = useStore((s) => s.d3Selection)
const d3Zoom = useStore((s) => s.d3Zoom)
const { accessControl } = restProps
useEffect(() => {
if (storeRef.current && d3Selection && d3Zoom) storeRef.current.getState().refreshAll(restProps)
}, [d3Selection, d3Zoom])
useEffect(() => {
if (storeRef.current && accessControl) storeRef.current.getState().refreshAll({ accessControl })
}, [accessControl])
if (!storeRef.current) storeRef.current = createHooksStore(restProps)
return (
<HooksStoreContext.Provider value={storeRef.current}>{children}</HooksStoreContext.Provider>
)
}