2023-03-05 16:56:26 +01:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
2023-03-03 23:17:11 +01:00
|
|
|
import useStore from '@store/store';
|
|
|
|
|
|
|
|
import Chat from './components/Chat';
|
|
|
|
import Menu from './components/Menu';
|
|
|
|
|
2023-03-04 01:29:12 +01:00
|
|
|
import useInitialiseNewChat from '@hooks/useInitialiseNewChat';
|
2023-03-05 17:35:18 +01:00
|
|
|
import { ChatInterface } from '@type/chat';
|
|
|
|
import { Theme } from '@type/theme';
|
2023-03-03 23:17:11 +01:00
|
|
|
|
2023-03-03 06:25:10 +01:00
|
|
|
function App() {
|
2023-03-04 01:29:12 +01:00
|
|
|
const initialiseNewChat = useInitialiseNewChat();
|
2023-03-05 17:35:18 +01:00
|
|
|
const setChats = useStore((state) => state.setChats);
|
|
|
|
const setTheme = useStore((state) => state.setTheme);
|
|
|
|
const setApiKey = useStore((state) => state.setApiKey);
|
|
|
|
const setCurrentChatIndex = useStore((state) => state.setCurrentChatIndex);
|
2023-03-03 23:17:11 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-03-05 17:35:18 +01:00
|
|
|
// 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;
|
2023-03-05 17:50:18 +01:00
|
|
|
const currentChatIndex = useStore.getState().currentChatIndex;
|
2023-03-05 17:35:18 +01:00
|
|
|
if (!chats || chats.length === 0) {
|
|
|
|
initialiseNewChat();
|
|
|
|
}
|
2023-03-05 17:50:18 +01:00
|
|
|
if (
|
|
|
|
chats &&
|
|
|
|
!(currentChatIndex >= 0 && currentChatIndex < chats.length)
|
|
|
|
) {
|
|
|
|
setCurrentChatIndex(0);
|
|
|
|
}
|
2023-03-03 23:17:11 +01:00
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='overflow-hidden w-full h-full relative'>
|
|
|
|
<Menu />
|
|
|
|
<Chat />
|
|
|
|
</div>
|
|
|
|
);
|
2023-03-03 06:25:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|