diff --git a/apps/code/src/renderer/components/CodeBlock.tsx b/apps/code/src/renderer/components/CodeBlock.tsx index 733291a71..ab59946a0 100644 --- a/apps/code/src/renderer/components/CodeBlock.tsx +++ b/apps/code/src/renderer/components/CodeBlock.tsx @@ -1,4 +1,7 @@ +import { Check, Copy } from "@phosphor-icons/react"; +import { IconButton } from "@radix-ui/themes"; import type { ReactNode } from "react"; +import { useCallback, useState } from "react"; type CodeBlockSize = "1" | "1.5" | "2" | "3"; @@ -29,27 +32,67 @@ const sizeStyles: Record< }, }; +function extractText(children: ReactNode): string { + if (typeof children === "string") return children; + if (Array.isArray(children)) return children.map(extractText).join(""); + if (children && typeof children === "object" && "props" in children) { + return extractText( + (children as { props: { children?: ReactNode } }).props.children, + ); + } + return ""; +} + export function CodeBlock({ children, size = "1" }: CodeBlockProps) { const styles = sizeStyles[size]; + const [copied, setCopied] = useState(false); + + const handleCopy = useCallback(() => { + const text = extractText(children); + navigator.clipboard.writeText(text); + setCopied(true); + setTimeout(() => setCopied(false), 2000); + }, [children]); return ( -
-      {children}
-    
+
+
+        {children}
+      
+ + {copied ? : } + +
); }