import { useCallback, useEffect } from "react"; import IconChatAdd from "virtual:icons/mdi/chat-add"; import IconClose from "virtual:icons/mdi/close"; import GetStarted from "./GetStarted"; import GetStartedFooter from "./GetStartedFooter"; import Input from "./Input"; import Layout from "./Layout"; import MessagesList from "./MessagesList"; import TopicSelector from "./TopicSelector"; import { useChat, useI18n, useOptions } from "../composables"; import { chatEventBus } from "../event-buses"; import "./Chat.scss"; function emitScrollToBottom() { if (typeof queueMicrotask === "function") { queueMicrotask(() => chatEventBus.emit("scrollToBottom")); return; } setTimeout(() => chatEventBus.emit("scrollToBottom"), 0); } export default function Chat() { const { t } = useI18n(); const { options } = useOptions(); const { messages, initialMessages, currentSessionId, loadPreviousSession, startNewSession, isTopicSelectionPending, } = useChat(); const shouldShowWelcomeScreen = !currentSessionId && options.showWelcomeScreen; const handleGetStarted = useCallback(async () => { if (!startNewSession) return; await startNewSession(); emitScrollToBottom(); }, [startNewSession]); const handleNewChat = useCallback(async () => { if (!startNewSession) return; await startNewSession(); emitScrollToBottom(); }, [startNewSession]); useEffect(() => { let cancelled = false; const initialize = async () => { let hasRestoredSession = false; if (loadPreviousSession && !currentSessionId) { hasRestoredSession = await loadPreviousSession(); if (!cancelled) { emitScrollToBottom(); } } if ( !options.showWelcomeScreen && !hasRestoredSession && !currentSessionId && startNewSession ) { await startNewSession(); if (!cancelled) { emitScrollToBottom(); } } }; void initialize(); return () => { cancelled = true; }; }, [ currentSessionId, loadPreviousSession, options.showWelcomeScreen, startNewSession, ]); const closeChat = () => { chatEventBus.emit("close"); }; return (
{t("subtitle")}
}