new function to clean invalid edges of a flow
This commit is contained in:
parent
ba61b0e9e0
commit
86bdec36e6
2 changed files with 57 additions and 0 deletions
10
src/frontend/src/types/utils/reactflowUtils.ts
Normal file
10
src/frontend/src/types/utils/reactflowUtils.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Edge } from "reactflow";
|
||||
import { NodeType } from "../flow";
|
||||
|
||||
export type cleanEdgesType = {
|
||||
flow: {
|
||||
edges: Edge[];
|
||||
nodes: NodeType[];
|
||||
};
|
||||
updateEdge: (edge: Edge[]) => void;
|
||||
};
|
||||
47
src/frontend/src/util/reactflowUtils.ts
Normal file
47
src/frontend/src/util/reactflowUtils.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import _ from "lodash";
|
||||
import { cleanEdgesType } from "./../types/utils/reactflowUtils";
|
||||
|
||||
export function cleanEdges({
|
||||
flow: { edges, nodes },
|
||||
updateEdge,
|
||||
}: cleanEdgesType) {
|
||||
let newEdges = _.cloneDeep(edges);
|
||||
console.log("cleanEdges", newEdges);
|
||||
edges.forEach((edge) => {
|
||||
// check if the source and target node still exists
|
||||
const sourceNode = nodes.find((node) => node.id === edge.source);
|
||||
const targetNode = nodes.find((node) => node.id === edge.target);
|
||||
if (!sourceNode || !targetNode) {
|
||||
newEdges = newEdges.filter((e) => e.id !== edge.id);
|
||||
}
|
||||
// check if the source and target handle still exists
|
||||
if (sourceNode && targetNode) {
|
||||
const sourceHandle = edge.sourceHandle; //right
|
||||
const targetHandle = edge.targetHandle; //left
|
||||
if (targetHandle) {
|
||||
const field = targetHandle.split("|")[1];
|
||||
const id =
|
||||
(targetNode.data.node.template[field].input_types?.join(";") ??
|
||||
targetNode.data.node.template[field].type) +
|
||||
"|" +
|
||||
field +
|
||||
"|" +
|
||||
targetNode.data.id;
|
||||
if (id !== targetHandle) {
|
||||
newEdges = newEdges.filter((e) => e.id !== edge.id);
|
||||
}
|
||||
}
|
||||
if (sourceHandle) {
|
||||
const id = [
|
||||
sourceNode.data.type,
|
||||
sourceNode.data.id,
|
||||
...sourceNode.data.node.base_classes,
|
||||
].join("|");
|
||||
if (id !== sourceHandle) {
|
||||
newEdges = newEdges.filter((e) => e.id !== edge.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
updateEdge(newEdges);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue