Skip to content

Commit 7f7e71e

Browse files
committed
feat: copy code button
chore: add timeout to chat response
1 parent 59f57fc commit 7f7e71e

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

components/ui/markdownRenderer.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,48 @@
11
"use client";
22

3+
import React from "react";
34
import ReactMarkdown from "react-markdown";
45
import remarkGfm from "remark-gfm";
56
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
67
import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism";
78

89
export default function MarkdownRenderer({ content }) {
10+
11+
const [copiedCode, setCopiedCode] = React.useState(null);
12+
const handleCopy = (code) => {
13+
navigator.clipboard.writeText(code)
14+
.then(() => {
15+
setCopiedCode(code);
16+
setTimeout(() => setCopiedCode(null), 2000);
17+
})
18+
.catch((err) => console.error('Failed to copy code: ', err));
19+
};
20+
921
return (
1022
<ReactMarkdown
1123
remarkPlugins={[remarkGfm]}
1224
components={{
1325
code({ node, inline, className, children, ...props }) {
1426
const match = /language-(\w+)/.exec(className || "");
27+
const codeString = String(children).replace(/\n$/, "");
1528
return !inline && match ? (
16-
<SyntaxHighlighter
17-
style={oneDark}
18-
language={match[1]}
19-
PreTag="div"
20-
className="rounded-md"
21-
{...props}
22-
>
23-
{String(children).replace(/\n$/, "")}
24-
</SyntaxHighlighter>
29+
<div className="relative group">
30+
<SyntaxHighlighter
31+
style={oneDark}
32+
language={match[1]}
33+
PreTag="div"
34+
className="rounded-md"
35+
{...props}
36+
>
37+
{codeString}
38+
</SyntaxHighlighter>
39+
<button
40+
onClick={() => handleCopy(codeString)}
41+
className="absolute top-2 right-2 bg-gray-700 hover:bg-gray-600 text-white px-2 py-1 text-sm rounded opacity-0 group-hover:opacity-100 transition-opacity duration-200"
42+
>
43+
{copiedCode === codeString ? "Copied!" : "Copy"}
44+
</button>
45+
</div>
2546
) : (
2647
<code className="bg-muted px-1 py-0.5 rounded text-sm" {...props}>
2748
{children}

hooks/useChat.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export default function useChat() {
3434
{
3535
Message: chatQuery,
3636
queryId: sessionId,
37+
},
38+
{
39+
timeout: 300000, // 300 seconds timeout
3740
}
3841
);
3942

0 commit comments

Comments
 (0)