2025-03-11 11:02:30 +08:00
|
|
|
import { useState } from "react";
|
2025-05-13 11:24:07 +08:00
|
|
|
import clsx from "clsx";
|
2025-03-11 11:02:30 +08:00
|
|
|
|
|
|
|
|
import { CopyButton } from "@/components/Common/CopyButton";
|
|
|
|
|
|
|
|
|
|
interface UserMessageProps {
|
|
|
|
|
messageContent: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const UserMessage = ({ messageContent }: UserMessageProps) => {
|
|
|
|
|
const [showCopyButton, setShowCopyButton] = useState(false);
|
|
|
|
|
|
2025-06-17 15:39:18 +08:00
|
|
|
const handleDoubleClick = (e: React.MouseEvent<HTMLDivElement>) => {
|
|
|
|
|
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
|
|
|
|
|
const selection = window.getSelection();
|
|
|
|
|
const range = document.createRange();
|
|
|
|
|
|
|
|
|
|
if (e.currentTarget && selection && range) {
|
|
|
|
|
try {
|
|
|
|
|
range.selectNodeContents(e.currentTarget);
|
|
|
|
|
selection.removeAllRanges();
|
|
|
|
|
selection.addRange(range);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Selection failed:', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-11 11:02:30 +08:00
|
|
|
return (
|
|
|
|
|
<div
|
2025-07-21 21:17:20 +08:00
|
|
|
className="max-w-full flex gap-1 items-center justify-end"
|
2025-03-11 11:02:30 +08:00
|
|
|
onMouseEnter={() => setShowCopyButton(true)}
|
|
|
|
|
onMouseLeave={() => setShowCopyButton(false)}
|
|
|
|
|
>
|
2025-05-13 11:24:07 +08:00
|
|
|
<div
|
|
|
|
|
className={clsx("size-6 transition", {
|
|
|
|
|
"opacity-0": !showCopyButton,
|
|
|
|
|
})}
|
|
|
|
|
>
|
|
|
|
|
<CopyButton textToCopy={messageContent} />
|
|
|
|
|
</div>
|
2025-03-11 11:02:30 +08:00
|
|
|
<div
|
2025-06-10 19:25:54 +08:00
|
|
|
className="max-w-[85%] overflow-auto text-left px-3 py-2 bg-white dark:bg-[#202126] rounded-xl border border-black/12 dark:border-black/15 font-normal text-sm text-[#333333] dark:text-[#D8D8D8] cursor-pointer user-select-text whitespace-pre-wrap"
|
2025-06-17 15:39:18 +08:00
|
|
|
onDoubleClick={handleDoubleClick}
|
2025-03-11 11:02:30 +08:00
|
|
|
>
|
|
|
|
|
{messageContent}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|