From 9c4e9ed4be3ed32dddca0bfea99fd9f7e01a7a82 Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Thu, 6 Feb 2025 17:04:32 +0100 Subject: [PATCH] Website: Add Download for json-editor (#2099) --- frontend/src/app/json-editor/page.tsx | 37 ++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/frontend/src/app/json-editor/page.tsx b/frontend/src/app/json-editor/page.tsx index 4986378ef..3d259fc57 100644 --- a/frontend/src/app/json-editor/page.tsx +++ b/frontend/src/app/json-editor/page.tsx @@ -23,7 +23,7 @@ import { fetchCategories } from "@/lib/data"; import { Category } from "@/lib/types"; import { cn } from "@/lib/utils"; import { format } from "date-fns"; -import { CalendarIcon, Check, Clipboard } from "lucide-react"; +import { CalendarIcon, Check, Clipboard, Download } from "lucide-react"; import { useCallback, useEffect, useMemo, useState } from "react"; import { toast } from "sonner"; import { z } from "zod"; @@ -97,6 +97,21 @@ export default function JSONGenerator() { toast.success("Copied metadata to clipboard"); }, [script]); + const handleDownload = useCallback(() => { + const jsonString = JSON.stringify(script, null, 2); + const blob = new Blob([jsonString], { type: "application/json" }); + const url = URL.createObjectURL(blob); + + const a = document.createElement("a"); + a.href = url; + a.download = `${script.slug || "script"}.json`; + document.body.appendChild(a); + a.click(); + + URL.revokeObjectURL(url); + document.body.removeChild(a); + }, [script]); + const handleDateSelect = useCallback( (date: Date | undefined) => { updateScript("date_created", format(date || new Date(), "yyyy-MM-dd")); @@ -313,18 +328,26 @@ export default function JSONGenerator() {
{JSON.stringify(script, null, 2)}