store.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { useContext } from 'react'
  2. import {
  3. useStore as useZustandStore,
  4. } from 'zustand'
  5. import { createStore } from 'zustand/vanilla'
  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 PreviewRunningData = WorkflowRunningData & {
  22. resultTabActive?: boolean
  23. resultText?: string
  24. }
  25. type Shape = {
  26. appId: string
  27. panelWidth: number
  28. workflowRunningData?: PreviewRunningData
  29. setWorkflowRunningData: (workflowData: PreviewRunningData) => void
  30. historyWorkflowData?: HistoryWorkflowData
  31. setHistoryWorkflowData: (historyWorkflowData?: HistoryWorkflowData) => void
  32. showRunHistory: boolean
  33. setShowRunHistory: (showRunHistory: boolean) => void
  34. showFeaturesPanel: boolean
  35. setShowFeaturesPanel: (showFeaturesPanel: boolean) => void
  36. helpLineHorizontal?: HelpLineHorizontalPosition
  37. setHelpLineHorizontal: (helpLineHorizontal?: HelpLineHorizontalPosition) => void
  38. helpLineVertical?: HelpLineVerticalPosition
  39. setHelpLineVertical: (helpLineVertical?: HelpLineVerticalPosition) => void
  40. draftUpdatedAt: number
  41. setDraftUpdatedAt: (draftUpdatedAt: number) => void
  42. publishedAt: number
  43. setPublishedAt: (publishedAt: number) => void
  44. showInputsPanel: boolean
  45. setShowInputsPanel: (showInputsPanel: boolean) => void
  46. inputs: Record<string, string>
  47. setInputs: (inputs: Record<string, string>) => void
  48. files: RunFile[]
  49. setFiles: (files: RunFile[]) => void
  50. backupDraft?: {
  51. nodes: Node[]
  52. edges: Edge[]
  53. viewport: Viewport
  54. features: Record<string, any>
  55. }
  56. setBackupDraft: (backupDraft?: Shape['backupDraft']) => void
  57. notInitialWorkflow: boolean
  58. setNotInitialWorkflow: (notInitialWorkflow: boolean) => void
  59. nodesDefaultConfigs: Record<string, any>
  60. setNodesDefaultConfigs: (nodesDefaultConfigs: Record<string, any>) => void
  61. nodeAnimation: boolean
  62. setNodeAnimation: (nodeAnimation: boolean) => void
  63. isRestoring: boolean
  64. setIsRestoring: (isRestoring: boolean) => void
  65. debouncedSyncWorkflowDraft: (fn: () => void) => void
  66. buildInTools: ToolWithProvider[]
  67. setBuildInTools: (tools: ToolWithProvider[]) => void
  68. customTools: ToolWithProvider[]
  69. setCustomTools: (tools: ToolWithProvider[]) => void
  70. clipboardElements: Node[]
  71. setClipboardElements: (clipboardElements: Node[]) => void
  72. shortcutsDisabled: boolean
  73. setShortcutsDisabled: (shortcutsDisabled: boolean) => void
  74. showDebugAndPreviewPanel: boolean
  75. setShowDebugAndPreviewPanel: (showDebugAndPreviewPanel: boolean) => void
  76. selection: null | { x1: number; y1: number; x2: number; y2: number }
  77. setSelection: (selection: Shape['selection']) => void
  78. bundleNodeSize: { width: number; height: number } | null
  79. setBundleNodeSize: (bundleNodeSize: Shape['bundleNodeSize']) => void
  80. controlMode: 'pointer' | 'hand'
  81. setControlMode: (controlMode: Shape['controlMode']) => void
  82. candidateNode?: Node
  83. setCandidateNode: (candidateNode?: Node) => void
  84. panelMenu?: {
  85. top: number
  86. left: number
  87. }
  88. setPanelMenu: (panelMenu: Shape['panelMenu']) => void
  89. nodeMenu?: {
  90. top: number
  91. left: number
  92. nodeId: string
  93. }
  94. setNodeMenu: (nodeMenu: Shape['nodeMenu']) => void
  95. mousePosition: { pageX: number; pageY: number; elementX: number; elementY: number }
  96. setMousePosition: (mousePosition: Shape['mousePosition']) => void
  97. syncWorkflowDraftHash: string
  98. setSyncWorkflowDraftHash: (hash: string) => void
  99. }
  100. export const createWorkflowStore = () => {
  101. return createStore<Shape>(set => ({
  102. appId: '',
  103. panelWidth: localStorage.getItem('workflow-node-panel-width') ? parseFloat(localStorage.getItem('workflow-node-panel-width')!) : 420,
  104. workflowRunningData: undefined,
  105. setWorkflowRunningData: workflowRunningData => set(() => ({ workflowRunningData })),
  106. historyWorkflowData: undefined,
  107. setHistoryWorkflowData: historyWorkflowData => set(() => ({ historyWorkflowData })),
  108. showRunHistory: false,
  109. setShowRunHistory: showRunHistory => set(() => ({ showRunHistory })),
  110. showFeaturesPanel: false,
  111. setShowFeaturesPanel: showFeaturesPanel => set(() => ({ showFeaturesPanel })),
  112. helpLineHorizontal: undefined,
  113. setHelpLineHorizontal: helpLineHorizontal => set(() => ({ helpLineHorizontal })),
  114. helpLineVertical: undefined,
  115. setHelpLineVertical: helpLineVertical => set(() => ({ helpLineVertical })),
  116. draftUpdatedAt: 0,
  117. setDraftUpdatedAt: draftUpdatedAt => set(() => ({ draftUpdatedAt: draftUpdatedAt ? draftUpdatedAt * 1000 : 0 })),
  118. publishedAt: 0,
  119. setPublishedAt: publishedAt => set(() => ({ publishedAt: publishedAt ? publishedAt * 1000 : 0 })),
  120. showInputsPanel: false,
  121. setShowInputsPanel: showInputsPanel => set(() => ({ showInputsPanel })),
  122. inputs: {},
  123. setInputs: inputs => set(() => ({ inputs })),
  124. files: [],
  125. setFiles: files => set(() => ({ files })),
  126. backupDraft: undefined,
  127. setBackupDraft: backupDraft => set(() => ({ backupDraft })),
  128. notInitialWorkflow: false,
  129. setNotInitialWorkflow: notInitialWorkflow => set(() => ({ notInitialWorkflow })),
  130. nodesDefaultConfigs: {},
  131. setNodesDefaultConfigs: nodesDefaultConfigs => set(() => ({ nodesDefaultConfigs })),
  132. nodeAnimation: false,
  133. setNodeAnimation: nodeAnimation => set(() => ({ nodeAnimation })),
  134. isRestoring: false,
  135. setIsRestoring: isRestoring => set(() => ({ isRestoring })),
  136. debouncedSyncWorkflowDraft: debounce((syncWorkflowDraft) => {
  137. syncWorkflowDraft()
  138. }, 5000),
  139. buildInTools: [],
  140. setBuildInTools: buildInTools => set(() => ({ buildInTools })),
  141. customTools: [],
  142. setCustomTools: customTools => set(() => ({ customTools })),
  143. clipboardElements: [],
  144. setClipboardElements: clipboardElements => set(() => ({ clipboardElements })),
  145. shortcutsDisabled: false,
  146. setShortcutsDisabled: shortcutsDisabled => set(() => ({ shortcutsDisabled })),
  147. showDebugAndPreviewPanel: false,
  148. setShowDebugAndPreviewPanel: showDebugAndPreviewPanel => set(() => ({ showDebugAndPreviewPanel })),
  149. selection: null,
  150. setSelection: selection => set(() => ({ selection })),
  151. bundleNodeSize: null,
  152. setBundleNodeSize: bundleNodeSize => set(() => ({ bundleNodeSize })),
  153. controlMode: localStorage.getItem('workflow-operation-mode') === 'pointer' ? 'pointer' : 'hand',
  154. setControlMode: (controlMode) => {
  155. set(() => ({ controlMode }))
  156. localStorage.setItem('workflow-operation-mode', controlMode)
  157. },
  158. candidateNode: undefined,
  159. setCandidateNode: candidateNode => set(() => ({ candidateNode })),
  160. panelMenu: undefined,
  161. setPanelMenu: panelMenu => set(() => ({ panelMenu })),
  162. nodeMenu: undefined,
  163. setNodeMenu: nodeMenu => set(() => ({ nodeMenu })),
  164. mousePosition: { pageX: 0, pageY: 0, elementX: 0, elementY: 0 },
  165. setMousePosition: mousePosition => set(() => ({ mousePosition })),
  166. syncWorkflowDraftHash: '',
  167. setSyncWorkflowDraftHash: syncWorkflowDraftHash => set(() => ({ syncWorkflowDraftHash })),
  168. }))
  169. }
  170. export function useStore<T>(selector: (state: Shape) => T): T {
  171. const store = useContext(WorkflowContext)
  172. if (!store)
  173. throw new Error('Missing WorkflowContext.Provider in the tree')
  174. return useZustandStore(store, selector)
  175. }
  176. export const useWorkflowStore = () => {
  177. return useContext(WorkflowContext)!
  178. }