import { Button, Stack } from "@mui/joy"; import { head } from "lodash-es"; import React, { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { useTranslate } from "@/utils/i18n"; import * as api from "@/helpers/api"; import useLoading from "@/hooks/useLoading"; import { useMessageStore } from "@/store/zustand/message"; import { Conversation, useConversationStore } from "@/store/zustand/conversation"; import Icon from "@/components/Icon"; import { generateUUID } from "@/utils/uuid"; import MobileHeader from "@/components/MobileHeader"; import ChatMessage from "@/components/MemoChat/ChatMessage"; import ChatInput from "@/components/MemoChat/ChatInput"; import ConversationTab from "@/components/MemoChat/ConversationTab"; import Empty from "@/components/Empty"; const MemoChat = () => { const t = useTranslate(); const fetchingState = useLoading(false); const [isEnabled, setIsEnabled] = useState(true); const [isInIME, setIsInIME] = useState(false); const [question, setQuestion] = useState(""); const conversationStore = useConversationStore(); const conversationList = conversationStore.conversationList; const [selectedConversationId, setSelectedConversationId] = useState(head(conversationList)?.messageStorageId || ""); const messageStore = useMessageStore(selectedConversationId)(); const messageList = messageStore.messageList; // The state didn't show in component, just for trigger re-render const [message, setMessage] = useState(""); useEffect(() => { api.checkOpenAIEnabled().then(({ data }) => { setIsEnabled(data); }); }, []); // to new a conversation when no conversation useEffect(() => { if (conversationList.length === 0) { newConversation(); } }, [conversationList]); // to select head message conversation(conversation) when conversation be deleted useEffect(() => { setSelectedConversationId(head(conversationList)?.messageStorageId || ""); }, [conversationList]); const handleGotoSystemSetting = () => { window.open(`/setting`); }; const handleQuestionTextareaChange = async (event: React.ChangeEvent) => { setQuestion(event.currentTarget.value); }; const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === "Enter" && !event.shiftKey && !isInIME) { event.preventDefault(); handleSendQuestionButtonClick().then(); } }; const handleSendQuestionButtonClick = async () => { if (!question) { return; } fetchingState.setLoading(); setQuestion(""); messageStore.addMessage({ id: generateUUID(), role: "user", content: question, }); const messageId = generateUUID(); messageStore.addMessage({ id: messageId, role: "assistant", content: "", }); try { fetchChatStreaming(messageId); } catch (error: any) { console.error(error); toast.error(error.response.data.error); } }; const fetchChatStreaming = async (messageId: string) => { const messageList = messageStore.getState().messageList; await api.chatStreaming( messageList, async (event: any) => { messageStore.updateMessage(messageId, event.data); // to trigger re-render setMessage(message + event.data); }, async () => { fetchingState.setFinish(); } ); }; const newConversation = () => { const uuid = generateUUID(); // get the time HH:mm as the default name const name = new Date().toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit", }); conversationStore.addConversation({ name: name, messageStorageId: uuid, }); setSelectedConversationId(uuid); }; return (
{t("memo-chat.title")}
{conversationList.map((item: Conversation) => ( { // this is very important. otherwise, the select event also be clicked. e.stopPropagation(); conversationStore.removeConversation(item); toast.success("Remove successfully"); }} /> ))}
{messageList.length == 0 && (

{t("memo-chat.no-message")}

)} {messageList.map((message, index) => ( ))}
{fetchingState.isLoading && (

)} {!isEnabled && (

{t("memo-chat.not-enabled")}

)}
); }; export default MemoChat;