-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent cyclic connections between nodes
- Loading branch information
1 parent
b7583d2
commit 55373e8
Showing
5 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Node, Edge } from '@xyflow/react' | ||
|
||
export const isTargetAncestorOfSource = (sourceId: string, targetId: string, nodes: Node[], edges: Edge[]): boolean => { | ||
if (!sourceId || !targetId) { | ||
return false | ||
} | ||
if (sourceId === targetId) { | ||
return true | ||
} | ||
|
||
// Find all outgoing edges from the target node | ||
const outgoingEdges = edges.filter((edge) => edge.source === targetId) | ||
if (outgoingEdges.length === 0) { | ||
return false | ||
} | ||
|
||
// Get all child nodes | ||
const childNodes = outgoingEdges.map((edge) => nodes.find((node) => node.id === edge.target)).filter(Boolean) | ||
if (childNodes.length === 0) { | ||
return false | ||
} | ||
|
||
// Check if any child node is the source node | ||
return childNodes.some((node) => isTargetAncestorOfSource(sourceId, node.id, nodes, edges)) | ||
} |