Skip to content

Commit 50b4568

Browse files
committed
feat: download output button
1 parent a5e6663 commit 50b4568

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/Preview.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Button } from "@/components/Button";
2-
import * as Dialog from "@radix-ui/react-dialog";
32
import {
43
ResizableHandle,
54
ResizablePanel,
@@ -15,6 +14,7 @@ import type { ParserLog, PreviewOutput } from "@/gen/types";
1514
import { useDebouncedValue } from "@/hooks/debounce";
1615
import { useStore } from "@/store";
1716
import { cn } from "@/utils/cn";
17+
import * as Dialog from "@radix-ui/react-dialog";
1818
import {
1919
ActivityIcon,
2020
BugIcon,
@@ -31,10 +31,10 @@ import {
3131
useCallback,
3232
useEffect,
3333
useMemo,
34-
useRef,
3534
useState,
3635
} from "react";
3736
import { useSearchParams } from "react-router";
37+
import ReactJsonView from "@microlink/react-json-view";
3838

3939
export const Preview: FC = () => {
4040
const $wasmState = useStore((state) => state.wasmState);
@@ -48,7 +48,6 @@ export const Preview: FC = () => {
4848
const [params] = useSearchParams();
4949
const isDebug = useMemo(() => params.has("debug"), [params]);
5050

51-
5251
const onDownloadOutput = useCallback(() => {
5352
const blob = new Blob([JSON.stringify(output, null, 2)], {
5453
type: "application/json",
@@ -59,7 +58,16 @@ export const Preview: FC = () => {
5958
const link = document.createElement("a");
6059
link.href = url;
6160
link.download = "output.json";
61+
document.appendChild(link);
6262
link.click();
63+
document.removeChild(link);
64+
65+
// Give the click event enough time to fire and then revoke the URL.
66+
// This method of doing it doesn't seem great but I'm not sure if there is a
67+
// better way.
68+
setTimeout(() => {
69+
URL.revokeObjectURL(url);
70+
}, 100);
6371
}, [output]);
6472

6573
useEffect(() => {
@@ -125,7 +133,13 @@ export const Preview: FC = () => {
125133
<Tabs.Trigger value="preview" icon={PlayIcon} label="Preview" />
126134
<Tabs.Trigger value="debug" icon={BugIcon} label="Debugger" />
127135
</div>
128-
<Button size="sm" variant="outline" className="self-center" onClick={onDownloadOutput}>
136+
<Button
137+
size="sm"
138+
variant="outline"
139+
className="self-center"
140+
onClick={onDownloadOutput}
141+
disabled={isDebouncing}
142+
>
129143
<DownloadIcon />
130144
Download output
131145
</Button>
@@ -353,7 +367,9 @@ const Debugger: FC<DebuggerProps> = ({ output }) => {
353367
direction="vertical"
354368
className="h-full w-full bg-surface-primary"
355369
>
356-
<ResizablePanel> foo</ResizablePanel>
370+
<ResizablePanel>
371+
<ReactJsonView src={output ?? {}} />
372+
</ResizablePanel>
357373
<ResizableHandle className="bg-surface-quaternary" />
358374
<ResizablePanel
359375
className={cn(

0 commit comments

Comments
 (0)