2023-06-15 12:52:46 +03:00
|
|
|
/* eslint-disable */
|
2023-06-11 00:59:16 +03:00
|
|
|
"use client";
|
2023-06-22 18:50:06 +03:00
|
|
|
import { useCallback, useEffect, useRef } from "react";
|
2023-06-15 12:52:46 +03:00
|
|
|
|
2023-06-13 17:33:41 +03:00
|
|
|
import Card from "@/lib/components/ui/Card";
|
2023-06-22 18:50:06 +03:00
|
|
|
import { useChat } from "../../[chatId]/hooks/useChat";
|
2023-06-15 12:52:46 +03:00
|
|
|
import { ChatMessage } from "./ChatMessage";
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-15 12:52:46 +03:00
|
|
|
export const ChatMessages = (): JSX.Element => {
|
2023-06-11 00:59:16 +03:00
|
|
|
const lastChatRef = useRef<HTMLDivElement | null>(null);
|
2023-06-22 18:50:06 +03:00
|
|
|
const { history } = useChat();
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
const scrollToBottom = useCallback(() => {
|
|
|
|
if (lastChatRef.current) {
|
|
|
|
lastChatRef.current.scrollIntoView({
|
|
|
|
behavior: "smooth",
|
|
|
|
block: "start",
|
|
|
|
});
|
2023-06-15 12:52:46 +03:00
|
|
|
}
|
2023-06-22 18:50:06 +03:00
|
|
|
}, []);
|
2023-06-11 16:20:59 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
useEffect(() => {
|
|
|
|
scrollToBottom();
|
|
|
|
}, [history, scrollToBottom]);
|
2023-06-11 00:59:16 +03:00
|
|
|
|
|
|
|
return (
|
2023-06-11 11:44:23 +03:00
|
|
|
<Card className="p-5 max-w-3xl w-full flex flex-col h-full mb-8">
|
|
|
|
<div className="flex-1">
|
2023-06-22 18:50:06 +03:00
|
|
|
{history.length === 0 ? (
|
2023-06-11 00:59:16 +03:00
|
|
|
<div className="text-center opacity-50">
|
|
|
|
Ask a question, or describe a task.
|
|
|
|
</div>
|
|
|
|
) : (
|
2023-06-22 18:50:06 +03:00
|
|
|
history.map(({ assistant, message_id, user_message }, idx) => (
|
|
|
|
<>
|
|
|
|
<ChatMessage
|
|
|
|
key={message_id}
|
|
|
|
speaker={"user"}
|
|
|
|
text={user_message}
|
|
|
|
/>
|
2023-06-11 16:20:59 +03:00
|
|
|
<ChatMessage
|
2023-06-22 18:50:06 +03:00
|
|
|
key={message_id}
|
|
|
|
speaker={"assistant"}
|
|
|
|
text={assistant}
|
2023-06-11 16:20:59 +03:00
|
|
|
/>
|
2023-06-22 18:50:06 +03:00
|
|
|
</>
|
|
|
|
))
|
2023-06-11 00:59:16 +03:00
|
|
|
)}
|
2023-06-22 18:50:06 +03:00
|
|
|
<div ref={lastChatRef} />
|
2023-06-11 00:59:16 +03:00
|
|
|
</div>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default ChatMessages;
|