mirror of
https://github.com/NovaOSS/nova-betterchat.git
synced 2024-11-26 00:13:59 +01:00
restore old chats
This commit is contained in:
parent
72a89ada1f
commit
46642e24c2
41
src/App.tsx
41
src/App.tsx
|
@ -5,15 +5,56 @@ import Chat from './components/Chat';
|
||||||
import Menu from './components/Menu';
|
import Menu from './components/Menu';
|
||||||
|
|
||||||
import useInitialiseNewChat from '@hooks/useInitialiseNewChat';
|
import useInitialiseNewChat from '@hooks/useInitialiseNewChat';
|
||||||
|
import { ChatInterface } from '@type/chat';
|
||||||
|
import { Theme } from '@type/theme';
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const initialiseNewChat = useInitialiseNewChat();
|
const initialiseNewChat = useInitialiseNewChat();
|
||||||
|
const setChats = useStore((state) => state.setChats);
|
||||||
|
const setTheme = useStore((state) => state.setTheme);
|
||||||
|
const setApiKey = useStore((state) => state.setApiKey);
|
||||||
|
const setCurrentChatIndex = useStore((state) => state.setCurrentChatIndex);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// legacy local storage
|
||||||
|
const oldChats = localStorage.getItem('chats');
|
||||||
|
const apiKey = localStorage.getItem('apiKey');
|
||||||
|
const theme = localStorage.getItem('theme');
|
||||||
|
|
||||||
|
if (apiKey) {
|
||||||
|
// legacy local storage
|
||||||
|
setApiKey(apiKey);
|
||||||
|
localStorage.removeItem('apiKey');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (theme) {
|
||||||
|
// legacy local storage
|
||||||
|
setTheme(theme as Theme);
|
||||||
|
localStorage.removeItem('theme');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldChats) {
|
||||||
|
// legacy local storage
|
||||||
|
try {
|
||||||
|
const chats: ChatInterface[] = JSON.parse(oldChats);
|
||||||
|
if (chats.length > 0) {
|
||||||
|
setChats(chats);
|
||||||
|
setCurrentChatIndex(0);
|
||||||
|
} else {
|
||||||
|
initialiseNewChat();
|
||||||
|
}
|
||||||
|
} catch (e: unknown) {
|
||||||
|
console.log(e);
|
||||||
|
initialiseNewChat();
|
||||||
|
}
|
||||||
|
localStorage.removeItem('chats');
|
||||||
|
} else {
|
||||||
|
// existing local storage
|
||||||
const chats = useStore.getState().chats;
|
const chats = useStore.getState().chats;
|
||||||
if (!chats || chats.length === 0) {
|
if (!chats || chats.length === 0) {
|
||||||
initialiseNewChat();
|
initialiseNewChat();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
Loading…
Reference in a new issue