diff --git a/src/App.tsx b/src/App.tsx index fd5ecb5..a948f4c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -51,9 +51,16 @@ function App() { } else { // existing local storage const chats = useStore.getState().chats; + const currentChatIndex = useStore.getState().currentChatIndex; if (!chats || chats.length === 0) { initialiseNewChat(); } + if ( + chats && + !(currentChatIndex >= 0 && currentChatIndex < chats.length) + ) { + setCurrentChatIndex(0); + } } }, []); diff --git a/src/components/Chat/ChatContent/ChatContent.tsx b/src/components/Chat/ChatContent/ChatContent.tsx index 8e44537..4c7a398 100644 --- a/src/components/Chat/ChatContent/ChatContent.tsx +++ b/src/components/Chat/ChatContent/ChatContent.tsx @@ -14,10 +14,20 @@ const ChatContent = () => { const inputRole = useStore((state) => state.inputRole); const setError = useStore((state) => state.setError); const messages = useStore((state) => - state.chats ? state.chats[state.currentChatIndex].messages : [] + state.chats && + state.chats.length > 0 && + state.currentChatIndex >= 0 && + state.currentChatIndex < state.chats.length + ? state.chats[state.currentChatIndex].messages + : [] ); const stickyIndex = useStore((state) => - state.chats ? state.chats[state.currentChatIndex].messages.length : 0 + state.chats && + state.chats.length > 0 && + state.currentChatIndex >= 0 && + state.currentChatIndex < state.chats.length + ? state.chats[state.currentChatIndex].messages.length + : 0 ); const { handleSubmit, error } = useSubmit(); diff --git a/src/components/MobileBar/MobileBar.tsx b/src/components/MobileBar/MobileBar.tsx index 29812ec..0288830 100644 --- a/src/components/MobileBar/MobileBar.tsx +++ b/src/components/MobileBar/MobileBar.tsx @@ -6,7 +6,10 @@ import useAddChat from '@hooks/useAddChat'; const MobileBar = () => { const chatTitle = useStore((state) => - state.chats && state.chats.length > 0 + state.chats && + state.chats.length > 0 && + state.currentChatIndex >= 0 && + state.currentChatIndex < state.chats.length ? state.chats[state.currentChatIndex].title : 'New Chat' );