nova-betterchat/src/App.tsx

51 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-03-03 23:17:11 +01:00
import React, { useEffect } from 'react';
import useStore from '@store/store';
import Chat from './components/Chat';
import Menu from './components/Menu';
import ConfigMenu from './components/ConfigMenu';
import useSaveToLocalStorage from '@hooks/useSaveToLocalStorage';
2023-03-04 01:29:12 +01:00
import useInitialiseNewChat from '@hooks/useInitialiseNewChat';
2023-03-03 23:17:11 +01:00
2023-03-04 01:29:12 +01:00
import { ChatInterface } from '@type/chat';
2023-03-03 23:17:11 +01:00
2023-03-03 06:25:10 +01:00
function App() {
2023-03-03 23:17:11 +01:00
useSaveToLocalStorage();
2023-03-04 01:29:12 +01:00
const initialiseNewChat = useInitialiseNewChat();
2023-03-03 23:17:11 +01:00
const setChats = useStore((state) => state.setChats);
const setCurrentChatIndex = useStore((state) => state.setCurrentChatIndex);
2023-03-03 23:17:11 +01:00
useEffect(() => {
const storedChats = localStorage.getItem('chats');
if (storedChats) {
try {
const chats: ChatInterface[] = JSON.parse(storedChats);
2023-03-04 01:02:49 +01:00
if (chats.length > 0) {
setChats(chats);
setCurrentChatIndex(0);
} else {
initialiseNewChat();
}
2023-03-03 23:17:11 +01:00
} catch (e: unknown) {
setChats([]);
setCurrentChatIndex(-1);
console.log(e);
}
} else {
2023-03-04 01:02:49 +01:00
initialiseNewChat();
2023-03-03 23:17:11 +01:00
}
}, []);
return (
<div className='overflow-hidden w-full h-full relative'>
<Menu />
<Chat />
<ConfigMenu />
</div>
);
2023-03-03 06:25:10 +01:00
}
export default App;