From 8bfe692a407f6dba81247ed50f6933dce0d41316 Mon Sep 17 00:00:00 2001 From: Jing Hua Date: Sat, 4 Mar 2023 08:02:49 +0800 Subject: [PATCH] chat naming improvements --- src/App.tsx | 30 +++++++++++++++---- .../Chat/ChatContent/ChatContent.tsx | 4 +-- .../ChatContent/Message/NewMessageButton.tsx | 10 ++++++- src/components/Menu/NewChat.tsx | 10 ++++++- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index aad4b81..6d7c4bf 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -8,7 +8,8 @@ import ConfigMenu from './components/ConfigMenu'; import useSaveToLocalStorage from '@hooks/useSaveToLocalStorage'; import useUpdateCharts from '@hooks/useUpdateChats'; -import { ChatInterface } from '@type/chat'; +import { ChatInterface, MessageInterface } from '@type/chat'; +import { defaultSystemMessage } from '@constants/chat'; function App() { useSaveToLocalStorage(); @@ -20,15 +21,34 @@ function App() { state.setCurrentChatIndex, ]); + const initialiseNewChat = () => { + const message: MessageInterface = { + role: 'system', + content: defaultSystemMessage, + }; + setChats([ + { + title: 'New Chat', + messages: [message], + }, + ]); + setMessages([message]); + setCurrentChatIndex(0); + }; + useEffect(() => { // localStorage.removeItem('chats'); const storedChats = localStorage.getItem('chats'); if (storedChats) { try { const chats: ChatInterface[] = JSON.parse(storedChats); - setChats(chats); - setMessages(chats[0].messages); - setCurrentChatIndex(0); + if (chats.length > 0) { + setChats(chats); + setMessages(chats[0].messages); + setCurrentChatIndex(0); + } else { + initialiseNewChat(); + } } catch (e: unknown) { setChats([]); setMessages([]); @@ -36,7 +56,7 @@ function App() { console.log(e); } } else { - setChats([]); + initialiseNewChat(); } }, []); diff --git a/src/components/Chat/ChatContent/ChatContent.tsx b/src/components/Chat/ChatContent/ChatContent.tsx index 2250702..ed09be0 100644 --- a/src/components/Chat/ChatContent/ChatContent.tsx +++ b/src/components/Chat/ChatContent/ChatContent.tsx @@ -98,12 +98,12 @@ const ChatContent = () => { > Submit - + */}
diff --git a/src/components/Chat/ChatContent/Message/NewMessageButton.tsx b/src/components/Chat/ChatContent/Message/NewMessageButton.tsx index a6fa240..3cfc277 100644 --- a/src/components/Chat/ChatContent/Message/NewMessageButton.tsx +++ b/src/components/Chat/ChatContent/Message/NewMessageButton.tsx @@ -26,8 +26,16 @@ const NewMessageButton = ({ messageIndex }: { messageIndex: number }) => { const addChat = () => { if (chats) { const updatedChats: ChatInterface[] = JSON.parse(JSON.stringify(chats)); + let titleIndex = 1; + let title = `New Chat ${titleIndex}`; + + while (chats.some((chat) => chat.title === title)) { + titleIndex += 1; + title = `New Chat ${titleIndex}`; + } + updatedChats.unshift({ - title: `Chat ${Math.random()}`, + title, messages: [{ role: 'system', content: defaultSystemMessage }], }); setChats(updatedChats); diff --git a/src/components/Menu/NewChat.tsx b/src/components/Menu/NewChat.tsx index 40774b8..8e99c21 100644 --- a/src/components/Menu/NewChat.tsx +++ b/src/components/Menu/NewChat.tsx @@ -19,8 +19,16 @@ const NewChat = () => { const addChat = () => { if (chats) { const updatedChats: ChatInterface[] = JSON.parse(JSON.stringify(chats)); + let titleIndex = 1; + let title = `New Chat ${titleIndex}`; + + while (chats.some((chat) => chat.title === title)) { + titleIndex += 1; + title = `New Chat ${titleIndex}`; + } + updatedChats.unshift({ - title: `Chat ${Math.random()}`, + title, messages: [{ role: 'system', content: defaultSystemMessage }], }); setChats(updatedChats);