Skip to content

Commit

Permalink
Merge pull request #131 from softflow24/itsWindows11/fix-invalid-conn…
Browse files Browse the repository at this point in the history
…ections

Fixed invalid connections being made
  • Loading branch information
c0rtexR authored Oct 14, 2024
2 parents 4b8dc4a + 798873b commit 4df8b08
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/editor/src/hooks/useReactFlowEventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppDispatch>();
Expand Down Expand Up @@ -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],
Expand Down
17 changes: 17 additions & 0 deletions packages/editor/src/utils/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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,
);

return (
connection.sourceHandle !== connection.targetHandle &&
!circularConnectionExists
);
}

0 comments on commit 4df8b08

Please sign in to comment.