From f36984b195e40a59c14a2641895e3c90f33c92de Mon Sep 17 00:00:00 2001 From: itsWindows11 Date: Mon, 14 Oct 2024 11:59:00 +0300 Subject: [PATCH 1/2] Fixed invalid connections being made --- .../src/hooks/useReactFlowEventHandlers.ts | 4 ++++ packages/editor/src/utils/validation.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 packages/editor/src/utils/validation.ts diff --git a/packages/editor/src/hooks/useReactFlowEventHandlers.ts b/packages/editor/src/hooks/useReactFlowEventHandlers.ts index 369512b..b2464f1 100644 --- a/packages/editor/src/hooks/useReactFlowEventHandlers.ts +++ b/packages/editor/src/hooks/useReactFlowEventHandlers.ts @@ -29,6 +29,7 @@ import { import { useReactFlowState } from "@hooks/useReactFlowState"; import { setIsRightPanelVisible } from "@slices/layoutSlice"; import { useHotkeys } from "react-hotkeys-hook"; +import { isValidConnection } from "@/utils/validation"; export const useReactFlowEventHandlers = () => { const dispatch = useDispatch(); @@ -63,6 +64,9 @@ export const useReactFlowEventHandlers = () => { const onConnect: OnConnect = useCallback( (connection) => { const newEdges = addEdge(connection, edges); + + if (!isValidConnection(connection, newEdges)) return; + dispatch(setEdges(newEdges)); }, [edges, dispatch], diff --git a/packages/editor/src/utils/validation.ts b/packages/editor/src/utils/validation.ts new file mode 100644 index 0000000..de2018d --- /dev/null +++ b/packages/editor/src/utils/validation.ts @@ -0,0 +1,18 @@ +import { Connection, Edge } from "reactflow"; + +export function isValidConnection( + connection: Connection, + newEdges: Edge[], +): boolean { + const circularConnectionExists = newEdges.some( + (edge) => + edge.sourceHandle === connection.targetHandle || + edge.targetHandle === connection.sourceHandle || + connection.sourceHandle === connection.targetHandle, + ); + + return ( + connection.sourceHandle !== connection.targetHandle && + !circularConnectionExists + ); +} From 7fa0e283d4a40135e21dfb0d9cfc2275e2b787b9 Mon Sep 17 00:00:00 2001 From: itsWindows11 Date: Mon, 14 Oct 2024 12:01:04 +0300 Subject: [PATCH 2/2] Removed redundant check from `some` predicate --- packages/editor/src/utils/validation.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/editor/src/utils/validation.ts b/packages/editor/src/utils/validation.ts index de2018d..b9d6ba8 100644 --- a/packages/editor/src/utils/validation.ts +++ b/packages/editor/src/utils/validation.ts @@ -7,8 +7,7 @@ export function isValidConnection( const circularConnectionExists = newEdges.some( (edge) => edge.sourceHandle === connection.targetHandle || - edge.targetHandle === connection.sourceHandle || - connection.sourceHandle === connection.targetHandle, + edge.targetHandle === connection.sourceHandle, ); return (