index.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import React, { createContext, useReducer, Dispatch } from 'react'
  2. import reducer from './reducer'
  3. import { Actions } from '../enum'
  4. import type { Elements, OnLoadParams } from 'react-flow-renderer'
  5. import { transformElements } from '@/components/Flow/src/shared/transformer'
  6. import { FlowTreeNode } from '@/components/Flow/src/type'
  7. import { DrawerAction } from '@/components/Drawer'
  8. export type FlowContextState = {
  9. dataID: string
  10. process: FlowTreeNode[]
  11. elements: Elements
  12. flowInstance?: OnLoadParams
  13. drawer: DrawerAction
  14. executorList: API.ExecutorItem[]
  15. matterList: any[]
  16. }
  17. type FlowContextProviderProps = {
  18. children: React.ReactNode
  19. initialProcess: Elements
  20. dataID: string
  21. executorList: API.ExecutorItem[]
  22. matterList: any[]
  23. }
  24. export type DispatchAction = Dispatch<{
  25. type: `${Actions}`
  26. payload: any
  27. }>
  28. const FlowContext = createContext<{ flowStore: FlowContextState; dispatch: DispatchAction }>({})
  29. const FlowContextProvider: React.FC<FlowContextProviderProps> = props => {
  30. const { children, initialProcess = [], dataID, executorList = [], matterList = [] } = props
  31. const [flowStore, dispatch] = useReducer(reducer, {
  32. process: initialProcess,
  33. elements: transformElements(initialProcess),
  34. dataID,
  35. executorList,
  36. matterList
  37. })
  38. return <FlowContext.Provider value={{ flowStore, dispatch }}>{children}</FlowContext.Provider>
  39. }
  40. export { FlowContext, FlowContextProvider, Actions }