From 65b4fd4d636f266c526882ff328d8a1f8e4dd750 Mon Sep 17 00:00:00 2001 From: Igor Berlenko Date: Sun, 28 Jan 2024 15:00:26 +0800 Subject: [PATCH] Update App.tsx - reference chat name in url hash #515 --- src/App.tsx | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 7f9fb8d63..306126e94 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -18,6 +18,12 @@ function App() { const setApiKey = useStore((state) => state.setApiKey); const setCurrentChatIndex = useStore((state) => state.setCurrentChatIndex); + const urlChatTitle = window.location.hash.substring(1); + + const findChatIndexByTitle = (chats: ChatInterface[], title: string) => { + return chats.findIndex((chat) => chat.title === title); + } + useEffect(() => { document.documentElement.lang = i18n.language; i18n.on('languageChanged', (lng) => { @@ -44,12 +50,12 @@ function App() { } if (oldChats) { - // legacy local storage try { const chats: ChatInterface[] = JSON.parse(oldChats); if (chats.length > 0) { + chatIndexToSet = urlChatTitle ? findChatIndexByTitle(chats, urlChatTitle) : 0; setChats(chats); - setCurrentChatIndex(0); + setCurrentChatIndex(chatIndexToSet !== -1 ? chatIndexToSet : 0); } else { initialiseNewChat(); } @@ -57,20 +63,15 @@ function App() { console.log(e); initialiseNewChat(); } + localStorage.removeItem('chats'); - } 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); + } else if (chatsFromStore && chatsFromStore.length > 0) { + chatIndexToSet = urlChatTitle ? findChatIndexByTitle(chatsFromStore, urlChatTitle) : 0; + if (chatIndexToSet !== -1) { + setCurrentChatIndex(chatIndexToSet); } + } else { + initialiseNewChat(); } }, []);