Skip to content

Commit 9c4e9ed

Browse files
authored
Website: Add Download for json-editor (#2099)
1 parent 5a25348 commit 9c4e9ed

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

frontend/src/app/json-editor/page.tsx

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { fetchCategories } from "@/lib/data";
2323
import { Category } from "@/lib/types";
2424
import { cn } from "@/lib/utils";
2525
import { format } from "date-fns";
26-
import { CalendarIcon, Check, Clipboard } from "lucide-react";
26+
import { CalendarIcon, Check, Clipboard, Download } from "lucide-react";
2727
import { useCallback, useEffect, useMemo, useState } from "react";
2828
import { toast } from "sonner";
2929
import { z } from "zod";
@@ -97,6 +97,21 @@ export default function JSONGenerator() {
9797
toast.success("Copied metadata to clipboard");
9898
}, [script]);
9999

100+
const handleDownload = useCallback(() => {
101+
const jsonString = JSON.stringify(script, null, 2);
102+
const blob = new Blob([jsonString], { type: "application/json" });
103+
const url = URL.createObjectURL(blob);
104+
105+
const a = document.createElement("a");
106+
a.href = url;
107+
a.download = `${script.slug || "script"}.json`;
108+
document.body.appendChild(a);
109+
a.click();
110+
111+
URL.revokeObjectURL(url);
112+
document.body.removeChild(a);
113+
}, [script]);
114+
100115
const handleDateSelect = useCallback(
101116
(date: Date | undefined) => {
102117
updateScript("date_created", format(date || new Date(), "yyyy-MM-dd"));
@@ -313,18 +328,26 @@ export default function JSONGenerator() {
313328
<div className="w-1/2 p-4 bg-background overflow-y-auto">
314329
{validationAlert}
315330
<div className="relative">
331+
{/* Copy Button */}
316332
<Button
317-
className="absolute right-2 top-2"
333+
className="absolute right-10 top-2"
318334
size="icon"
319335
variant="outline"
320336
onClick={handleCopy}
321337
>
322-
{isCopied ? (
323-
<Check className="h-4 w-4" />
324-
) : (
325-
<Clipboard className="h-4 w-4" />
326-
)}
338+
{isCopied ? <Check className="h-4 w-4" /> : <Clipboard className="h-4 w-4" />}
339+
</Button>
340+
341+
{/* Download Button */}
342+
<Button
343+
className="absolute right-2 top-2"
344+
size="icon"
345+
variant="outline"
346+
onClick={handleDownload}
347+
>
348+
<Download className="h-4 w-4" />
327349
</Button>
350+
328351
<pre className="mt-4 p-4 bg-secondary rounded shadow overflow-x-scroll">
329352
{JSON.stringify(script, null, 2)}
330353
</pre>

0 commit comments

Comments
 (0)