2023-03-12 00:32:03 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2023-02-22 22:42:22 -05:00
|
|
|
import Messages from './components/Messages';
|
2023-02-22 21:30:48 -05:00
|
|
|
import Landing from './components/Main/Landing';
|
|
|
|
|
import TextChat from './components/Main/TextChat';
|
2023-02-06 13:27:28 -05:00
|
|
|
import Nav from './components/Nav';
|
2023-02-07 10:26:19 -05:00
|
|
|
import MobileNav from './components/Nav/MobileNav';
|
2023-02-13 18:02:29 -05:00
|
|
|
import useDocumentTitle from '~/hooks/useDocumentTitle';
|
2023-03-14 01:24:43 +08:00
|
|
|
import { useSelector, useDispatch } from 'react-redux';
|
|
|
|
|
import { setUser } from './store/userReducer';
|
2023-03-16 13:39:41 -04:00
|
|
|
import axios from 'axios';
|
2023-02-04 19:19:53 -05:00
|
|
|
|
|
|
|
|
const App = () => {
|
2023-03-14 01:24:43 +08:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
2023-03-15 10:42:45 -04:00
|
|
|
const { messages, messageTree } = useSelector((state) => state.messages);
|
2023-03-14 01:24:43 +08:00
|
|
|
const { user } = useSelector((state) => state.user);
|
2023-02-13 18:02:29 -05:00
|
|
|
const { title } = useSelector((state) => state.convo);
|
2023-03-12 00:32:03 +08:00
|
|
|
const [ navVisible, setNavVisible ]= useState(false)
|
2023-02-13 18:02:29 -05:00
|
|
|
useDocumentTitle(title);
|
2023-02-06 18:25:11 -05:00
|
|
|
|
2023-03-16 21:20:40 -04:00
|
|
|
useEffect(async () => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await axios.get('/api/me', {
|
|
|
|
|
timeout: 1000,
|
|
|
|
|
withCredentials: true
|
|
|
|
|
});
|
|
|
|
|
const user = response.data;
|
|
|
|
|
if (user) {
|
|
|
|
|
dispatch(setUser(user));
|
|
|
|
|
} else {
|
|
|
|
|
console.log('Not login!');
|
|
|
|
|
window.location.href = '/auth/login';
|
2023-03-14 01:24:43 +08:00
|
|
|
}
|
2023-03-16 21:20:40 -04:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
console.log('Not login!');
|
|
|
|
|
window.location.href = '/auth/login';
|
|
|
|
|
}
|
2023-03-14 01:24:43 +08:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
if (user)
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex h-screen">
|
|
|
|
|
<Nav navVisible={navVisible} setNavVisible={setNavVisible} />
|
|
|
|
|
<div className="flex h-full w-full flex-1 flex-col bg-gray-50 md:pl-[260px]">
|
|
|
|
|
<div className="transition-width relative flex h-full w-full flex-1 flex-col items-stretch overflow-hidden bg-white dark:bg-gray-800">
|
|
|
|
|
<MobileNav setNavVisible={setNavVisible} />
|
2023-03-19 01:14:19 -04:00
|
|
|
{messages.length === 0 && title.toLowerCase() === 'chatgpt clone' ? (
|
2023-03-14 01:24:43 +08:00
|
|
|
<Landing title={title} />
|
|
|
|
|
) : (
|
|
|
|
|
<Messages
|
|
|
|
|
messages={messages}
|
|
|
|
|
messageTree={messageTree}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<TextChat messages={messages} />
|
|
|
|
|
</div>
|
2023-02-08 09:15:47 -05:00
|
|
|
</div>
|
2023-02-04 19:19:53 -05:00
|
|
|
</div>
|
2023-03-14 01:24:43 +08:00
|
|
|
);
|
|
|
|
|
else
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex h-screen">
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
)
|
2023-02-04 19:19:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default App;
|