From 29e294d9512c611b60d1c98bf8d8018a78a9a303 Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 10 May 2024 05:43:55 -0500 Subject: [PATCH] Add actions: copy to clipboard, download response file --- src/components/common/AbstractCard.tsx | 49 ++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/components/common/AbstractCard.tsx b/src/components/common/AbstractCard.tsx index 53386c3..5e25d56 100644 --- a/src/components/common/AbstractCard.tsx +++ b/src/components/common/AbstractCard.tsx @@ -4,6 +4,8 @@ import { useBoolean } from "usehooks-ts"; import { LinkIcon, CodeBracketIcon, + DocumentArrowDownIcon, + ClipboardDocumentIcon, } from "@heroicons/react/24/outline"; type AbstractCardProps = { @@ -28,12 +30,6 @@ const AbstractCard: FunctionComponent = ({ {header != undefined || data != undefined ? (
{header}
- {data != undefined ? ( -
- {url != undefined ? ( ) : null} + {data != undefined ? ( + <> +
+ { + // stringify the JSON object, then begin the async clipboard write + navigator.clipboard.writeText(JSON.stringify(data, null, 4)).then(() => { + console.log('Copied to clipboard.'); + }, (err) => { + if (err instanceof Error) + console.error(`Failed to copy to clipboard (${err.toString()}).`); + else + console.error("Failed to copy to clipboard."); + }); + }} + className="h-6 w-6 cursor-pointer" + /> +
+
+ { + const file = new Blob([JSON.stringify(data, null, 4)], { + type: "application/json", + }); + + const anchor = document.createElement("a"); + anchor.href = URL.createObjectURL(file); + anchor.download = "response.json"; + anchor.click(); + }} + className="h-6 w-6 cursor-pointer" + /> +
+
+ +
+ + ) : null}
) : null}