forked from LangGraph-GUI/LangGraph-GUI-reactflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonUtils.js
179 lines (160 loc) · 5.18 KB
/
JsonUtils.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// JsonUtils.js
import NodeData from './NodeData';
import { createEdge } from './Edge';
import { createConditionEdge } from './ConditionEdge';
// Convert nodes to a JSON object format
export const convertFlowToJson = (nodes, nodeIdCounter) => {
const nodesData = nodes.map((node) => {
// Create a unique set from nexts, then convert it back to an array
const uniqueNexts = Array.from(new Set(node.data.nexts || []));
const ext = {
pos_x: node.position.x,
pos_y: node.position.y,
width: node.data.width || 200,
height: node.data.height || 200,
info: node.data.info || '',
};
// Return a new node with the updated unique 'nexts' array
const nodeData = NodeData.fromReactFlowNode({
...node,
data: {
...node.data,
nexts: uniqueNexts,
},
});
return {
...nodeData.toDict(),
ext,
};
});
const flowData = {
nodes: nodesData,
node_counter: nodeIdCounter,
};
return flowData;
};
// Save the JSON object to a file
export const saveJsonToFile = (flowData) => {
try {
const blob = new Blob([JSON.stringify(flowData, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'flow.json';
document.body.appendChild(a); // Append the link to the body
a.click(); // Simulate a click on the link
document.body.removeChild(a); // Remove the link from the body
URL.revokeObjectURL(url); // Revoke the object URL
alert('Flow saved!');
} catch (error) {
console.error('Error saving JSON:', error);
alert('Failed to save flow.');
}
};
// Original saveJson function - keeps the original interface
export const saveJson = async (nodes, nodeIdCounter) => {
try {
// Convert nodes to JSON
const flowData = convertFlowToJson(nodes, nodeIdCounter);
// Save the JSON data to a file
saveJsonToFile(flowData);
} catch (error) {
console.error('Error in saveJson:', error);
alert('Failed to save flow.');
}
};
// Read and process JSON file
export const readJsonFile = (event) => {
return new Promise((resolve, reject) => {
const file = event.target.files[0];
if (!file) {
reject(new Error('No file selected.'));
return;
}
const reader = new FileReader();
reader.onload = (e) => {
try {
const contents = e.target.result;
resolve(JSON.parse(contents));
} catch (error) {
reject(new Error('Error parsing JSON.'));
}
};
reader.onerror = () => reject(new Error('Error reading file.'));
reader.readAsText(file);
});
};
// Process flow data
export const processFlowData = (flowData, setEdges, setNodes, setNodeIdCounter) => {
try {
const loadedNodes = (flowData.nodes || []).map((nodeData) => {
const node = NodeData.fromDict(nodeData);
// Create a new object that includes ext properties and updated width/height
return {
...node.toReactFlowNode(),
position: { x: nodeData.ext.pos_x, y: nodeData.ext.pos_y },
data: {
...node.toReactFlowNode().data,
width: nodeData.ext.width,
height: nodeData.ext.height,
info: nodeData.ext.info,
},
};
});
// First, set the nodes
setNodes(loadedNodes);
// Then, create edges
const loadedEdges = [];
loadedNodes.forEach((node) => {
node.data.nexts.forEach((nextId) => {
const newEdge = createEdge(loadedEdges, setEdges, { source: node.id, target: nextId }, loadedNodes, setNodes);
if (newEdge) {
loadedEdges.push(newEdge);
}
});
if (node.data.true_next) {
const newEdge = createConditionEdge(loadedEdges, setEdges, { source: node.id, target: node.data.true_next, sourceHandle: 'true' }, loadedNodes, setNodes);
if (newEdge) {
loadedEdges.push(newEdge);
}
}
if (node.data.false_next) {
const newEdge = createConditionEdge(loadedEdges, setEdges, { source: node.id, target: node.data.false_next, sourceHandle: 'false' }, loadedNodes, setNodes);
if (newEdge) {
loadedEdges.push(newEdge);
}
}
});
setEdges(loadedEdges);
// Set node counter
setNodeIdCounter(flowData.node_counter || 1);
} catch (error) {
console.error('Error processing JSON data:', error);
alert('Failed to process JSON data.');
}
};
// Load JSON file and process
export const loadJson = (setEdges, setNodes, setNodeIdCounter) => {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = '.json';
fileInput.style.display = 'none';
document.body.appendChild(fileInput);
fileInput.addEventListener('change', async (event) => {
try {
const flowData = await readJsonFile(event);
if (flowData) {
setNodes([]);
setEdges([]);
setNodeIdCounter(0);
processFlowData(flowData, setEdges, setNodes, setNodeIdCounter);
}
} catch (error) {
console.error('Error loading JSON:', error);
alert('Failed to load flow.');
} finally {
document.body.removeChild(fileInput); // Clean up
}
});
fileInput.click();
};