@@ -27,12 +27,15 @@ import FlowNode from 'components/atoms/flow/FlowNode';
2727import { Popover } from '@headlessui/react' ;
2828import { generateFlowData } from './flowtestai' ;
2929import { GENAI_MODELS } from 'constants/Common' ;
30+ import useCanvasStore from 'stores/CanvasStore' ;
31+
32+ import { shallow } from 'zustand/shallow' ;
3033
3134const StartNode = ( ) => (
3235 < FlowNode title = 'Start' handleLeft = { false } handleRight = { true } handleRightData = { { type : 'source' } } > </ FlowNode >
3336) ;
3437
35- const init = ( flowData ) => {
38+ export const init = ( flowData ) => {
3639 // Initialization
3740 if ( flowData && flowData . nodes && flowData . edges ) {
3841 return {
@@ -95,36 +98,49 @@ const init = (flowData) => {
9598 }
9699} ;
97100
98- const Flow = ( { tabId, collectionId, flowData } ) => {
99- useEffect ( ( ) => {
100- // Action to perform on tab change
101- console . log ( `Tab changed to: ${ tabId } ` ) ;
102- console . log ( flowData ) ;
103- // perform actions based on the new tabId
104- const result = init ( cloneDeep ( flowData ) ) ;
105- setNodes ( result . nodes ) ;
106- setEdges ( result . edges ) ;
107- } , [ tabId ] ) ;
108-
109- const setCanvasDirty = ( ) => {
110- console . debug ( 'set canvas dirty' ) ;
111- const tab = useTabStore . getState ( ) . tabs . find ( ( t ) => t . id === tabId ) ;
112- if ( tab ) {
113- tab . isDirty = true ;
114- tab . flowData = {
115- nodes : nodes . map ( ( node ) => {
116- const _node = JSON . parse ( JSON . stringify ( node ) ) ;
117- return { ..._node } ;
118- } ) ,
119- edges : edges . map ( ( edge ) => {
120- return {
121- ...edge ,
122- animated : false ,
123- } ;
124- } ) ,
125- } ;
126- }
127- } ;
101+ const selector = ( state ) => ( {
102+ nodes : state . nodes ,
103+ edges : state . edges ,
104+ onNodesChange : state . onNodesChange ,
105+ onEdgesChange : state . onEdgesChange ,
106+ onConnect : state . onConnect ,
107+ setNodes : state . setNodes ,
108+ setEdges : state . setEdges ,
109+ } ) ;
110+
111+ const Flow = ( { collectionId } ) => {
112+ const { nodes, edges, onNodesChange, onEdgesChange, onConnect, setNodes, setEdges } = useCanvasStore ( selector ) ;
113+ //console.log(nodes);
114+
115+ // useEffect(() => {
116+ // // Action to perform on tab change
117+ // console.log(`Tab changed to: ${tabId}`);
118+ // console.log(flowData);
119+ // // perform actions based on the new tabId
120+ // const result = init(cloneDeep(flowData));
121+ // setNodes(result.nodes);
122+ // setEdges(result.edges);
123+ // }, [tabId]);
124+
125+ // const setCanvasDirty = () => {
126+ // console.debug('set canvas dirty');
127+ // const tab = useTabStore.getState().tabs.find((t) => t.id === tabId);
128+ // if (tab) {
129+ // tab.isDirty = true;
130+ // tab.flowData = {
131+ // nodes: nodes.map((node) => {
132+ // const _node = JSON.parse(JSON.stringify(node));
133+ // return { ..._node };
134+ // }),
135+ // edges: edges.map((edge) => {
136+ // return {
137+ // ...edge,
138+ // animated: false,
139+ // };
140+ // }),
141+ // };
142+ // }
143+ // };
128144
129145 // notification
130146 // eslint-disable-next-line no-unused-vars
@@ -151,28 +167,20 @@ const Flow = ({ tabId, collectionId, flowData }) => {
151167 [ ] ,
152168 ) ;
153169
154- const [ nodes , setNodes , onNodesChange ] = useNodesState ( [ ] ) ;
155- const [ edges , setEdges , onEdgesChange ] = useEdgesState ( [ ] ) ;
156-
157- useEffect ( ( ) => {
158- // skip inital render
159- if ( flowData === undefined || ( isEqual ( nodes , [ ] ) && isEqual ( edges , [ ] ) ) ) {
160- return ;
161- }
162- if ( flowData && isEqual ( JSON . parse ( JSON . stringify ( nodes ) ) , flowData . nodes ) && isEqual ( edges , flowData . edges ) ) {
163- console . debug ( 'canvas is unchanged' ) ;
164- return ;
165- }
166- setCanvasDirty ( ) ;
167- } , [ nodes , edges ] ) ;
168-
169- const onConnect = ( params ) => {
170- const newEdge = {
171- ...params ,
172- type : 'buttonedge' ,
173- } ;
174- setEdges ( ( eds ) => addEdge ( newEdge , eds ) ) ;
175- } ;
170+ // const [nodes, setNodes, onNodesChange] = useNodesState([]);
171+ // const [edges, setEdges, onEdgesChange] = useEdgesState([]);
172+
173+ // useEffect(() => {
174+ // // skip inital render
175+ // if (flowData === undefined || (isEqual(nodes, []) && isEqual(edges, []))) {
176+ // return;
177+ // }
178+ // if (flowData && isEqual(JSON.parse(JSON.stringify(nodes)), flowData.nodes) && isEqual(edges, flowData.edges)) {
179+ // console.debug('canvas is unchanged');
180+ // return;
181+ // }
182+ // setCanvasDirty();
183+ // }, [nodes, edges]);
176184
177185 const runnableEdges = ( runnable ) => {
178186 const updatedEdges = reactFlowInstance . getEdges ( ) . map ( ( edge ) => {
@@ -220,7 +228,7 @@ const Flow = ({ tabId, collectionId, flowData }) => {
220228 } ;
221229 console . debug ( 'Dropped node: ' , newNode ) ;
222230
223- setNodes ( ( nds ) => nds . concat ( newNode ) ) ;
231+ setNodes ( [ ... useCanvasStore . getState ( ) . nodes , newNode ] ) ;
224232 } ,
225233 [ reactFlowInstance ] ,
226234 ) ;
@@ -279,7 +287,7 @@ const Flow = ({ tabId, collectionId, flowData }) => {
279287 onInit = { setReactFlowInstance }
280288 onDrop = { onDrop }
281289 onDragOver = { onDragOver }
282- onNodeDragStop = { ( ) => setCanvasDirty ( ) }
290+ // onNodeDragStop={() => setCanvasDirty()}
283291 isValidConnection = { isValidConnection }
284292 //fitView
285293 >
0 commit comments