forked from stackblitz/tutorialkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadButton.tsx
More file actions
44 lines (35 loc) · 1.4 KB
/
DownloadButton.tsx
File metadata and controls
44 lines (35 loc) · 1.4 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
import { tutorialStore, webcontainer as webcontainerPromise } from './webcontainer.js';
export function DownloadButton() {
return (
<button
title="Download lesson as zip-file"
className="flex items-center text-2xl text-tk-elements-topBar-iconButton-iconColor hover:text-tk-elements-topBar-iconButton-iconColorHover transition-theme bg-tk-elements-topBar-iconButton-backgroundColor hover:bg-tk-elements-topBar-iconButton-backgroundColorHover p-1 rounded-md"
onClick={onClick}
>
<div className="i-ph-download-simple" />
</button>
);
}
async function onClick() {
const lesson = tutorialStore.lesson;
if (!lesson) {
throw new Error('Missing lesson');
}
const webcontainer = await webcontainerPromise;
const data = await webcontainer.export('/home/tutorial', { format: 'zip', excludes: ['node_modules'] });
let filename =
typeof lesson.data.downloadAsZip === 'object'
? lesson.data.downloadAsZip.filename
: [lesson.part?.id, lesson.chapter?.id, lesson.id].filter(Boolean).join('-');
if (!filename.endsWith('.zip')) {
filename += '.zip';
}
const link = document.createElement('a');
link.style.display = 'none';
link.download = filename;
link.href = URL.createObjectURL(new Blob([data] as any, { type: 'application/zip' }));
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(link.href);
}