store.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { useContext } from 'react'
  2. import {
  3. create,
  4. useStore as useZustandStore,
  5. } from 'zustand'
  6. import { debounce } from 'lodash-es'
  7. import type { Viewport } from 'reactflow'
  8. import type {
  9. HelpLineHorizontalPosition,
  10. HelpLineVerticalPosition,
  11. } from './help-line/types'
  12. import type {
  13. Edge,
  14. HistoryWorkflowData,
  15. Node,
  16. RunFile,
  17. ToolWithProvider,
  18. WorkflowRunningData,
  19. } from './types'
  20. import { WorkflowContext } from './context'
  21. type Shape = {
  22. appId: string
  23. workflowRunningData?: WorkflowRunningData
  24. setWorkflowRunningData: (workflowData: WorkflowRunningData) => void
  25. historyWorkflowData?: HistoryWorkflowData
  26. setHistoryWorkflowData: (historyWorkflowData: HistoryWorkflowData) => void
  27. showRunHistory: boolean
  28. setShowRunHistory: (showRunHistory: boolean) => void
  29. showFeaturesPanel: boolean
  30. setShowFeaturesPanel: (showFeaturesPanel: boolean) => void
  31. helpLineHorizontal?: HelpLineHorizontalPosition
  32. setHelpLineHorizontal: (helpLineHorizontal?: HelpLineHorizontalPosition) => void
  33. helpLineVertical?: HelpLineVerticalPosition
  34. setHelpLineVertical: (helpLineVertical?: HelpLineVerticalPosition) => void
  35. draftUpdatedAt: number
  36. setDraftUpdatedAt: (draftUpdatedAt: number) => void
  37. publishedAt: number
  38. setPublishedAt: (publishedAt: number) => void
  39. showInputsPanel: boolean
  40. setShowInputsPanel: (showInputsPanel: boolean) => void
  41. inputs: Record<string, string>
  42. setInputs: (inputs: Record<string, string>) => void
  43. files: RunFile[]
  44. setFiles: (files: RunFile[]) => void
  45. backupDraft?: {
  46. nodes: Node[]
  47. edges: Edge[]
  48. viewport: Viewport
  49. features: Record<string, any>
  50. }
  51. setBackupDraft: (backupDraft?: Shape['backupDraft']) => void
  52. notInitialWorkflow: boolean
  53. setNotInitialWorkflow: (notInitialWorkflow: boolean) => void
  54. nodesDefaultConfigs: Record<string, any>
  55. setNodesDefaultConfigs: (nodesDefaultConfigs: Record<string, any>) => void
  56. nodeAnimation: boolean
  57. setNodeAnimation: (nodeAnimation: boolean) => void
  58. isRestoring: boolean
  59. setIsRestoring: (isRestoring: boolean) => void
  60. debouncedSyncWorkflowDraft: (fn: () => void) => void
  61. buildInTools: ToolWithProvider[]
  62. setBuildInTools: (tools: ToolWithProvider[]) => void
  63. customTools: ToolWithProvider[]
  64. setCustomTools: (tools: ToolWithProvider[]) => void
  65. clipboardElements: Node[]
  66. setClipboardElements: (clipboardElements: Node[]) => void
  67. shortcutsDisabled: boolean
  68. setShortcutsDisabled: (shortcutsDisabled: boolean) => void
  69. }
  70. export const createWorkflowStore = () => {
  71. return create<Shape>(set => ({
  72. appId: '',
  73. workflowData: undefined,
  74. setWorkflowRunningData: workflowRunningData => set(() => ({ workflowRunningData })),
  75. historyWorkflowData: undefined,
  76. setHistoryWorkflowData: historyWorkflowData => set(() => ({ historyWorkflowData })),
  77. showRunHistory: false,
  78. setShowRunHistory: showRunHistory => set(() => ({ showRunHistory })),
  79. showFeaturesPanel: false,
  80. setShowFeaturesPanel: showFeaturesPanel => set(() => ({ showFeaturesPanel })),
  81. helpLineHorizontal: undefined,
  82. setHelpLineHorizontal: helpLineHorizontal => set(() => ({ helpLineHorizontal })),
  83. helpLineVertical: undefined,
  84. setHelpLineVertical: helpLineVertical => set(() => ({ helpLineVertical })),
  85. draftUpdatedAt: 0,
  86. setDraftUpdatedAt: draftUpdatedAt => set(() => ({ draftUpdatedAt: draftUpdatedAt ? draftUpdatedAt * 1000 : 0 })),
  87. publishedAt: 0,
  88. setPublishedAt: publishedAt => set(() => ({ publishedAt: publishedAt ? publishedAt * 1000 : 0 })),
  89. showInputsPanel: false,
  90. setShowInputsPanel: showInputsPanel => set(() => ({ showInputsPanel })),
  91. inputs: {},
  92. setInputs: inputs => set(() => ({ inputs })),
  93. files: [],
  94. setFiles: files => set(() => ({ files })),
  95. backupDraft: undefined,
  96. setBackupDraft: backupDraft => set(() => ({ backupDraft })),
  97. notInitialWorkflow: false,
  98. setNotInitialWorkflow: notInitialWorkflow => set(() => ({ notInitialWorkflow })),
  99. nodesDefaultConfigs: {},
  100. setNodesDefaultConfigs: nodesDefaultConfigs => set(() => ({ nodesDefaultConfigs })),
  101. nodeAnimation: false,
  102. setNodeAnimation: nodeAnimation => set(() => ({ nodeAnimation })),
  103. isRestoring: false,
  104. setIsRestoring: isRestoring => set(() => ({ isRestoring })),
  105. debouncedSyncWorkflowDraft: debounce((syncWorkflowDraft) => {
  106. syncWorkflowDraft()
  107. }, 5000),
  108. buildInTools: [],
  109. setBuildInTools: buildInTools => set(() => ({ buildInTools })),
  110. customTools: [],
  111. setCustomTools: customTools => set(() => ({ customTools })),
  112. clipboardElements: [],
  113. setClipboardElements: clipboardElements => set(() => ({ clipboardElements })),
  114. shortcutsDisabled: false,
  115. setShortcutsDisabled: shortcutsDisabled => set(() => ({ shortcutsDisabled })),
  116. }))
  117. }
  118. export function useStore<T>(selector: (state: Shape) => T): T {
  119. const store = useContext(WorkflowContext)
  120. if (!store)
  121. throw new Error('Missing WorkflowContext.Provider in the tree')
  122. return useZustandStore(store, selector)
  123. }
  124. export const useWorkflowStore = () => {
  125. return useContext(WorkflowContext)!
  126. }