All files / src/utils/graph calculateData.ts

0% Statements 0/24
0% Branches 0/5
0% Functions 0/7
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                                                                                                     
import { graphlib, layout } from "@dagrejs/dagre";
import { GraphData } from "./fetchGraphData.ts";
 
export const calculateData = (
  graphData: GraphData,
  width: number,
  height: number,
) => {
  const { nodes, edges } = graphData;
 
  const g = new graphlib.Graph();
  g.setGraph({});
 
  g.setDefaultEdgeLabel(function () {
    return {};
  });
 
  nodes?.forEach((node) => {
    g.setNode(node.id, {
      label: node.id,
      width: node.type === "step" ? node.dimensions.width : 350,
      height: node.type === "step" ? node.dimensions.height : 200,
    });
  });
 
  edges?.forEach((edge) => {
    g.setEdge(edge.source, edge.target);
  });
 
  layout(g, {
    align: "center",
    width,
    height,
  });
 
  const graphNodes = g
    .nodes()
    .map((node) => {
      const graphNode = g.node(node);
      const newNode = nodes.find((e) => e.id === node);
      Iif (!newNode) return undefined;
      newNode.position.x = graphNode.x;
      newNode.position.y = graphNode.y;
 
      return newNode;
    })
    .filter((e) => e !== undefined);
 
  return { nodes: graphNodes, edges };
};