Skip to content

add copy to clipboard for messages #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"eslint": "8.36.0",
"eslint-config-next": "13.2.4",
"eventsource-parser": "^1.0.0",
"framer-motion": "^10.12.22",
"next": "13.2.4",
"openai": "^3.2.1",
"parse-numeric-range": "^1.3.0",
Expand Down
37 changes: 32 additions & 5 deletions src/components/chat/ChatMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { useOpenAI } from "@/context/OpenAIProvider";
import { motion } from "framer-motion";
import { OpenAIChatMessage } from "@/utils/OpenAI";
import React from "react";
import { MdPerson, MdSmartToy } from "react-icons/md";
import {
MdPerson,
MdSmartToy,
MdContentCopy,
MdCheckCircle,
} from "react-icons/md";
import AssistantMessageContent from "./AssistantMessageContent";
import UserMessageContent from "./UserMessageContent";
import useCopyToClipboard from "../hooks/useCopyToClipboard";

type Props = {
message: OpenAIChatMessage;
};

export default function ChatMessage({ message: { id, role, content } }: Props) {
const [hover, setHover] = React.useState(false);
const [copied, handleCopy] = useCopyToClipboard(content);

return (
<div
Expand All @@ -20,22 +27,42 @@ export default function ChatMessage({ message: { id, role, content } }: Props) {
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
>
<div className="relative max-w-screen mx-auto flex w-full max-w-4xl flex-row items-center">
<div className="max-w-screen relative mx-auto flex w-full max-w-4xl flex-row items-center">
<div
className={`flex sticky top-0 my-4 h-10 w-10 items-center justify-center text-4xl mr-2 self-start transition-colors ${
className={`sticky top-0 my-4 mr-2 flex h-10 w-10 items-center justify-center self-start text-4xl transition-colors ${
hover ? "text-stone-300" : "text-primary/20"
}`}
>
{role === "user" ? <MdPerson /> : <MdSmartToy />}
</div>
<div className="overflow-x-auto">
<div className="text-md prose w-full max-w-4xl rounded p-4 text-primary dark:prose-invert prose-code:text-primary prose-pre:bg-transparent prose-pre:p-0">
<div className="text-md prose w-full max-w-4xl rounded p-4 pr-16 text-primary dark:prose-invert prose-code:text-primary prose-pre:bg-transparent prose-pre:p-0">
{role === "user" ? (
<UserMessageContent content={content} />
) : (
<AssistantMessageContent content={content} />
)}
</div>
<div
className={`absolute right-1 top-6 h-10 w-10 cursor-pointer text-xl transition-none duration-500 ease-in-out ${
copied ? "text-emerald-600" : "text-gray-500"
}`}
onClick={handleCopy}
>
{copied ? (
<motion.div
animate={{ scale: [1, 1.25, 1] }}
transition={{
type: "tween",
duration: 0.25,
}}
>
<MdCheckCircle />
</motion.div>
) : (
<MdContentCopy />
)}
</div>
</div>
</div>
</div>
Expand Down
27 changes: 27 additions & 0 deletions src/components/hooks/useCopyToClipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useState, useEffect } from "react";

type UseCopyToClipboardOutput = [boolean, () => void];

const useCopyToClipboard = (textToCopy: string): UseCopyToClipboardOutput => {
const [copied, setCopied] = useState<boolean>(false);

const handleCopy = (): void => {
navigator.clipboard.writeText(textToCopy);
setCopied(true);
};

useEffect(() => {
if (copied) {
const timer = setTimeout(() => {
setCopied(false);
}, 2500);

return () => clearTimeout(timer);
}
}, [copied]);

return [copied, handleCopy];
};

export default useCopyToClipboard;