2024-04-08 18:51:46 +08:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useRef,
|
|
|
|
} from 'react'
|
2025-04-18 13:59:12 +08:00
|
|
|
import {
|
|
|
|
createWorkflowStore,
|
|
|
|
} from './store'
|
|
|
|
import type { StateCreator } from 'zustand'
|
|
|
|
import type { WorkflowSliceShape } from '@/app/components/workflow-app/store/workflow/workflow-slice'
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
type WorkflowStore = ReturnType<typeof createWorkflowStore>
|
|
|
|
export const WorkflowContext = createContext<WorkflowStore | null>(null)
|
|
|
|
|
2025-04-18 13:59:12 +08:00
|
|
|
export type WorkflowProviderProps = {
|
2024-04-08 18:51:46 +08:00
|
|
|
children: React.ReactNode
|
2025-04-18 13:59:12 +08:00
|
|
|
injectWorkflowStoreSliceFn?: StateCreator<WorkflowSliceShape>
|
2024-04-08 18:51:46 +08:00
|
|
|
}
|
2025-04-18 13:59:12 +08:00
|
|
|
export const WorkflowContextProvider = ({ children, injectWorkflowStoreSliceFn }: WorkflowProviderProps) => {
|
2025-03-21 17:41:03 +08:00
|
|
|
const storeRef = useRef<WorkflowStore | undefined>(undefined)
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
if (!storeRef.current)
|
2025-04-18 13:59:12 +08:00
|
|
|
storeRef.current = createWorkflowStore({ injectWorkflowStoreSliceFn })
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<WorkflowContext.Provider value={storeRef.current}>
|
|
|
|
{children}
|
|
|
|
</WorkflowContext.Provider>
|
|
|
|
)
|
|
|
|
}
|