File size: 1,531 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { DBEdge, DBNode } from "app-types/workflow";
import { NodeKind } from "../workflow.interface";

export function addEdgeBranchLabel(nodes: DBNode[], edges: DBEdge[]) {
  const outs = (id: string) => edges.filter((e) => e.source === id);
  const start = nodes.find((n) => n.kind === NodeKind.Input)!;
  const q: { id: string; bid: string }[] = [{ id: start.id, bid: "B0" }];

  while (q.length) {
    const { id, bid } = q.shift()!;
    const node = nodes.find((n) => n.id === id)!;
    const nexts = outs(id);

    if (node.kind === NodeKind.Condition) {
      const byHandle = new Map<string, DBEdge[]>();
      nexts.forEach((e) => {
        const h = e.uiConfig.sourceHandle ?? "right";
        (byHandle.get(h) ?? byHandle.set(h, []).get(h))!.push(e);
      });
      byHandle.forEach((group) => {
        if (group.length === 1) {
          const [e] = group;
          if (!e.uiConfig.label) {
            e.uiConfig.label = bid;
            q.push({ id: e.target, bid });
          }
        } else {
          group.forEach((e, i) => {
            const newBid = `${bid}.${i}`;
            if (!e.uiConfig.label) {
              e.uiConfig.label = newBid;
              q.push({ id: e.target, bid: newBid });
            }
          });
        }
      });
    } else {
      nexts.forEach((e, i) => {
        const newBid = nexts.length > 1 ? `${bid}.${i}` : bid;
        if (!e.uiConfig.label) {
          e.uiConfig.label = newBid;
          q.push({ id: e.target, bid: newBid });
        }
      });
    }
  }
}