From 41aec50819fa77f08b6473b43f63b2160bc15422 Mon Sep 17 00:00:00 2001 From: igorrCarvalho Date: Mon, 15 Jan 2024 17:46:24 -0300 Subject: [PATCH] Refactor: Change onConnection function to zustand store --- .../components/PageComponent/index.tsx | 38 +------------------ src/frontend/src/stores/flowStore.ts | 35 ++++++++++++++++- src/frontend/src/types/zustand/flow/index.ts | 2 + 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/frontend/src/pages/FlowPage/components/PageComponent/index.tsx b/src/frontend/src/pages/FlowPage/components/PageComponent/index.tsx index 2e2c2bac7..57716b5b5 100644 --- a/src/frontend/src/pages/FlowPage/components/PageComponent/index.tsx +++ b/src/frontend/src/pages/FlowPage/components/PageComponent/index.tsx @@ -78,6 +78,7 @@ export default function Page({ const setLastCopiedSelection = useFlowStore( (state) => state.setLastCopiedSelection ); + const onConnect = useFlowStore((state) => state.onConnect); const position = useRef({ x: 0, y: 0 }); const [lastSelection, setLastSelection] = @@ -215,43 +216,6 @@ export default function Page({ } }, []); - const onConnect = useCallback( - (connection: Connection) => { - let newEdges:Edge[] = [] - setEdges((oldEdges) => { - newEdges = addEdge( - { - ...connection, - data: { - targetHandle: scapeJSONParse(connection.targetHandle!), - sourceHandle: scapeJSONParse(connection.sourceHandle!), - }, - style: { stroke: "#555" }, - className: - ((scapeJSONParse(connection.targetHandle!) as targetHandleType) - .type === "Text" - ? "stroke-foreground " - : "stroke-foreground ") + " stroke-connection", - animated: - (scapeJSONParse(connection.targetHandle!) as targetHandleType) - .type === "Text", - }, - oldEdges - ); - return newEdges; - - }) - useFlowsManagerStore - .getState() - .autoSaveCurrentFlow( - nodes, - newEdges, - reactFlowInstance?.getViewport() ?? { x: 0, y: 0, zoom: 1 } - ); - }, - [nodes, setEdges, reactFlowInstance, addEdge] - ); - const onDrop = useCallback( (event: React.DragEvent) => { event.preventDefault(); diff --git a/src/frontend/src/stores/flowStore.ts b/src/frontend/src/stores/flowStore.ts index cf041ee0f..d8338428b 100644 --- a/src/frontend/src/stores/flowStore.ts +++ b/src/frontend/src/stores/flowStore.ts @@ -263,7 +263,40 @@ const useFlowStore = create((set, get) => ({ setFilterEdge: (newState) => { set({ getFilterEdge: newState }); }, - getFilterEdge: [] + getFilterEdge: [], + onConnect: (connection) => { + let newEdges: Edge[] = [] + get().setEdges((oldEdges) => { + newEdges = addEdge( + { + ...connection, + data: { + targetHandle: scapeJSONParse(connection.targetHandle!), + sourceHandle: scapeJSONParse(connection.sourceHandle!), + }, + style: { stroke: "#555" }, + className: + ((scapeJSONParse(connection.targetHandle!) as targetHandleType) + .type === "Text" + ? "stroke-foreground " + : "stroke-foreground ") + " stroke-connection", + animated: + (scapeJSONParse(connection.targetHandle!) as targetHandleType) + .type === "Text", + }, + oldEdges + ); + return newEdges; + + }) + useFlowsManagerStore + .getState() + .autoSaveCurrentFlow( + get().nodes, + newEdges, + get().reactFlowInstance?.getViewport() ?? { x: 0, y: 0, zoom: 1 } + ); + }, })); export default useFlowStore; diff --git a/src/frontend/src/types/zustand/flow/index.ts b/src/frontend/src/types/zustand/flow/index.ts index 9b76a5347..273a1958f 100644 --- a/src/frontend/src/types/zustand/flow/index.ts +++ b/src/frontend/src/types/zustand/flow/index.ts @@ -1,4 +1,5 @@ import { + Connection, Edge, Node, OnEdgesChange, @@ -52,4 +53,5 @@ export type FlowStoreType = { cleanFlow: () => void; setFilterEdge: (newState) => void; getFilterEdge: any[]; + onConnect: (connection: Connection) => void; };