2023-06-11 00:59:16 +03:00
|
|
|
"use client";
|
2023-06-13 17:33:41 +03:00
|
|
|
import Card from "@/lib/components/ui/Card";
|
|
|
|
import useChatsContext from "@/lib/context/ChatsProvider/hooks/useChatsContext";
|
2023-06-11 15:34:36 +03:00
|
|
|
import { FC, useEffect, useRef } from "react";
|
2023-06-11 00:59:16 +03:00
|
|
|
import ChatMessage from "./ChatMessage";
|
|
|
|
|
2023-06-11 15:34:36 +03:00
|
|
|
export const ChatMessages: FC = () => {
|
2023-06-11 00:59:16 +03:00
|
|
|
const lastChatRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
2023-06-11 15:34:36 +03:00
|
|
|
const { chat } = useChatsContext();
|
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
useEffect(() => {
|
2023-06-11 16:20:59 +03:00
|
|
|
if (!chat || !lastChatRef.current) return;
|
|
|
|
|
|
|
|
// if (chat.history.length > 2) {
|
|
|
|
lastChatRef.current?.scrollIntoView({
|
|
|
|
behavior: "smooth",
|
|
|
|
block: "end",
|
|
|
|
});
|
|
|
|
// }
|
|
|
|
}, [chat, lastChatRef]);
|
|
|
|
|
2023-06-11 15:34:36 +03:00
|
|
|
if (!chat) return null;
|
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-11 00:59:16 +03:00
|
|
|
{chat.history.length === 0 ? (
|
|
|
|
<div className="text-center opacity-50">
|
|
|
|
Ask a question, or describe a task.
|
|
|
|
</div>
|
|
|
|
) : (
|
2023-06-11 16:20:59 +03:00
|
|
|
chat.history.map(([speaker, text], idx) => {
|
|
|
|
return (
|
|
|
|
<ChatMessage
|
|
|
|
ref={idx === chat.history.length - 1 ? lastChatRef : null}
|
|
|
|
key={idx}
|
|
|
|
speaker={speaker}
|
|
|
|
text={text}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})
|
2023-06-11 00:59:16 +03:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default ChatMessages;
|