2023-06-27 12:28:09 +03:00
|
|
|
import React from "react";
|
2023-08-07 15:13:41 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-06-15 12:52:46 +03:00
|
|
|
|
2023-07-03 18:39:59 +03:00
|
|
|
import { useChatContext } from "@/lib/context";
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-07-03 18:39:59 +03:00
|
|
|
import { ChatMessage } from "./components/ChatMessage/components/ChatMessage";
|
2023-08-25 13:05:04 +03:00
|
|
|
import { useChatMessages } from "./hooks/useChatMessages";
|
2023-06-11 16:20:59 +03:00
|
|
|
|
2023-06-27 12:28:09 +03:00
|
|
|
export const ChatMessages = (): JSX.Element => {
|
|
|
|
const { history } = useChatContext();
|
2023-08-07 15:13:41 +03:00
|
|
|
const { t } = useTranslation(["chat"]);
|
2023-08-25 13:05:04 +03:00
|
|
|
const { chatListRef } = useChatMessages();
|
2023-06-11 00:59:16 +03:00
|
|
|
|
|
|
|
return (
|
2023-08-11 11:06:20 +03:00
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
2023-08-25 13:05:04 +03:00
|
|
|
flex: 1,
|
|
|
|
overflowY: "auto",
|
2023-08-11 11:06:20 +03:00
|
|
|
}}
|
2023-08-25 13:05:04 +03:00
|
|
|
ref={chatListRef}
|
2023-06-27 12:28:09 +03:00
|
|
|
>
|
2023-08-11 11:06:20 +03:00
|
|
|
{history.length === 0 ? (
|
|
|
|
<div
|
|
|
|
data-testid="empty-history-message"
|
|
|
|
className="text-center opacity-50"
|
|
|
|
>
|
|
|
|
{t("ask", { ns: "chat" })}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="flex flex-col gap-3">
|
|
|
|
{history.map(
|
|
|
|
({
|
|
|
|
assistant,
|
|
|
|
message_id,
|
|
|
|
user_message,
|
|
|
|
brain_name,
|
|
|
|
prompt_title,
|
|
|
|
}) => (
|
|
|
|
<React.Fragment key={message_id}>
|
|
|
|
<ChatMessage
|
|
|
|
key={`user-${message_id}`}
|
|
|
|
speaker={"user"}
|
|
|
|
text={user_message}
|
|
|
|
promptName={prompt_title}
|
|
|
|
brainName={brain_name}
|
|
|
|
/>
|
|
|
|
<ChatMessage
|
|
|
|
key={`assistant-${message_id}`}
|
|
|
|
speaker={"assistant"}
|
|
|
|
text={assistant}
|
|
|
|
brainName={brain_name}
|
|
|
|
promptName={prompt_title}
|
|
|
|
/>
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-06-11 00:59:16 +03:00
|
|
|
);
|
|
|
|
};
|