2023-03-12 00:32:03 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2023-03-28 20:36:21 +08:00
|
|
|
import { createBrowserRouter, RouterProvider, Navigate } from 'react-router-dom';
|
|
|
|
|
import Root from './routes/Root';
|
2023-03-28 22:39:27 +08:00
|
|
|
import Chat from './routes/Chat';
|
2023-03-29 00:08:02 +08:00
|
|
|
import Search from './routes/Search';
|
2023-03-28 20:36:21 +08:00
|
|
|
import store from './store';
|
2023-03-21 19:31:57 -04:00
|
|
|
import userAuth from './utils/userAuth';
|
2023-03-28 20:36:21 +08:00
|
|
|
import { useRecoilState, useSetRecoilState } from 'recoil';
|
|
|
|
|
|
2023-03-16 13:39:41 -04:00
|
|
|
import axios from 'axios';
|
2023-02-04 19:19:53 -05:00
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
const router = createBrowserRouter([
|
|
|
|
|
{
|
|
|
|
|
path: '/',
|
|
|
|
|
element: <Root />,
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
index: true,
|
|
|
|
|
element: (
|
|
|
|
|
<Navigate
|
|
|
|
|
to="/chat/new"
|
|
|
|
|
replace={true}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-03-29 00:08:02 +08:00
|
|
|
path: 'chat/:conversationId?',
|
2023-03-28 22:39:27 +08:00
|
|
|
element: <Chat />
|
2023-03-29 00:08:02 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'search/:query?',
|
|
|
|
|
element: <Search />
|
2023-03-28 20:36:21 +08:00
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
]);
|
2023-03-21 19:31:57 -04:00
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
const App = () => {
|
|
|
|
|
const [user, setUser] = useRecoilState(store.user);
|
|
|
|
|
const setIsSearchEnabled = useSetRecoilState(store.isSearchEnabled);
|
2023-04-01 14:33:07 +08:00
|
|
|
const setEndpointsConfig = useSetRecoilState(store.endpointsConfig);
|
2023-04-02 04:15:07 +08:00
|
|
|
const setPresets = useSetRecoilState(store.presets);
|
2023-02-06 18:25:11 -05:00
|
|
|
|
2023-03-21 19:31:57 -04:00
|
|
|
useEffect(() => {
|
2023-03-28 22:39:27 +08:00
|
|
|
// fetch if seatch enabled
|
|
|
|
|
axios
|
|
|
|
|
.get('/api/search/enable', {
|
|
|
|
|
timeout: 1000,
|
|
|
|
|
withCredentials: true
|
|
|
|
|
})
|
|
|
|
|
.then(res => {
|
|
|
|
|
setIsSearchEnabled(res.data);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// fetch user
|
2023-03-21 19:31:57 -04:00
|
|
|
userAuth()
|
2023-03-28 20:36:21 +08:00
|
|
|
.then(user => setUser(user))
|
|
|
|
|
.catch(err => console.log(err));
|
2023-03-28 22:39:27 +08:00
|
|
|
|
|
|
|
|
// fetch models
|
|
|
|
|
axios
|
2023-03-31 03:22:57 +08:00
|
|
|
.get('/api/endpoints', {
|
2023-03-28 22:39:27 +08:00
|
|
|
timeout: 1000,
|
|
|
|
|
withCredentials: true
|
|
|
|
|
})
|
|
|
|
|
.then(({ data }) => {
|
2023-04-01 14:33:07 +08:00
|
|
|
setEndpointsConfig(data);
|
2023-03-28 22:39:27 +08:00
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
console.error(error);
|
|
|
|
|
console.log('Not login!');
|
|
|
|
|
window.location.href = '/auth/login';
|
|
|
|
|
});
|
2023-04-02 04:15:07 +08:00
|
|
|
|
|
|
|
|
// fetch presets
|
|
|
|
|
axios
|
|
|
|
|
.get('/api/presets', {
|
|
|
|
|
timeout: 1000,
|
|
|
|
|
withCredentials: true
|
|
|
|
|
})
|
|
|
|
|
.then(({ data }) => {
|
|
|
|
|
setPresets(data);
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
console.error(error);
|
|
|
|
|
console.log('Not login!');
|
|
|
|
|
window.location.href = '/auth/login';
|
|
|
|
|
});
|
2023-03-21 19:31:57 -04:00
|
|
|
}, []);
|
2023-03-14 01:24:43 +08:00
|
|
|
|
|
|
|
|
if (user)
|
|
|
|
|
return (
|
2023-03-28 20:36:21 +08:00
|
|
|
<div>
|
|
|
|
|
<RouterProvider router={router} />
|
2023-02-04 19:19:53 -05:00
|
|
|
</div>
|
2023-03-14 01:24:43 +08:00
|
|
|
);
|
2023-03-21 19:31:57 -04:00
|
|
|
else return <div className="flex h-screen"></div>;
|
2023-02-04 19:19:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default App;
|