From 262d6904d36d4a2189d18f3930ba22d3f779fa58 Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 19 Mar 2023 20:30:57 +0800 Subject: [PATCH] feat: update chat view response style --- components/ChatView/Header.tsx | 25 +++++++++++++++---------- components/ChatView/MessageTextarea.tsx | 9 +-------- components/ChatView/index.tsx | 19 +++++++------------ pages/chat.tsx | 4 ++-- store/user.ts | 4 ++-- types/chat.ts | 1 - 6 files changed, 27 insertions(+), 35 deletions(-) diff --git a/components/ChatView/Header.tsx b/components/ChatView/Header.tsx index 1c5348f..35a819c 100644 --- a/components/ChatView/Header.tsx +++ b/components/ChatView/Header.tsx @@ -1,4 +1,4 @@ -import { Menu } from "@headlessui/react"; +import { Menu, Popover } from "@headlessui/react"; import Link from "next/link"; import { toast } from "react-hot-toast"; import { useChatStore, useMessageStore, useUserStore } from "../../store"; @@ -9,7 +9,7 @@ const Header = () => { const chatStore = useChatStore(); const messageStore = useMessageStore(); const currentChat = chatStore.currentChat; - const chatTitle = currentChat ? userStore.getAssistantById(currentChat.assistantId)?.name : "No chat"; + const assistant = userStore.getAssistantById(currentChat?.assistantId)!; const handleClearMessage = () => { messageStore.clearMessage((message) => message.chatId !== currentChat?.id); @@ -17,13 +17,13 @@ const Header = () => { }; return ( -
- +
+
- + @@ -41,14 +41,19 @@ const Header = () => { - - {chatTitle} - +
+ + {assistant.name} + +

{assistant.description}

+
+
+
- +
{
- +
); }; diff --git a/components/ChatView/MessageTextarea.tsx b/components/ChatView/MessageTextarea.tsx index 5cd13ef..f40e230 100644 --- a/components/ChatView/MessageTextarea.tsx +++ b/components/ChatView/MessageTextarea.tsx @@ -38,22 +38,15 @@ const MessageTextarea = (props: Props) => { return; } - const currentChat = chatStore.currentChat; - if (currentChat.isRequesting) { - toast.error("Please wait for the previous message to be sent."); - return; - } - messageStore.addMessage({ id: generateUUID(), - chatId: currentChat.id, + chatId: chatStore.currentChat.id, creatorId: userStore.currentUser.id, createdAt: Date.now(), content: value, }); setValue(""); textareaRef.current!.value = ""; - await sendMessage(); }; diff --git a/components/ChatView/index.tsx b/components/ChatView/index.tsx index bf5a0e8..617bec8 100644 --- a/components/ChatView/index.tsx +++ b/components/ChatView/index.tsx @@ -14,6 +14,7 @@ const ChatView = () => { const messageStore = useMessageStore(); const [messageList, setMessageList] = useState([]); const [currentChat, setCurrentChat] = useState(null); + const [isRequesting, setIsRequesting] = useState(false); const chatViewRef = useRef(null); useEffect(() => { @@ -32,20 +33,17 @@ const ChatView = () => { return; } chatViewRef.current.scrollTop = chatViewRef.current.scrollHeight; - }, [currentChat, currentChat?.isRequesting]); + }, [currentChat, isRequesting]); const sendMessageToCurrentChat = async () => { if (!currentChat || !chatViewRef.current) { return; } - if (currentChat.isRequesting) { + if (isRequesting) { return; } - chatStore.setCurrentChat({ - ...currentChat, - isRequesting: true, - }); + setIsRequesting(true); const messageList = messageStore.getState().messageList.filter((message) => message.chatId === currentChat.id); const { data } = await axios.post("/api/chat", { messages: messageList.map((message) => ({ @@ -60,21 +58,18 @@ const ChatView = () => { createdAt: Date.now(), content: data, }); - chatStore.setCurrentChat({ - ...currentChat, - isRequesting: false, - }); + setIsRequesting(false); }; return (
{messageList.length === 0 ? <> : messageList.map((message) => )} - {currentChat?.isRequesting && ( + {isRequesting && (
Loading...
diff --git a/pages/chat.tsx b/pages/chat.tsx index 4ee81d3..16e7f65 100644 --- a/pages/chat.tsx +++ b/pages/chat.tsx @@ -12,8 +12,8 @@ const ChatPage: NextPage = () => { -
-
+
+
diff --git a/store/user.ts b/store/user.ts index 811afe3..c5e734e 100644 --- a/store/user.ts +++ b/store/user.ts @@ -4,8 +4,8 @@ import { Id, User, UserRole } from "../types"; export const assistantList: User[] = [ { id: "assistant-dba", - name: "Chat DBA", - description: "", + name: "ChatDBA", + description: "🤖️ I am a chatbot that can help you with database administration.", avatar: "", role: UserRole.Assistant, }, diff --git a/types/chat.ts b/types/chat.ts index 7b47795..05d8581 100644 --- a/types/chat.ts +++ b/types/chat.ts @@ -3,5 +3,4 @@ import { Id } from "./common"; export interface Chat { id: string; assistantId: Id; - isRequesting: boolean; }