diff --git a/src/app/components/MarkdownContent.tsx b/src/app/components/MarkdownContent.tsx index bca2b76..5486c7b 100644 --- a/src/app/components/MarkdownContent.tsx +++ b/src/app/components/MarkdownContent.tsx @@ -1,17 +1,78 @@ "use client"; -import React from "react"; +import React, { useCallback, useState } from "react"; import ReactMarkdown from "react-markdown"; import remarkGfm from "remark-gfm"; import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism"; +import { Check, Copy } from "lucide-react"; +import { toast } from "sonner"; import { cn } from "@/lib/utils"; +import { copyText } from "@/lib/clipboard"; interface MarkdownContentProps { content: string; className?: string; } +interface CodeBlockProps { + language: string; + value: string; +} + +const CodeBlock = React.memo(({ language, value }) => { + const [copied, setCopied] = useState(false); + const handleCopy = useCallback(async () => { + if (await copyText(value)) { + setCopied(true); + setTimeout(() => setCopied(false), 2000); + } else { + toast.error("Couldn't copy to clipboard."); + } + }, [value]); + + return ( +
+ + + {value} + +
+ ); +}); +CodeBlock.displayName = "CodeBlock"; + export const MarkdownContent = React.memo( ({ content, className = "" }) => { return ( @@ -36,29 +97,10 @@ export const MarkdownContent = React.memo( }) { const match = /language-(\w+)/.exec(className || ""); return !inline && match ? ( - - {String(children).replace(/\n$/, "")} - + value={String(children).replace(/\n$/, "")} + /> ) : (