interstellar_ai/app/components/ConversationFrontend.tsx
2024-10-01 11:57:38 +02:00

61 lines
1.8 KiB
TypeScript

import React, { ForwardedRef, useEffect, useRef } from 'react';
type Message = {
role: string
content: string
}
interface ConversationProps {
messages: Message[];
onResendClick: () => void;
onEditClick: () => void;
onCopyClick: () => void;
isClicked: boolean
}
const ConversationFrontend = React.forwardRef<HTMLDivElement, ConversationProps>(
({ messages, onResendClick, onEditClick, onCopyClick, isClicked }, ref: ForwardedRef<HTMLDivElement>) => {
const messagesEndRef = useRef<HTMLDivElement | null>(null)
useEffect(() => {
messagesEndRef.current?.scrollIntoView()
}, [messages])
return (
<div className="output" ref={ref}>
<div className="conversation resize" id="conversation">
{messages.map((message, index) => {
if (index >= 1) {
return (
<div
key={index}
className={message.role === "user" ? 'user-message' : 'ai-message'}
>
<p> {message.content}</p>
</div>
);
}
})}
<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>
<p id="copiedText" style={{ opacity: isClicked ? "1" : "0", transition: "all 0.3s ease-in-out" }}>Copied!</p>
</div>
<div ref={messagesEndRef} />
</div>
</div>
);
}
);
export default ConversationFrontend;