feat: build react flow nodes and edges from mock data #918 (#919)

### What problem does this PR solve?
feat: build react flow nodes and edges from mock data #918

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu
2024-05-27 08:21:30 +08:00
committed by GitHub
parent 20b57144b0
commit 55fb96131e
8 changed files with 129 additions and 31 deletions

View File

@@ -0,0 +1,44 @@
import { DSLComponents } from '@/interfaces/database/flow';
import { Edge, Node, Position } from 'reactflow';
import { v4 as uuidv4 } from 'uuid';
export const buildNodesFromDSLComponents = (data: DSLComponents) => {
const nodes: Node[] = [];
const edges: Edge[] = [];
Object.entries(data).forEach(([key, value]) => {
const downstream = [...value.downstream];
const upstream = [...value.upstream];
nodes.push({
id: key,
type: 'textUpdater',
position: { x: 0, y: 0 },
data: {
label: value.obj.component_name,
params: value.obj.params,
downstream: downstream,
upstream: upstream,
},
sourcePosition: Position.Left,
targetPosition: Position.Right,
});
// intermediate node
// The first and last nodes do not need to be considered
if (upstream.length > 0 && downstream.length > 0) {
for (let i = 0; i < upstream.length; i++) {
const up = upstream[i];
for (let j = 0; j < downstream.length; j++) {
const down = downstream[j];
edges.push({
id: uuidv4(),
label: '',
type: 'step',
source: up,
target: down,
});
}
}
}
});
};