Skip to content

Commit 533ee18

Browse files
sinonMichaReiser
andauthored
[ty] Add Download button to ty playground which creates a zip export (#23478)
Co-authored-by: Micha Reiser <micha@reiser.io>
1 parent 55bfd2f commit 533ee18

8 files changed

Lines changed: 108 additions & 2 deletions

File tree

‎playground/package-lock.json‎

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎playground/ruff/src/Editor/Chrome.tsx‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ruffSchema from "../../../../ruff.schema.json";
22
import { useCallback, useMemo, useRef, useState } from "react";
3-
import { Header, useTheme, setupMonaco } from "shared";
3+
import { Header, useTheme, setupMonaco, downloadZip } from "shared";
44
import {
55
copyAsMarkdown,
66
copyAsMarkdownLink,
@@ -44,6 +44,24 @@ export default function Chrome() {
4444
await copyAsMarkdown(settings, pythonSource);
4545
}, [pythonSource, settings]);
4646

47+
const handleDownload = useCallback(async () => {
48+
if (settings == null || pythonSource == null) {
49+
return;
50+
}
51+
52+
const toml = await import("smol-toml");
53+
54+
const files: { [name: string]: string } = { "main.py": pythonSource };
55+
56+
try {
57+
files["ruff.toml"] = toml.stringify(JSON.parse(settings));
58+
} catch {
59+
files["ruff.json"] = settings;
60+
}
61+
62+
await downloadZip(files, "ruff-playground");
63+
}, [pythonSource, settings]);
64+
4765
if (initPromise.current == null) {
4866
initPromise.current = startPlayground()
4967
.then(({ sourceCode, settings, ruffVersion }) => {
@@ -110,6 +128,7 @@ export default function Chrome() {
110128
onShare={handleShare}
111129
onCopyMarkdownLink={handleCopyMarkdownLink}
112130
onCopyMarkdown={handleCopyMarkdown}
131+
onDownload={handleDownload}
113132
onReset={handleResetClicked}
114133
/>
115134

‎playground/shared/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"dependencies": {
77
"@monaco-editor/react": "^4.7.0",
88
"classnames": "^2.3.2",
9+
"fflate": "^0.8.2",
910
"react-aria-components": "^1.16.0",
1011
"react": "^19.0.0",
1112
"react-resizable-panels": "^4.0.0"

‎playground/shared/src/Header.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default function Header({
1616
onShare,
1717
onCopyMarkdownLink,
1818
onCopyMarkdown,
19+
onDownload,
1920
}: {
2021
theme: Theme;
2122
tool: "ruff" | "ty";
@@ -26,6 +27,7 @@ export default function Header({
2627
onShare: () => Promise<void>;
2728
onCopyMarkdownLink: () => Promise<void>;
2829
onCopyMarkdown: () => Promise<void>;
30+
onDownload(): void;
2931
}) {
3032
return (
3133
<div
@@ -61,6 +63,7 @@ export default function Header({
6163
onShare={onShare}
6264
onCopyMarkdownLink={onCopyMarkdownLink}
6365
onCopyMarkdown={onCopyMarkdown}
66+
onDownload={onDownload}
6467
/>
6568
</div>
6669
<Divider />

‎playground/shared/src/ShareButton.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
MenuTrigger,
88
Popover,
99
Pressable,
10+
Separator,
1011
} from "react-aria-components";
1112
import AstralButton from "./AstralButton";
1213

@@ -17,10 +18,12 @@ export default function ShareButton({
1718
onShare,
1819
onCopyMarkdownLink,
1920
onCopyMarkdown,
21+
onDownload,
2022
}: {
2123
onShare: () => Promise<void>;
2224
onCopyMarkdownLink: () => Promise<void>;
2325
onCopyMarkdown: () => Promise<void>;
26+
onDownload(): void;
2427
}) {
2528
const [status, dispatch, isPending] = useActionState(
2629
async (_previousStatus: ShareStatus, action: ShareAction) => {
@@ -101,6 +104,8 @@ export default function ShareButton({
101104
>
102105
Markdown
103106
</ShareMenuItem>
107+
<Separator className="my-1 border-t border-gray-200 dark:border-gray-700" />
108+
<ShareMenuItem onAction={onDownload}>Download ZIP</ShareMenuItem>
104109
</Menu>
105110
</Popover>
106111
</MenuTrigger>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { strToU8, zipSync } from "fflate";
2+
3+
/**
4+
* Creates a ZIP archive from the given files and triggers a browser download.
5+
* The filename includes a short content hash for uniqueness.
6+
*/
7+
export async function downloadZip(
8+
files: { [name: string]: string },
9+
prefix = "playground",
10+
): Promise<void> {
11+
const data: { [name: string]: Uint8Array } = {};
12+
13+
for (const [name, content] of Object.entries(files)) {
14+
data[name] = strToU8(content);
15+
}
16+
17+
const zipped = zipSync(data);
18+
19+
const hash = await contentHash(JSON.stringify(files));
20+
21+
const blob = new Blob([zipped.buffer as ArrayBuffer], {
22+
type: "application/zip",
23+
});
24+
const url = URL.createObjectURL(blob);
25+
26+
const a = document.createElement("a");
27+
a.href = url;
28+
a.download = `${prefix}-${hash}.zip`;
29+
a.click();
30+
31+
URL.revokeObjectURL(url);
32+
}
33+
34+
async function contentHash(content: string): Promise<string> {
35+
const encoded = new TextEncoder().encode(content);
36+
const digest = await crypto.subtle.digest("SHA-256", encoded);
37+
const bytes = new Uint8Array(digest);
38+
return Array.from(bytes.slice(0, 4))
39+
.map((b) => b.toString(16).padStart(2, "0"))
40+
.join("");
41+
}

‎playground/shared/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * as Icons from "./Icons";
66
export { type Theme, useTheme } from "./theme";
77
export { HorizontalResizeHandle, VerticalResizeHandle } from "./ResizeHandle";
88
export { setupMonaco } from "./setupMonaco";
9+
export { downloadZip } from "./downloadZip";
910
export {
1011
default as SideBar,
1112
SideBarEntry,

‎playground/ty/src/Playground.tsx‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import {
99
useRef,
1010
useState,
1111
} from "react";
12-
import { ErrorMessage, Header, setupMonaco, useTheme } from "shared";
12+
import {
13+
ErrorMessage,
14+
Header,
15+
setupMonaco,
16+
useTheme,
17+
downloadZip,
18+
} from "shared";
1319
import { FileHandle, PositionEncoding, Workspace } from "ty_wasm";
1420
import {
1521
copyAsMarkdown,
@@ -80,6 +86,28 @@ export default function Playground() {
8086
}
8187
}, [files]);
8288

89+
const handleDownload = useCallback(async () => {
90+
const serialized = serializeFiles(files);
91+
92+
if (serialized != null) {
93+
const downloadFiles = { ...serialized.files };
94+
95+
if (SETTINGS_FILE_NAME in downloadFiles) {
96+
try {
97+
const toml = await import("smol-toml");
98+
const tomlContent = toml.stringify(
99+
JSON.parse(downloadFiles[SETTINGS_FILE_NAME]),
100+
);
101+
delete downloadFiles[SETTINGS_FILE_NAME];
102+
downloadFiles["ty.toml"] = tomlContent;
103+
} catch {
104+
// Keep the original JSON file if conversion fails.
105+
}
106+
}
107+
108+
await downloadZip(downloadFiles, "ty-playground");
109+
}
110+
}, [files]);
83111
const handleFileAdded = useCallback((workspace: Workspace, name: string) => {
84112
let handle = null;
85113

@@ -205,6 +233,7 @@ export default function Playground() {
205233
onShare={handleShare}
206234
onCopyMarkdownLink={handleCopyMarkdownLink}
207235
onCopyMarkdown={handleCopyMarkdown}
236+
onDownload={handleDownload}
208237
onReset={workspace == null ? undefined : handleReset}
209238
/>
210239

0 commit comments

Comments
 (0)