Spaces:
Build error
Build error
File size: 1,224 Bytes
a8b3f00 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import { useCallback } from 'react'
import produce from 'immer'
import { useStoreApi } from 'reactflow'
import { useNodesSyncDraft } from './use-nodes-sync-draft'
import { useNodesReadOnly } from './use-workflow'
type NodeDataUpdatePayload = {
id: string
data: Record<string, any>
}
export const useNodeDataUpdate = () => {
const store = useStoreApi()
const { handleSyncWorkflowDraft } = useNodesSyncDraft()
const { getNodesReadOnly } = useNodesReadOnly()
const handleNodeDataUpdate = useCallback(({ id, data }: NodeDataUpdatePayload) => {
const {
getNodes,
setNodes,
} = store.getState()
const newNodes = produce(getNodes(), (draft) => {
const currentNode = draft.find(node => node.id === id)!
if (currentNode)
currentNode.data = { ...currentNode.data, ...data }
})
setNodes(newNodes)
}, [store])
const handleNodeDataUpdateWithSyncDraft = useCallback((payload: NodeDataUpdatePayload) => {
if (getNodesReadOnly())
return
handleNodeDataUpdate(payload)
handleSyncWorkflowDraft()
}, [handleSyncWorkflowDraft, handleNodeDataUpdate, getNodesReadOnly])
return {
handleNodeDataUpdate,
handleNodeDataUpdateWithSyncDraft,
}
}
|