handle edge cases

This commit is contained in:
Jing Hua 2023-03-06 00:50:18 +08:00
parent 46642e24c2
commit a2b3c5e884
3 changed files with 23 additions and 3 deletions

View file

@ -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);
}
}
}, []);

View file

@ -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();

View file

@ -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'
);