-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJsonUtil.tsx
129 lines (112 loc) · 3.8 KB
/
JsonUtil.tsx
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Graph/JsonUtil.tsx
import { ReactToJsonNode, ReactNodeProps, ReactFlowNodeEXT, JsonToReactNode, JsonNodeData } from './NodeData';
import { SubGraph } from './GraphContext';
import { Node, Edge } from '@xyflow/react';
// Type guard to check if an object is a ReactFlowNodeEXT
function isReactFlowNodeEXT(data: any): data is ReactFlowNodeEXT {
return (
typeof data === 'object' &&
data !== null &&
typeof (data as ReactFlowNodeEXT).type === 'string'
);
}
export const subGraphToJson = (subGraph: SubGraph) => {
const jsonNodes = subGraph.nodes.map(node => {
let nodeData: ReactFlowNodeEXT;
if (isReactFlowNodeEXT(node.data)) {
nodeData = node.data;
} else {
// Handle the case where node.data is not a ReactFlowNodeEXT
console.error("Invalid node data:", node.data);
nodeData = { type: "STEP" }; // Providing default values to avoid potential errors
}
const reactNodeProps: ReactNodeProps = {
id: node.id,
width: node.width || 200,
height: node.height || 200,
position: node.position,
data: nodeData,
};
return ReactToJsonNode(reactNodeProps);
});
return {
name: subGraph.graphName,
nodes: jsonNodes,
serial_number: subGraph.serial_number
}
};
export const allSubGraphsToJson = (subGraphs: SubGraph[]) => {
return subGraphs.map(subGraphToJson);
};
export interface JsonSubGraph {
name: string;
nodes: JsonNodeData[];
serial_number: number;
}
export const jsonToSubGraph = (json: JsonSubGraph): SubGraph => {
const nodes: Node[] = json.nodes.map(nodeJson => {
const reactNodeProps = JsonToReactNode(nodeJson, { x: nodeJson.ext?.pos_x || 0, y: nodeJson.ext?.pos_y || 0 });
const { data, ...rest } = reactNodeProps;
return {
type: 'custom',
...rest,
data: data as unknown as Record<string, unknown>,
};
});
const edges: Edge[] = [];
nodes.forEach(node => {
const nodeData = node.data as any;
if (nodeData.nexts && Array.isArray(nodeData.nexts)) {
nodeData.nexts.forEach((nextId:string) => {
const newEdge: Edge = {
id: `${node.id}-${nextId}`,
source: node.id,
target: nextId,
type: 'custom',
data: {
sourceNode: node.id,
targetNode: nextId
}
};
edges.push(newEdge);
});
}
if (nodeData.true_next) {
const newEdge: Edge = {
id: `${node.id}-${nodeData.true_next}-true`,
source: node.id,
target: nodeData.true_next,
sourceHandle: 'true',
type: 'custom',
data: {
sourceNode: node.id,
targetNode: nodeData.true_next
}
};
edges.push(newEdge);
}
if (nodeData.false_next) {
const newEdge: Edge = {
id: `${node.id}-${nodeData.false_next}-false`,
source: node.id,
target: nodeData.false_next,
sourceHandle: 'false',
type: 'custom',
data: {
sourceNode: node.id,
targetNode: nodeData.false_next
}
};
edges.push(newEdge);
}
});
return {
graphName: json.name,
nodes,
edges,
serial_number: json.serial_number,
};
};
export const jsonToSubGraphs = (jsonArray: JsonSubGraph[]): SubGraph[] => {
return jsonArray.map(jsonToSubGraph);
};