-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSaveProjectWindow.tsx
292 lines (275 loc) · 8.18 KB
/
SaveProjectWindow.tsx
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
import { FunctionComponent, useCallback, useContext, useState } from "react";
import { serializeAsZip } from "@SpCore/ProjectSerialization";
import { FileRegistry, mapModelToFileManifest } from "@SpCore/FileMapping";
import { ProjectContext } from "@SpCore/ProjectContextProvider";
import saveAsGitHubGist from "@SpCore/gists/saveAsGitHubGist";
import { triggerDownload } from "@SpUtil/triggerDownload";
import Button from "@mui/material/Button";
import BrowserProjectsInterface from "./BrowserProjectsInterface";
import timeAgoString from "@SpUtil/timeAgoString";
type SaveProjectWindowProps = {
onClose: () => void;
};
const SaveProjectWindow: FunctionComponent<SaveProjectWindowProps> = ({
onClose,
}) => {
const { data, update } = useContext(ProjectContext);
const fileManifest = mapModelToFileManifest(data);
const [exportingToGist, setExportingToGist] = useState(false);
const [savingToBrowser, setSavingToBrowser] = useState(false);
return (
<div>
<h3>Save this project</h3>
<table className="project-summary-table">
<tbody>
<tr>
<td>Title</td>
<td>
<EditTitleComponent
value={data.meta.title}
onChange={(newTitle: string) =>
update({ type: "retitle", title: newTitle })
}
/>
</td>
</tr>
{Object.entries(fileManifest).map(
([name, content], i) =>
content.trim() !== "" && (
<tr key={i}>
<td>{name}</td>
<td>{content.length} bytes</td>
</tr>
),
)}
</tbody>
</table>
<div> </div>
{!exportingToGist && !savingToBrowser && (
<div>
<Button
onClick={async () => {
serializeAsZip(data).then(([zipBlob, name]) =>
triggerDownload(zipBlob, `SP-${name}.zip`, onClose),
);
}}
>
Save to .zip file
</Button>
<Button
onClick={() => {
setExportingToGist(true);
}}
>
Save to GitHub Gist
</Button>
<Button
onClick={() => {
setSavingToBrowser(true);
}}
>
Save to Browser
</Button>
</div>
)}
{exportingToGist && (
<GistExportView
fileManifest={fileManifest}
title={data.meta.title}
onClose={onClose}
/>
)}
{savingToBrowser && (
<SaveToBrowserView
fileManifest={fileManifest}
title={data.meta.title}
onCancel={onClose}
/>
)}
</div>
);
};
type EditTitleComponentProps = {
value: string;
onChange: (value: string) => void;
};
const EditTitleComponent: FunctionComponent<EditTitleComponentProps> = ({
value,
onChange,
}) => {
return (
<input
type="text"
value={value}
onChange={(e) => onChange(e.target.value)}
/>
);
};
type GistExportViewProps = {
fileManifest: Partial<FileRegistry>;
title: string;
onClose: () => void;
};
const GistExportView: FunctionComponent<GistExportViewProps> = ({
fileManifest,
title,
onClose,
}) => {
const [gitHubPersonalAccessToken, setGitHubPersonalAccessToken] =
useState("");
const [gistUrl, setGistUrl] = useState<string | null>(null);
const handleExport = useCallback(async () => {
try {
const gistUrl = await saveAsGitHubGist(fileManifest, {
defaultDescription: title,
personalAccessToken: gitHubPersonalAccessToken,
});
setGistUrl(gistUrl);
} catch (err: any) {
alert(`Error saving to GitHub Gist: ${err.message}`);
}
}, [gitHubPersonalAccessToken, fileManifest, title]);
return (
<div className="GistExplainer">
<h3>Save to GitHub Gist</h3>
<p>
In order to save this project as a GitHub Gist, you will need to provide
a GitHub Personal Access Token. This token will be used to
authenticate with GitHub and create a new Gist with the files in this
project. You can create a new Personal Access Token by visiting
your{" "}
<a
href="https://github.com/settings/tokens?type=beta"
target="_blank"
rel="noreferrer"
>
GitHub settings
</a>
. Go to <i>Fine-grained tokens</i> and generate a new fine-grained
token. Be sure to only grant Gist read/write permission by using Gists
item in the <i>Account Permissions</i> section. You should also specify
an expiration date. Copy the token and paste it into the field
below.
</p>
<p>
For security reasons, your token will not be saved in this
application, so you should store it securely in a text file for
future use.
</p>
<table className="project-summary-table">
<tbody>
<tr>
<td>GitHub Personal Access Token</td>
<td>
<input
type="password"
value={gitHubPersonalAccessToken}
onChange={(e) => setGitHubPersonalAccessToken(e.target.value)}
/>
</td>
</tr>
</tbody>
</table>
<div> </div>
{!gistUrl && (
<div>
<Button onClick={handleExport} disabled={!gitHubPersonalAccessToken}>
Save to GitHub Gist
</Button>
<Button onClick={onClose}>Cancel</Button>
</div>
)}
{gistUrl && (
<div>
<p>
Successfully saved to GitHub Gist:
<a href={gistUrl} target="_blank" rel="noreferrer">
{gistUrl}
</a>
</p>
<p>
You can now share the following link to this Stan Playground
project:
<br />
<br />
<a
href={makeSPShareableLinkFromGistUrl(gistUrl)}
target="_blank"
rel="noreferrer"
>
{makeSPShareableLinkFromGistUrl(gistUrl)}
</a>
<br />
</p>
<Button onClick={onClose}>Close</Button>
</div>
)}
</div>
);
};
const makeSPShareableLinkFromGistUrl = (gistUrl: string) => {
const protocol = window.location.protocol;
const host = window.location.host;
const url = `${protocol}//${host}?project=${gistUrl}`;
return url;
};
type SaveToBrowserViewProps = {
fileManifest: Partial<FileRegistry>;
title: string;
onCancel: () => void;
};
const SaveToBrowserView: FunctionComponent<SaveToBrowserViewProps> = ({
fileManifest,
title,
onCancel,
}) => {
// use IndexedDB to save the project
// https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB
const handleSave = useCallback(async () => {
try {
const bpi = new BrowserProjectsInterface();
const existingBrowserProject = await bpi.loadBrowserProject(title);
if (existingBrowserProject) {
const overwrite = window.confirm(
`A project with the title "${title}" already exists (modified ${timeAgoString(existingBrowserProject.timestamp)}). Do you want to overwrite it?`,
);
if (!overwrite) {
return;
}
}
await bpi.saveBrowserProject(title, {
title,
timestamp: Date.now(),
fileManifest,
});
} catch (err: any) {
alert(`Error saving to browser: ${err.message}`);
}
onCancel();
}, [title, fileManifest, onCancel]);
return (
<div className="SaveToBrowserView">
<h3>Save to Browser</h3>
<p>
This project will be saved to your browser as "{title}". It
will be available to you on this device until you clear your browser
cache, but not on other devices or browsers.
</p>
<div>
<Button
onClick={() => {
handleSave();
}}
>
Save to Browser
</Button>
<Button onClick={onCancel}>Cancel</Button>
</div>
</div>
);
};
export default SaveProjectWindow;