-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
38 lines (32 loc) · 945 Bytes
/
utils.ts
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
import { SandpackFile } from "@codesandbox/sandpack-react";
export async function getFiles({ id }: { id: string }) {
const configUrl = `https://codesandbox.io/api/v1/sandboxes/${id}/sandpack`;
const response = await fetch(configUrl);
if (response.ok) {
const data = await response.json();
return updateFiles(data.files);
}
return {};
}
function getChallengeConfig(json: string) {
const csb = JSON.parse(json);
if (csb?.previewConfig) {
return csb.previewConfig;
}
return {
visibleFiles: [],
activeFile: "/src/App.js",
};
}
export function updateFiles(files: { [key: string]: SandpackFile }) {
const previewConfig = getChallengeConfig(files["/package.json"].code);
Object.keys(files).map((key) => {
if (key === previewConfig.activeFile) {
files[key].active = true;
}
if (!previewConfig.visibleFiles.includes(key)) {
files[key].hidden = true;
}
});
return files;
}