forked from ControlCore-Project/concore-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphWorkspace.jsx
More file actions
150 lines (140 loc) · 6.11 KB
/
Copy pathGraphWorkspace.jsx
File metadata and controls
150 lines (140 loc) · 6.11 KB
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
import React from 'react';
import ZoomComp from './component/ZoomSetter';
import ConfirmModal from './component/modals/ConfirmModal';
import SearchPanel from './component/SearchPanel';
import { actionType as T } from './reducer';
import './graphWorkspace.css';
import localStorageManager from './graph-builder/local-storage-manager';
import TabBar from './component/TabBar';
import Graph from './GraphArea';
const GraphComp = (props) => {
const graphContainerRef = React.useRef();
const { dispatcher, superState } = props;
React.useEffect(() => {
let mounted = true;
const restoreWorkspace = async () => {
await localStorageManager.initialize();
if (!mounted) return;
const session = localStorageManager.getSession();
const allIDs = Array.isArray(session?.openGraphIDs) && session.openGraphIDs.length
? session.openGraphIDs
: localStorageManager.getAllGraphs();
const validIDs = allIDs.filter(
(id) => typeof id === 'string' && id && localStorageManager.get(id) !== null,
);
if (validIDs.length !== allIDs.length) {
allIDs.filter((id) => !validIDs.includes(id)).forEach((id) => localStorageManager.remove(id));
}
if (Array.isArray(session?.fileState)) {
dispatcher({ type: T.SET_FILE_STATE, payload: session.fileState });
}
if (typeof session?.uploadedDirName === 'string') {
dispatcher({ type: T.SET_DIR_NAME, payload: session.uploadedDirName });
}
if (validIDs.length === 0) return;
dispatcher({
type: T.ADD_GRAPH_BULK,
payload: {
graphs: validIDs.map((graphID) => ({ graphID })),
activeGraphID: session?.activeGraphID || null,
},
});
};
restoreWorkspace();
return () => {
mounted = false;
};
}, []);
React.useEffect(() => {
const openGraphIDs = superState.graphs
.map((graph) => graph.graphID)
.filter((graphID) => typeof graphID === 'string' && graphID);
const activeGraphID = superState.curGraphIndex >= 0 && superState.curGraphIndex < superState.graphs.length
? superState.graphs[superState.curGraphIndex].graphID
: null;
localStorageManager.saveSession({
openGraphIDs,
activeGraphID,
fileState: superState.fileState,
uploadedDirName: superState.uploadedDirName,
});
}, [
superState.graphs,
superState.curGraphIndex,
superState.fileState,
superState.uploadedDirName,
]);
// Remote server implementation - Not being used.
// useEffect(() => {
// if (!loadedFromStorage) return;
// const graphFromParams = Object.fromEntries(new URLSearchParams(window.location.search).entries()).g;
// if (graphFromParams) {
// const graphContent = JSON.parse(window.atob(graphFromParams));
// const gid = new Date().getTime().toString();
// localStorageManager.addToFront(gid);
// localStorageManager.save(gid, graphContent);
// window.history.replaceState({}, document.title, window.location.pathname);
// dispatcher({ type: T.ADD_GRAPH, payload: { graphID: gid } });
// }
// const urlParms = window.location.pathname.split('/');
// const serverIDIndex = urlParms.indexOf('s');
// const localIDIndex = urlParms.indexOf('l');
// if (serverIDIndex !== -1 && serverIDIndex + 1 < urlParms.length) {
// const serverID = urlParms[serverIDIndex + 1];
// dispatcher({ type: T.ADD_GRAPH, payload: { serverID } });
// }
// if (localIDIndex !== -1 && localIDIndex + 1 < urlParms.length) {
// const graphID = urlParms[localIDIndex + 1];
// dispatcher({ type: T.ADD_GRAPH, payload: { graphID } });
// }
// }, [loadedFromStorage]);
return (
<div
style={{
flex: 1,
flexDirection: 'column',
display: 'flex',
width: '100%',
}}
>
<TabBar superState={superState} dispatcher={dispatcher} />
<div style={{ flex: 1, position: 'relative' }} className="graph-container" ref={graphContainerRef}>
{superState.graphs.map((el, i) => (
<Graph
el={el}
i={i}
superState={superState}
graphContainerRef={graphContainerRef}
dispatcher={dispatcher}
key={el.graphID}
active={i === superState.curGraphIndex}
graphID={el.graphID}
serverID={el.serverID}
graphML={el.graphML}
importedJson={el.importedJson || null}
projectName={el.projectName}
fileHandle={el.fileHandle}
fileName={el.fileName}
authorName={el.authorName}
/>
))}
<SearchPanel superState={superState} dispatcher={dispatcher} />
<ZoomComp dispatcher={dispatcher} superState={superState} />
</div>
<ConfirmModal
isOpen={superState.confirmModal.open}
title="Confirm"
message={superState.confirmModal.message}
onConfirm={() => {
if (superState.confirmModal.onConfirm) superState.confirmModal.onConfirm();
dispatcher({ type: T.SET_CONFIRM_MODAL, payload: { open: false, message: '', onConfirm: null } });
}}
onCancel={() => dispatcher({
type: T.SET_CONFIRM_MODAL,
payload: { open: false, message: '', onConfirm: null },
})}
/>
</div>
);
};
export default GraphComp;