Skip to content

Commit a5e6663

Browse files
committed
feat: download output as json button
1 parent 726a5ab commit a5e6663

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

src/Preview.tsx

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@ import { cn } from "@/utils/cn";
1818
import {
1919
ActivityIcon,
2020
BugIcon,
21+
DownloadIcon,
2122
ExternalLinkIcon,
2223
LoaderIcon,
2324
PlayIcon,
2425
ScrollTextIcon,
2526
XIcon,
2627
} from "lucide-react";
2728
import { AnimatePresence, motion } from "motion/react";
28-
import { type FC, useEffect, useMemo, useState } from "react";
29+
import {
30+
type FC,
31+
useCallback,
32+
useEffect,
33+
useMemo,
34+
useRef,
35+
useState,
36+
} from "react";
2937
import { useSearchParams } from "react-router";
3038

3139
export const Preview: FC = () => {
@@ -40,6 +48,20 @@ export const Preview: FC = () => {
4048
const [params] = useSearchParams();
4149
const isDebug = useMemo(() => params.has("debug"), [params]);
4250

51+
52+
const onDownloadOutput = useCallback(() => {
53+
const blob = new Blob([JSON.stringify(output, null, 2)], {
54+
type: "application/json",
55+
});
56+
57+
const url = URL.createObjectURL(blob);
58+
59+
const link = document.createElement("a");
60+
link.href = url;
61+
link.download = "output.json";
62+
link.click();
63+
}, [output]);
64+
4365
useEffect(() => {
4466
if (!window.go_preview) {
4567
return;
@@ -93,9 +115,20 @@ export const Preview: FC = () => {
93115
</div>
94116
) : null}
95117

96-
<Tabs.List className={!isDebug ? "hidden" : undefined}>
97-
<Tabs.Trigger value="preview" icon={PlayIcon} label="Preview" />
98-
<Tabs.Trigger value="debug" icon={BugIcon} label="Debugger" />
118+
<Tabs.List
119+
className={cn(
120+
"justify-between pr-3",
121+
!isDebug ? "hidden" : undefined,
122+
)}
123+
>
124+
<div className="flex">
125+
<Tabs.Trigger value="preview" icon={PlayIcon} label="Preview" />
126+
<Tabs.Trigger value="debug" icon={BugIcon} label="Debugger" />
127+
</div>
128+
<Button size="sm" variant="outline" className="self-center" onClick={onDownloadOutput}>
129+
<DownloadIcon />
130+
Download output
131+
</Button>
99132
</Tabs.List>
100133

101134
<Tabs.Content value="preview" asChild={true}>

0 commit comments

Comments
 (0)