-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathuseProject.js
More file actions
207 lines (179 loc) · 5.73 KB
/
useProject.js
File metadata and controls
207 lines (179 loc) · 5.73 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
/* eslint-disable react-hooks/exhaustive-deps */
import { useRef, useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { syncProject, setProject } from "../redux/EditorSlice";
import { createDefaultPythonProject } from "../utils/defaultProjects";
import { useTranslation } from "react-i18next";
export const useProject = ({
reactAppApiEndpoint = null,
assetsIdentifier = null,
projectIdentifier = null,
initialProject = null,
code = null,
accessToken = null,
loadRemix = false,
loadCache = true,
remixLoadFailed = false,
locale = null,
}) => {
const loading = useSelector((state) => state.editor.loading);
const isEmbedded = useSelector((state) => state.editor.isEmbedded);
const isBrowserPreview = useSelector((state) => state.editor.browserPreview);
const project = useSelector((state) => state.editor.project);
const loadDispatched = useRef(false);
const getCachedProject = (id) =>
isEmbedded && !isBrowserPreview
? null
: JSON.parse(localStorage.getItem(id || "project"));
const [cachedProject, setCachedProject] = useState(
getCachedProject(projectIdentifier),
);
const { i18n } = useTranslation();
const dispatch = useDispatch();
const effectiveLocale = locale ?? i18n.language;
const loadCachedProject = () => {
dispatch(setProject(cachedProject));
};
useEffect(() => {
setCachedProject(getCachedProject(projectIdentifier));
}, [projectIdentifier]);
useEffect(() => {
let didUnmount = false;
const loadProjectData = async () => {
if (loadRemix) {
return;
}
const is_cached_saved_project =
projectIdentifier &&
cachedProject &&
cachedProject.identifier === projectIdentifier;
const is_cached_unsaved_project =
!projectIdentifier && cachedProject && !initialProject;
// At the moment this will never match because the cachedProject doesn't have a locale attribute (yet),
// so this will always be false, which effectively disables the whole caching mechanism
const cachedLocaleMatches = cachedProject?.locale === effectiveLocale;
if (
loadCache &&
(is_cached_saved_project || is_cached_unsaved_project) &&
cachedLocaleMatches
) {
loadCachedProject();
return;
}
if (assetsIdentifier) {
dispatch(
syncProject("load")({
reactAppApiEndpoint,
identifier: assetsIdentifier,
locale: effectiveLocale,
accessToken,
assetsOnly: true,
}),
);
return;
}
if (projectIdentifier) {
dispatch(
syncProject("load")({
reactAppApiEndpoint,
identifier: projectIdentifier,
locale: effectiveLocale,
accessToken: accessToken,
}),
);
return;
}
if (initialProject) {
const project = JSON.parse(initialProject);
dispatch(setProject(project));
return;
}
if (code) {
const project = {
name: "Blank project",
project_type: "python",
components: [{ name: "main", extension: "py", content: code }],
};
dispatch(setProject(project));
return;
}
const data = await createDefaultPythonProject(effectiveLocale);
if (didUnmount) {
return;
}
dispatch(setProject(data));
};
void loadProjectData();
return () => {
didUnmount = true;
};
}, [
code,
projectIdentifier,
cachedProject,
effectiveLocale,
accessToken,
loadRemix,
initialProject,
]);
// Try to load the remix, if it fails set `remixLoadFailed` true, and load the project in the next useEffect
useEffect(() => {
if (!projectIdentifier || !accessToken || !loadRemix) return;
if (!remixLoadFailed && !loadDispatched.current) {
dispatch(
syncProject("loadRemix")({
reactAppApiEndpoint,
identifier: projectIdentifier,
accessToken: accessToken,
}),
);
// Prevents a failure on the initial render (using a ref to avoid triggering a render)
loadDispatched.current = true;
}
}, [projectIdentifier, accessToken, project, loadRemix, remixLoadFailed]);
useEffect(() => {
if (!projectIdentifier || !loadRemix) return;
if (remixLoadFailed && !loadDispatched.current) {
dispatch(
syncProject("load")({
reactAppApiEndpoint,
identifier: projectIdentifier,
locale: effectiveLocale,
accessToken: accessToken,
}),
);
loadDispatched.current = true;
}
}, [projectIdentifier, effectiveLocale, accessToken, remixLoadFailed]);
useEffect(() => {
if (code && loading === "success") {
const defaultName = project.project_type === "html" ? "index" : "main";
const defaultExtension = project.project_type === "html" ? "html" : "py";
const mainComponent = project.components?.find(
(component) =>
component.name === defaultName &&
component.extension === defaultExtension,
) || { name: defaultName, extension: defaultExtension, content: "" };
const otherComponents =
project.components?.filter(
(component) =>
!(
component.name === defaultName &&
component.extension === defaultExtension
),
) || [];
const updatedProject = {
...project,
project_type: project.project_type || "python",
components: [
...otherComponents,
{
...mainComponent,
content: code,
},
],
};
dispatch(setProject(updatedProject));
}
}, [code, loading]);
};