import { CheckIcon, CopyIcon } from "lucide-react"; import { useState } from "react"; import { cn } from "@/lib/utils"; interface PreProps { children?: React.ReactNode; className?: string; } export const CodeBlock = ({ children, className, ...props }: PreProps) => { const [copied, setCopied] = useState(false); // Extract the code element and its props const codeElement = children as React.ReactElement; const codeClassName = codeElement?.props?.className || ""; const codeContent = String(codeElement?.props?.children || "").replace(/\n$/, ""); // Extract language from className (format: language-xxx) const match = /language-(\w+)/.exec(codeClassName); const language = match ? match[1] : ""; const handleCopy = async () => { try { await navigator.clipboard.writeText(codeContent); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch (err) { console.error("Failed to copy code:", err); } }; return (
      
{language}
{children}
); };