From 3fb8817fd9f12c1e211a5f34cdd1613c020a98c2 Mon Sep 17 00:00:00 2001 From: 4d62 Date: Fri, 31 Jan 2025 19:39:42 -0500 Subject: [PATCH] add types --- .../Integrations/IntegrationsCodeblock.tsx | 102 +++++++++++------- 1 file changed, 66 insertions(+), 36 deletions(-) diff --git a/website/src/components/Integrations/IntegrationsCodeblock.tsx b/website/src/components/Integrations/IntegrationsCodeblock.tsx index 969442e5a073..c4ae8203fbd5 100644 --- a/website/src/components/Integrations/IntegrationsCodeblock.tsx +++ b/website/src/components/Integrations/IntegrationsCodeblock.tsx @@ -1,57 +1,87 @@ import React, { ReactNode, useState } from "react"; -const IntegrationsMultilineCodeblock = ({ - children, -}: { +type IntegrationsMultilineCodeblockProps = { children: ReactNode; -}) => { - const [isCopied, setIsCopied] = useState(false); - const content = React.Children.toArray(children) - .map((child) => (typeof child === "string" ? child : "")) - .join("") - .replace(/\n\s+/g, "\n") - .trim(); - - const handleCopy = () => { - navigator.clipboard.writeText(content).then(() => { - setIsCopied(true); - setTimeout(() => setIsCopied(false), 2000); - }); + className?: string; +}; + +type CopyButtonState = { + isCopied: boolean; + className: string; +}; + +type ContentProcessor = (children: ReactNode) => string; + +const IntegrationsMultilineCodeblock: React.FC< + IntegrationsMultilineCodeblockProps +> = ({ children, className = "" }) => { + const [copyState, setCopyState] = useState({ + isCopied: false, + className: "", + }); + + const processContent: ContentProcessor = (children) => { + return React.Children.toArray(children) + .map((child): string => (typeof child === "string" ? child : "")) + .join("") + .replace(/\n\s+/g, "\n") + .trim(); + }; + + const content: string = processContent(children); + + const handleCopy = async (): Promise => { + try { + await navigator.clipboard.writeText(content); + setCopyState({ + isCopied: true, + className: "integration-codeblock__copy-btn--copied", + }); + + setTimeout(() => { + setCopyState({ + isCopied: false, + className: "", + }); + }, 2000); + } catch (error) { + console.error("Failed to copy content:", error); + } }; + type IconProps = { + className: string; + path: string; + }; + + const Icon: React.FC = ({ className, path }) => ( + + + + ); + return ( -
+        
             {children}