Skip to content

Commit 4c566b5

Browse files
committed
Validatate connections
1 parent b8acf38 commit 4c566b5

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"homepage": "https://blibliki-js.github.io/grid",
66
"dependencies": {
7-
"@blibliki/engine": "^0.1.25",
7+
"@blibliki/engine": "^0.1.26",
88
"@clerk/nextjs": "^4.29.7",
99
"@radix-ui/react-context-menu": "^2.1.5",
1010
"@radix-ui/react-dialog": "^1.0.5",

pnpm-lock.yaml

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Grid/index.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ const DEFAULT_REACT_FLOW_PROPS = {
1919
};
2020

2121
export default function Grid() {
22-
const { nodes, edges, viewport, onNodesChange, onEdgesChange, onConnect } =
23-
useGridNodes();
22+
const {
23+
nodes,
24+
edges,
25+
viewport,
26+
onNodesChange,
27+
onEdgesChange,
28+
onConnect,
29+
isValidConnection,
30+
} = useGridNodes();
2431
const { onDrop, onDragOver } = useDrag();
2532

2633
return (
@@ -35,6 +42,7 @@ export default function Grid() {
3542
minZoom={0.3}
3643
onDrop={onDrop}
3744
onDragOver={onDragOver}
45+
isValidConnection={isValidConnection}
3846
proOptions={DEFAULT_REACT_FLOW_PROPS}
3947
>
4048
<Controls className="dark:bg-gray-500" />

src/hooks/index.ts

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { TypedUseSelectorHook } from "react-redux";
66
import { useCallback, useEffect, useState } from "react";
77
import { useAuth, useUser } from "@clerk/nextjs";
88
import { getAuth, signInWithCustomToken } from "firebase/auth";
9+
import Engine from "@blibliki/engine";
910

1011
import type { RootState, AppDispatch } from "@/store";
1112
import {
@@ -96,6 +97,18 @@ export function useGridNodes() {
9697
[dispatch],
9798
);
9899

100+
const isValidConnection = useCallback((connection: Connection): boolean => {
101+
const { source, sourceHandle, target, targetHandle } = connection;
102+
if (!source || !sourceHandle || !target || !targetHandle) return false;
103+
104+
return Engine.validRoute({
105+
sourceId: source,
106+
sourceIOId: sourceHandle,
107+
destinationId: target,
108+
destinationIOId: targetHandle,
109+
});
110+
}, []);
111+
99112
return {
100113
nodes,
101114
edges,
@@ -104,6 +117,7 @@ export function useGridNodes() {
104117
onNodesChange,
105118
onEdgesChange,
106119
onConnect,
120+
isValidConnection,
107121
};
108122
}
109123

0 commit comments

Comments
 (0)