-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGraphContext.tsx
179 lines (155 loc) · 5.84 KB
/
GraphContext.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
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
// Graph/GraphContext.tsx
import React, { createContext, useState, useContext, useCallback, ReactNode } from 'react';
import { Node, Edge, NodeChange, applyNodeChanges, EdgeChange, applyEdgeChanges } from '@xyflow/react';
export interface SubGraph {
graphName: string;
nodes: Node[];
edges: Edge[];
serial_number: number;
}
export interface GraphContextType {
subGraphs: SubGraph[];
currentGraphName: string;
setCurrentGraphName: (graphName: string) => void;
getCurrentGraph: () => SubGraph;
addSubGraph: (graphName: string) => void;
updateSubGraph: (graphName: string, updatedGraph: SubGraph) => void;
removeSubGraph: (graphName: string) => void;
updateNodeData: (graphName: string, nodeId: string, newData: any) => void;
handleNodesChange: (graphName: string, changes: NodeChange[]) => void;
handleEdgesChange: (graphName: string, changes: EdgeChange[]) => void;
}
const initialGraphData = {
graphName: "root",
nodes: [],
edges: [],
serial_number: 1,
};
const GraphContext = createContext<GraphContextType | undefined>(undefined);
export const GraphProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const [subGraphs, setSubGraphs] = useState<SubGraph[]>([]);
const [currentGraphName, setCurrentGraphNameState] = useState<string>("root");
const getCurrentGraph = useCallback(():SubGraph => {
const currentGraph = subGraphs.find(graph => graph.graphName === currentGraphName);
return currentGraph || {
graphName: "root",
nodes: [],
edges: [],
serial_number: 1,
};
}, [subGraphs, currentGraphName]);
const addSubGraph = (graphName: string) => {
setSubGraphs(prevGraphs => {
const graphIndex = prevGraphs.findIndex(graph => graph.graphName === graphName);
if(graphIndex === -1){
const newSubgraphs = [...prevGraphs, {
graphName,
nodes: [],
edges: [],
serial_number: 1,
}]
if(!currentGraphName) setCurrentGraphNameState(graphName);
return newSubgraphs;
} else {
return prevGraphs; // Do nothing, just return the previous state
}
});
};
const updateSubGraph = (graphName: string, updatedGraph: SubGraph) => {
setSubGraphs(prevGraphs => {
const graphIndex = prevGraphs.findIndex(graph => graph.graphName === graphName);
if (graphIndex === -1) {
return [...prevGraphs, updatedGraph]
} else {
const updateGraph = prevGraphs.map((graph, index) => index === graphIndex ? updatedGraph : graph)
if(currentGraphName === graphName) {
setCurrentGraphNameState(updatedGraph.graphName);
}
return updateGraph;
}
});
};
const removeSubGraph = (graphName: string) => {
setSubGraphs(prevGraphs => prevGraphs.filter(graph => graph.graphName !== graphName));
if (currentGraphName === graphName) {
setCurrentGraphNameState("root")
}
};
const setCurrentGraphName = (graphName: string) => {
setCurrentGraphNameState(graphName);
};
const updateNodeData = (graphName: string, nodeId: string, newData: any) => {
setSubGraphs(prevGraphs => {
return prevGraphs.map(graph => {
if(graph.graphName === graphName){
return {
...graph,
nodes: graph.nodes.map(node =>{
if(node.id === nodeId){
return {
...node,
data: {...node.data, ...newData}
}
}
return node;
})
}
}
return graph;
})
})
}
const handleNodesChange = useCallback((graphName: string, changes: NodeChange[]) => {
setSubGraphs((prevGraphs) => {
return prevGraphs.map(graph => {
if(graph.graphName === graphName){
const updatedNodes = applyNodeChanges(changes, graph.nodes);
return { ...graph, nodes: updatedNodes };
}
return graph;
})
})
}, []);
const handleEdgesChange = useCallback((graphName: string, changes: EdgeChange[]) => {
setSubGraphs((prevGraphs) => {
return prevGraphs.map(graph => {
if(graph.graphName === graphName){
const updatedEdges = applyEdgeChanges(changes, graph.edges);
return { ...graph, edges: updatedEdges };
}
return graph;
})
})
}, []);
//Initialize root graph if not exist
React.useEffect(()=>{
const rootGraphExist = subGraphs.find(graph => graph.graphName === "root")
if(!rootGraphExist){
setSubGraphs([{...initialGraphData}]);
}
}, [subGraphs])
const value = {
subGraphs,
currentGraphName,
setCurrentGraphName,
getCurrentGraph,
addSubGraph,
updateSubGraph,
removeSubGraph,
updateNodeData,
handleNodesChange,
handleEdgesChange,
};
return (
<GraphContext.Provider value={value}>
{children}
</GraphContext.Provider>
);
};
export const useGraph = () => {
const context = useContext(GraphContext);
if (!context) {
throw new Error('useGraph must be used within a GraphProvider');
}
return context;
};