-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIDE.jsx
More file actions
297 lines (254 loc) · 8.32 KB
/
IDE.jsx
File metadata and controls
297 lines (254 loc) · 8.32 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { Box } from "@mui/material";
import ChatDrawer from "../../widgets/ChatDrawer/ChatDrawer";
import Context from "../../context";
import ContextProvider from "../../context/context";
import EducationDrawer from "../../components/EducationDrawer/EducationDrawer";
import Menu from "../../widgets/Menu";
import Onboard from "./Onboarding";
import Path from "../../utils/Path";
import PopChat from "../../widgets/PopChat";
import ProcessDrawer from "../../widgets/ProcessDrawer/ProcessDrawer";
import Settings from "../../settings";
import SwaggerDialog from "../../components/SwaggerDialog";
import { contextReducer } from "../../context/reducer";
import { contextToMap } from "../../utils/Parser";
import routes from "../../routes";
import sandboxService from "../../sandboxService";
import service from "../../service";
import { storage } from "@nucleoidjs/webstorage";
import styles from "./styles";
import { useLocation } from "react-router-dom";
import { useNavigate } from "react-router-dom";
import { v4 as uuid } from "uuid";
import vfs from "../../vfs";
import { Outlet, useParams } from "react-router-dom"; // eslint-disable-line
import React, { useEffect } from "react";
import { publish, useEvent } from "@nucleoidai/react-event";
import { useMediaQuery, useTheme } from "@mui/material";
let loaded = false;
function IDE() {
const [ReactContext, setReactContext] = React.useState();
const navigate = useNavigate();
const location = useLocation();
const modeQuery = location.search;
const { id } = useParams();
const page = location.pathname.split("/")[2];
const [contextProviderKey, setContextProviderKey] = React.useState(uuid());
const [projectChange] = useEvent("PROJECT_CHANGED", { id: null });
const [event] = useEvent("PAGE_LOADED", {
name: null,
});
const theme = useTheme();
const mobileSize = useMediaQuery(theme.breakpoints.down("sm"));
useEffect(() => {
if (mobileSize) {
navigate("/mobile");
Settings.landing({ level: Number.MAX_SAFE_INTEGER });
}
// eslint-disable-next-line
}, [mobileSize]);
function getContextFromStorage(projectId) {
const project = storage.get("ide", "project", projectId);
const specification = storage.get("ide", "specification", projectId);
if (!specification && !project) {
navigate("/error/api");
return null;
}
publish("PROJECT_FOUNDED", { id: projectId, type: "LOCAL", from: "URL" });
const context = Context.withPages({ specification, project });
context.get = (prop) => Context.resolve(context, prop);
return context;
}
function getContextFromTerminal(projectId) {
publish("PAGE_LOADED", {
id: projectId,
type: "TERMINAL",
from: "URL",
});
publish("RUNTIME_CONNECTION", {
status: false,
metrics: { total: 100, free: 50 },
});
sandboxService.setTerminalUrl(Settings.url.terminal());
const context = Context.withBlank();
context.get = (prop) => Context.resolve(context, prop);
return context;
}
async function project(projectId) {
const [projectResult, serviceResult] = await Promise.all([
service.getProject(projectId),
service.getProjectServices(projectId),
]).catch((error) => {
if (error.response.status === 404) {
return [undefined, undefined];
}
});
const projectService = serviceResult.data;
const project = projectResult.data;
publish("PROJECT_FOUNDED", { id: projectId, type: "CLOUD", from: "URL" });
if (project.type === "SINGLE") {
const specificationId = projectService[0].id;
const { data: specification } = await service.getContext(specificationId);
const context = Context.withPages({ specification });
context.get = (prop) => Context.resolve(context, prop);
context.project = {
type: "CLOUD",
name: project.name,
id: specificationId,
description: project.description,
};
storage.set("ide", "selected", "project", {
id: project.id,
type: "CLOUD",
});
return context;
} else {
console.log("Multiple projects not supported yet.");
}
}
function sampleProject() {
const context = Context.withSample();
context.get = (prop) => Context.resolve(context, prop);
const { specification, project } = context;
storage.set("ide", "project", project.id, project);
storage.set("ide", "specification", project.id, specification);
navigate(`/${project.id}/api?mode=local`);
return context;
}
function blankProject() {
const context = Context.withBlank();
context.get = (prop) => Context.resolve(context, prop);
return context;
}
const initContext = (context) => {
console.log(context);
if (
!Settings.description() ||
Settings.description() !== context.project.description
) {
Settings.description(context.project.description);
}
if (!Settings.name() || Settings.name !== context.project.name) {
Settings.name(context.project.name);
}
if (!Settings.landing()) {
Settings.landing({ level: 0 });
}
if (mobileSize) {
navigate("/mobile");
navigate(0);
Settings.landing({ level: Number.MAX_SAFE_INTEGER });
}
if (
context.project.type === "LOCAL" &&
storage.get("ide", "project", context.project.id)
) {
storage.set("ide", "selected", "project", {
id: context.project.id,
type: "LOCAL",
});
}
if (!page) {
context.project.type === "CLOUD"
? navigate("api")
: navigate("api?mode=local");
}
return context;
};
const initVfs = (context) => {
console.log(context);
const files = contextToMap(context.specification);
vfs.init(files);
};
const checkRecentProject = (recentProject) => {
if (recentProject) {
if (recentProject.type === "CLOUD") {
navigate(`/${recentProject.id}`);
} else if (recentProject.type === "LOCAL") {
navigate(`/${recentProject.id}?mode=local`);
}
} else {
navigate("/new");
}
};
React.useEffect(() => {
async function initMode() {
const mode = Path.getMode();
const recentProject = Path.getRecentProject();
if (mode === "cloud") {
project(id)
.then((result) => {
initVfs(result);
return setReactContext(initContext(result));
})
.catch(() => {
navigate("/error/api");
});
} else if (mode === "local") {
const context = getContextFromStorage(id);
if (!context) return;
initVfs(context);
return setReactContext(initContext(context));
} else if (mode === "terminal") {
const context = getContextFromTerminal(id);
return setReactContext(initContext(context));
} else {
if (id === "sample") {
sampleProject();
} else if (id === "mobile") {
return setReactContext("mobile");
} else if (id === "new") {
const blankContext = blankProject();
setReactContext(initContext(blankContext));
} else if (id === "error") {
publish("PROJECT_NOT_FOUNDED", true);
const blankContext = blankProject();
setReactContext(initContext(blankContext));
} else if (id === undefined) {
checkRecentProject(recentProject);
} else {
navigate("/error/api");
}
}
}
initMode();
loaded = false;
// eslint-disable-next-line
}, [id]);
useEffect(() => {
if (ReactContext && event.name && !loaded) {
publish("CONTAINER_LOADED", {
name: "IDE",
});
loaded = true;
}
}, [event.name, ReactContext]);
useEffect(() => {
if (projectChange.id) {
setContextProviderKey(uuid());
}
}, [projectChange, ReactContext]);
if (!ReactContext) return null;
if (ReactContext === "error") return <div>forbiden</div>;
return (
<ContextProvider
key={contextProviderKey}
state={ReactContext}
reducer={contextReducer}
>
<Box sx={styles.root}>
<Menu list={routes} query={modeQuery} id={id} title="IDE" />
<EducationDrawer />
<ChatDrawer />
<Box sx={styles.content}>
<Outlet />
</Box>
<Onboard />
<ProcessDrawer />
<SwaggerDialog />
<PopChat />
</Box>
</ContextProvider>
);
}
export default IDE;