From 303ea6040bdb06552d398c6bd4efe553da886df4 Mon Sep 17 00:00:00 2001 From: Arthur Degonde <44548105+ArthurD1@users.noreply.github.com> Date: Tue, 29 Oct 2024 19:10:32 +0100 Subject: [PATCH] refactor: better copy feature --- .../Transactions/Components/MessageBody.tsx | 12 +- .../Components/MessageHeaders.tsx | 9 +- .../Transactions/Components/PrettyBody.tsx | 2 +- .../TransactionDetailOnQuerySuccess.tsx | 2 +- .../TransactionDetailPage.test.tsx.snap | 115 +++--------------- .../CopyToClipBoard/CopyToClipboard.tsx | 35 +++--- 6 files changed, 51 insertions(+), 124 deletions(-) diff --git a/harp_apps/dashboard/frontend/src/Pages/Transactions/Components/MessageBody.tsx b/harp_apps/dashboard/frontend/src/Pages/Transactions/Components/MessageBody.tsx index eb60ba88..04680984 100644 --- a/harp_apps/dashboard/frontend/src/Pages/Transactions/Components/MessageBody.tsx +++ b/harp_apps/dashboard/frontend/src/Pages/Transactions/Components/MessageBody.tsx @@ -12,10 +12,14 @@ export function MessageBody({ id }: { id: string }) { if (query && query.isSuccess && query.data !== undefined) { if (query.data.content.byteLength) { return ( -
-- +-@@ -391,28 +321,11 @@ exports[`renders the title and data when the query is successful 1`] = `- +className?: string - contentType?: "text" | "html" + contentType?: string + description?: string } -const CopyToClipboard: React.FC= ({ text, targetRef, className, contentType = "text" }) => { +const CopyToClipboard: React.FC = ({ text, targetRef, className, contentType, description }) => { const [copySuccess, setCopySuccess] = useState(false) + const canCopy = navigator.clipboard const handleClipboardWrite = (clipboardData: string | ClipboardItem[]) => { const writePromise = @@ -34,14 +35,14 @@ const CopyToClipboard: React.FC = ({ text, targetRef, clas if (text) { handleClipboardWrite(text) } else if (targetRef?.current) { - if (contentType === "html") { + if (contentType?.includes("html")) { const html = targetRef.current.innerHTML - const blob = new Blob([html], { type: "text/html" }) - const data = [new ClipboardItem({ "text/html": blob })] + const blob = new Blob([html], { type: contentType }) + const data = [new ClipboardItem({ [contentType]: blob })] handleClipboardWrite(data) - } else { - const text = targetRef.current.textContent || "" + } else if (contentType?.startsWith("text/") || contentType?.startsWith("application/")) { + const text = targetRef.current.innerText || "" handleClipboardWrite(text) } } @@ -49,13 +50,17 @@ const CopyToClipboard: React.FC = ({ text, targetRef, clas const Icon = copySuccess ? ClipboardDocumentCheckIcon : ClipboardDocumentIcon - return ( - - ) + return canCopy && (!contentType || contentType.startsWith("text/") || contentType.startsWith("application/")) ? ( + + {description && ( + {description} + )} ++ ) : null } export default CopyToClipboard+