2024-09-18 15:52:51 +02:00
|
|
|
import React, { ForwardedRef, useEffect, useRef } from 'react';
|
2024-09-18 10:03:36 +02:00
|
|
|
|
2024-09-20 09:17:28 +02:00
|
|
|
type Message = {
|
|
|
|
role: string
|
|
|
|
content: string
|
|
|
|
}
|
|
|
|
|
2024-09-18 10:03:36 +02:00
|
|
|
interface ConversationProps {
|
2024-09-20 09:17:28 +02:00
|
|
|
messages: Message[];
|
2024-09-18 11:17:34 +02:00
|
|
|
onResendClick: () => void;
|
|
|
|
onEditClick: () => void;
|
|
|
|
onCopyClick: () => void;
|
2024-10-01 09:22:33 +02:00
|
|
|
isClicked: boolean
|
2024-09-18 10:03:36 +02:00
|
|
|
}
|
|
|
|
|
2024-09-18 14:52:04 +02:00
|
|
|
const ConversationFrontend = React.forwardRef<HTMLDivElement, ConversationProps>(
|
2024-10-01 10:39:21 +02:00
|
|
|
({ messages, onResendClick, onEditClick, onCopyClick, isClicked }, ref: ForwardedRef<HTMLDivElement>) => {
|
|
|
|
|
2024-10-01 11:57:38 +02:00
|
|
|
const messagesEndRef = useRef<HTMLDivElement | null>(null)
|
2024-10-01 11:37:53 +02:00
|
|
|
|
2024-10-01 11:57:38 +02:00
|
|
|
useEffect(() => {
|
2024-10-01 11:37:53 +02:00
|
|
|
messagesEndRef.current?.scrollIntoView()
|
2024-10-01 11:57:38 +02:00
|
|
|
}, [messages])
|
2024-10-01 10:39:21 +02:00
|
|
|
|
2024-09-18 11:17:34 +02:00
|
|
|
return (
|
2024-10-01 10:39:21 +02:00
|
|
|
<div className="output" ref={ref}>
|
|
|
|
<div className="conversation resize" id="conversation">
|
2024-09-18 11:17:34 +02:00
|
|
|
{messages.map((message, index) => {
|
2024-10-01 11:57:38 +02:00
|
|
|
if (index >= 1) {
|
2024-09-27 16:51:44 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
key={index}
|
2024-10-01 10:39:21 +02:00
|
|
|
className={message.role === "user" ? 'user-message' : 'ai-message'}
|
2024-10-01 11:57:38 +02:00
|
|
|
>
|
2024-09-27 16:51:44 +02:00
|
|
|
<p> {message.content}</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2024-09-18 11:17:34 +02:00
|
|
|
})}
|
2024-10-01 10:39:21 +02:00
|
|
|
|
2024-09-18 11:17:34 +02:00
|
|
|
<div className="button-container">
|
|
|
|
<button type="button" onClick={onResendClick}>
|
|
|
|
<img src="/img/resend.svg" alt="resend" />
|
|
|
|
</button>
|
|
|
|
<button type="button" onClick={onEditClick}>
|
|
|
|
<img src="/img/edit.svg" alt="edit" />
|
|
|
|
</button>
|
|
|
|
<button type="button" onClick={onCopyClick}>
|
|
|
|
<img src="/img/copy.svg" alt="copy" />
|
|
|
|
</button>
|
2024-10-01 11:57:38 +02:00
|
|
|
<p id="copiedText" style={{ opacity: isClicked ? "1" : "0", transition: "all 0.3s ease-in-out" }}>Copied!</p>
|
2024-09-18 11:17:34 +02:00
|
|
|
</div>
|
2024-10-01 11:37:53 +02:00
|
|
|
<div ref={messagesEndRef} />
|
2024-10-01 11:57:38 +02:00
|
|
|
</div>
|
2024-09-18 10:03:36 +02:00
|
|
|
</div>
|
2024-09-18 11:17:34 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2024-09-18 10:03:36 +02:00
|
|
|
|
2024-09-18 14:52:04 +02:00
|
|
|
export default ConversationFrontend;
|