189 lines
7.3 KiB
TypeScript
189 lines
7.3 KiB
TypeScript
import { Message } from "@/types";
|
|
import { IconEdit } from "@tabler/icons-react";
|
|
import { useTranslation } from "next-i18next";
|
|
import { FC, useEffect, useRef, useState } from "react";
|
|
import ReactMarkdown from "react-markdown";
|
|
import rehypeMathjax from "rehype-mathjax";
|
|
import remarkGfm from "remark-gfm";
|
|
import remarkMath from "remark-math";
|
|
import { CodeBlock } from "../Markdown/CodeBlock";
|
|
import { CopyButton } from "./CopyButton";
|
|
|
|
interface Props {
|
|
message: Message;
|
|
messageIndex: number;
|
|
lightMode: "light" | "dark";
|
|
onEditMessage: (message: Message, messageIndex: number) => void;
|
|
}
|
|
|
|
export const ChatMessage: FC<Props> = ({ message, messageIndex, lightMode, onEditMessage }) => {
|
|
const { t } = useTranslation("chat");
|
|
const [isEditing, setIsEditing] = useState<boolean>(false);
|
|
const [isHovering, setIsHovering] = useState<boolean>(false);
|
|
const [messageContent, setMessageContent] = useState(message.content);
|
|
const [messagedCopied, setMessageCopied] = useState(false);
|
|
|
|
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
|
|
|
const toggleEditing = () => {
|
|
setIsEditing(!isEditing);
|
|
};
|
|
|
|
const handleInputChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
setMessageContent(event.target.value);
|
|
if (textareaRef.current) {
|
|
textareaRef.current.style.height = "inherit";
|
|
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
|
|
}
|
|
};
|
|
|
|
const handleEditMessage = () => {
|
|
if (message.content != messageContent) {
|
|
onEditMessage({ ...message, content: messageContent }, messageIndex);
|
|
}
|
|
setIsEditing(false);
|
|
};
|
|
|
|
const handlePressEnter = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
|
if (e.key === "Enter" && !e.shiftKey) {
|
|
e.preventDefault();
|
|
handleEditMessage();
|
|
}
|
|
};
|
|
|
|
const copyOnClick = () => {
|
|
if (!navigator.clipboard) return;
|
|
|
|
navigator.clipboard.writeText(message.content).then(() => {
|
|
setMessageCopied(true);
|
|
setTimeout(() => {
|
|
setMessageCopied(false);
|
|
}, 2000);
|
|
});
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (textareaRef.current) {
|
|
textareaRef.current.style.height = "inherit";
|
|
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
|
|
}
|
|
}, [isEditing]);
|
|
|
|
return (
|
|
<div
|
|
className={`group ${message.role === "assistant" ? "border-b border-black/10 bg-gray-50 text-gray-800 dark:border-gray-900/50 dark:bg-[#444654] dark:text-gray-100" : "border-b border-black/10 bg-white text-gray-800 dark:border-gray-900/50 dark:bg-[#343541] dark:text-gray-100"}`}
|
|
style={{ overflowWrap: "anywhere" }}
|
|
onMouseEnter={() => setIsHovering(true)}
|
|
onMouseLeave={() => setIsHovering(false)}
|
|
>
|
|
<div className="relative m-auto flex gap-4 p-4 text-base md:max-w-2xl md:gap-6 md:py-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
|
|
<div className="min-w-[40px] font-bold">{message.role === "assistant" ? t("AI") : t("You")}:</div>
|
|
|
|
<div className="prose mt-[-2px] w-full dark:prose-invert">
|
|
{message.role === "user" ? (
|
|
<div className="flex w-full">
|
|
{isEditing ? (
|
|
<div className="flex w-full flex-col">
|
|
<textarea
|
|
ref={textareaRef}
|
|
className="w-full resize-none whitespace-pre-wrap border-none outline-none dark:bg-[#343541]"
|
|
value={messageContent}
|
|
onChange={handleInputChange}
|
|
onKeyDown={handlePressEnter}
|
|
style={{
|
|
fontFamily: "inherit",
|
|
fontSize: "inherit",
|
|
lineHeight: "inherit",
|
|
padding: "0",
|
|
margin: "0",
|
|
overflow: "hidden"
|
|
}}
|
|
/>
|
|
|
|
<div className="mt-10 flex justify-center space-x-4">
|
|
<button
|
|
className="h-[40px] rounded-md bg-blue-500 px-4 py-1 text-sm font-medium text-white enabled:hover:bg-blue-600 disabled:opacity-50"
|
|
onClick={handleEditMessage}
|
|
disabled={messageContent.trim().length <= 0}
|
|
>
|
|
Save & Submit
|
|
</button>
|
|
<button
|
|
className="h-[40px] rounded-md border border-neutral-300 px-4 py-1 text-sm font-medium text-neutral-700 hover:bg-neutral-100 dark:border-neutral-700 dark:text-neutral-300 dark:hover:bg-neutral-800"
|
|
onClick={() => {
|
|
setMessageContent(message.content);
|
|
setIsEditing(false);
|
|
}}
|
|
>
|
|
Cancel
|
|
</button>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<div className="prose whitespace-pre-wrap dark:prose-invert">{message.content}</div>
|
|
)}
|
|
|
|
{(isHovering || window.innerWidth < 640) && !isEditing && (
|
|
<button className={`absolute ${window.innerWidth < 640 ? "right-3 bottom-1" : "right-[-20px] top-[26px]"}`}>
|
|
<IconEdit
|
|
size={20}
|
|
className="text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300"
|
|
onClick={toggleEditing}
|
|
/>
|
|
</button>
|
|
)}
|
|
</div>
|
|
) : (
|
|
<>
|
|
<ReactMarkdown
|
|
className="prose dark:prose-invert"
|
|
remarkPlugins={[remarkGfm, remarkMath]}
|
|
rehypePlugins={[rehypeMathjax]}
|
|
components={{
|
|
code({ node, inline, className, children, ...props }) {
|
|
const match = /language-(\w+)/.exec(className || "");
|
|
return !inline && match ? (
|
|
<CodeBlock
|
|
key={Math.random()}
|
|
language={match[1]}
|
|
value={String(children).replace(/\n$/, "")}
|
|
lightMode={lightMode}
|
|
{...props}
|
|
/>
|
|
) : (
|
|
<code
|
|
className={className}
|
|
{...props}
|
|
>
|
|
{children}
|
|
</code>
|
|
);
|
|
},
|
|
table({ children }) {
|
|
return <table className="border-collapse border border-black py-1 px-3 dark:border-white">{children}</table>;
|
|
},
|
|
th({ children }) {
|
|
return <th className="break-words border border-black bg-gray-500 py-1 px-3 text-white dark:border-white">{children}</th>;
|
|
},
|
|
td({ children }) {
|
|
return <td className="break-words border border-black py-1 px-3 dark:border-white">{children}</td>;
|
|
}
|
|
}}
|
|
>
|
|
{message.content}
|
|
</ReactMarkdown>
|
|
|
|
{(isHovering || window.innerWidth < 640) && (
|
|
<CopyButton
|
|
messagedCopied={messagedCopied}
|
|
copyOnClick={copyOnClick}
|
|
/>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|