import { FormEvent, useState, useEffect, useRef } from "react"; import { ChatMessage } from "./ChatMessage"; import "material-symbols"; export { Chat }; interface Message { user: boolean; content: string; withDeepResearch: boolean; } interface ChatProps { assistant: string; messages: Message[]; pushMessage: (message: Message) => void; conversationId: string; setConversationId: (id: string) => void; setAssistantName: (name: string) => void; receivingMsg: boolean; setReceivingMsg: (receiving: boolean) => void; onStartConversation: ( user: string, assistant: string, withDeepResearch: boolean ) => Promise; sendIcon: string; userAvatar: string; botAvatar: string; onFeedback?: (key: string, rating: string) => Promise; } function Chat({ assistant, messages, pushMessage, conversationId, setConversationId, setAssistantName, receivingMsg, setReceivingMsg, onStartConversation, sendIcon, userAvatar, botAvatar, onFeedback, }: ChatProps) { const [input, setInput] = useState(""); const [isDeepResearch, setIsDeepResearch] = useState(false); const bottomRef = useRef(null); async function startConversation() { const newId = await onStartConversation("user", assistant, isDeepResearch); setConversationId(newId); } useEffect(() => { setAssistantName(assistant); startConversation(); }, []); function changeInput(e: FormEvent) { e.preventDefault(); setInput(e.currentTarget.value); } async function handleSubmit(e: FormEvent) { e.preventDefault(); const trimmedInput = input.trim(); if (!trimmedInput) return; pushMessage({ user: true, content: trimmedInput, withDeepResearch: isDeepResearch, }); setInput(""); pushMessage({ user: false, content: trimmedInput, withDeepResearch: isDeepResearch, }); } function toggleDeepResearch() { setIsDeepResearch(!isDeepResearch); } function scrollToBottom() { // @ts-expect-error idk bottomRef.current.scrollIntoView({ behavior: "smooth" }); } return (
{messages.map((message, index) => ( ))}
); }