forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.js
98 lines (85 loc) · 2.63 KB
/
save.js
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
import { dispatch } from "./dispatch.js";
import md5 from "https://cdn.skypack.dev/md5";
import copy from "./utils/copy.js";
import { html } from "./uhtml.js";
export const hashState = () => md5(dispatch("GET_SAVE_STATE"));
async function saveToS3({ content, state, copyUrl }) {
const uniqueID = md5(JSON.stringify(content));
const { exists, uploadURL, jsonFilename, id } = await fetch(
`https://vt4x133ukg.execute-api.eu-west-1.amazonaws.com/default/getPresignedURL?id=${uniqueID}`
).then((r) => r.json());
if (!exists) {
await fetch(uploadURL, {
mode: "cors",
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(content),
});
}
function getURLPath(extension) {
return (
window.location.protocol +
"//" +
window.location.host +
window.location.pathname +
extension
);
}
const link = getURLPath(`?id=${id}`);
if (copyUrl) {
copy(link);
dispatch("NOTIFICATION", {
message: html`
Sharing link copied to clipboard! If you're on Safari you'll have to
<button @click=${() => copy(link)}>click here to copy</button>.
`,
open: true,
});
}
state.lastSaved.name = content.name;
state.lastSaved.prog = content.prog;
state.lastSaved.link = link;
return link;
}
async function saveToFile({ content, state }) {
const blob = new Blob([JSON.stringify(content)], { type: "text/plain" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = `${state.name}.json`;
await new Promise((resolve) => setTimeout(resolve, 500)); // wait half a second (in milliseconds)
link.click();
URL.revokeObjectURL(link);
dispatch("NOTIFICATION", {
message:
"Your file has just been downloaded! Just drag it into the editor to load from your save",
open: true,
});
}
export async function save(type, state, copyUrl = true) {
state.runStatus = "loading";
dispatch("RENDER");
const content = JSON.parse(dispatch("GET_SAVE_STATE"));
switch (type) {
case "link":
state.saveLinkStatus = "loading";
dispatch("RENDER");
await saveToS3({ content, state, copyUrl });
dispatch("SOUND", "upload");
state.saveLinkStatus = "ready";
break;
case "file":
state.saveFileStatus = "loading";
dispatch("RENDER");
await saveToFile({ content, state });
dispatch("SOUND", "download");
state.saveFileStatus = "ready";
break;
default:
throw new Error("Sharing type", type, "does not exist");
}
state.runStatus = "ready";
dispatch("RENDER");
console.log("ready");
}