Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Merge branch 'pr-522-reference-chat-name-in-url-hash' from '7flash:pa…
Browse files Browse the repository at this point in the history
…tch-1'
  • Loading branch information
zewebdev1337 committed May 28, 2024
2 parents d83ce66 + 65b4fd4 commit 02547ad
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -44,33 +50,28 @@ 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();
}
} catch (e: unknown) {
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();
}
}, []);

Expand Down

0 comments on commit 02547ad

Please sign in to comment.