interstellar_ai/app/components/ConversationFrontend.tsx

88 lines
2.8 KiB
TypeScript
Raw Normal View History

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-10-01 14:29:15 +02:00
onStopClick: () => void;
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 14:29:15 +02:00
({ messages,onStopClick, onResendClick, onEditClick, onCopyClick, isClicked }, ref: ForwardedRef<HTMLDivElement>) => {
2024-10-01 10:39:21 +02:00
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 14:29:15 +02:00
useEffect(() => {
if (messagesEndRef.current) {
const rect = messagesEndRef.current.getBoundingClientRect();
console.log('Position of the target div:');
console.log('Top:', rect.top); // Distance from the top of the viewport
console.log('Left:', rect.left); // Distance from the left of the viewport
console.log('Width:', rect.width); // Width of the element
console.log('Height:', rect.height); // Height of the element
}
}, [messages]);
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) {
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
>
<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">
2024-10-01 14:29:15 +02:00
<div className="tooltip">
<button type="button" onClick={onStopClick}>
<img src="/img/resend.svg" alt="stop" />
</button>
<span className="tooltiptext">Stop</span>
</div>
<div className="tooltip">
<button type="button" onClick={onResendClick}>
<img src="/img/resend.svg" alt="resend" />
</button>
<span className="tooltiptext">Resend</span>
</div>
<div className="tooltip">
<button type="button" onClick={onEditClick}>
<img src="/img/edit.svg" alt="edit" />
</button>
<span className="tooltiptext">Edit</span>
</div>
<div className="tooltip">
<button type="button" onClick={onCopyClick}>
<img src="/img/copy.svg" alt="copy" />
</button>
<span className="tooltiptext">{isClicked?"Copied!": "Copy" }</span>
</div>
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;