tianji/src/client/App.tsx

88 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-08-31 16:11:47 +00:00
import { BrowserRouter, Route, Routes, Navigate } from 'react-router-dom';
import { Layout } from './pages/Layout';
import { DashboardPage } from './pages/Dashboard';
2023-08-31 16:11:47 +00:00
import { Login } from './pages/Login';
import { SettingsPage } from './pages/Settings';
2023-08-31 16:11:47 +00:00
import { Servers } from './pages/Servers';
import { useUserStore } from './store/user';
import { Register } from './pages/Register';
2023-09-04 17:18:43 +00:00
import { QueryClientProvider } from '@tanstack/react-query';
import { queryClient } from './api/cache';
2023-09-05 07:32:16 +00:00
import { TokenLoginContainer } from './components/TokenLoginContainer';
import React, { Suspense } from 'react';
2023-09-27 09:56:32 +00:00
import { trpc, trpcClient } from './api/trpc';
2023-09-29 09:42:36 +00:00
import { MonitorPage } from './pages/Monitor';
2023-10-06 06:39:08 +00:00
import { WebsitePage } from './pages/Website';
import { useGlobalConfig } from './hooks/useConfig';
import { useInjectWebsiteScript } from './hooks/useInjectWebsiteScript';
2023-12-08 13:49:49 +00:00
import { ConfigProvider, theme } from 'antd';
import clsx from 'clsx';
import { useSettingsStore } from './store/settings';
2023-12-15 16:33:36 +00:00
import { StatusPage } from './pages/Status';
import { Loading } from './components/Loading';
2023-09-05 07:32:16 +00:00
export const AppRoutes: React.FC = React.memo(() => {
const { info } = useUserStore();
const { allowRegister } = useGlobalConfig();
useInjectWebsiteScript();
2023-09-05 07:32:16 +00:00
return (
<Routes>
{info ? (
<Route element={<Layout />}>
<Route path="/dashboard" element={<DashboardPage />} />
2023-09-29 09:42:36 +00:00
<Route path="/monitor/*" element={<MonitorPage />} />
2023-10-06 06:39:08 +00:00
<Route path="/website/*" element={<WebsitePage />} />
2023-09-05 07:32:16 +00:00
<Route path="/servers" element={<Servers />} />
<Route path="/settings/*" element={<SettingsPage />} />
2023-09-05 07:32:16 +00:00
</Route>
) : (
<Route>
<Route path="/login" element={<Login />} />
{allowRegister && <Route path="/register" element={<Register />} />}
2023-09-05 07:32:16 +00:00
</Route>
)}
2023-12-15 16:33:36 +00:00
<Route path="/status/:slug" element={<StatusPage />} />
2023-09-05 07:32:16 +00:00
<Route
path="*"
element={
<Navigate to={info ? '/dashboard' : '/login'} replace={true} />
}
/>
</Routes>
);
});
AppRoutes.displayName = 'AppRoutes';
export const App: React.FC = React.memo(() => {
const colorScheme = useSettingsStore((state) => state.colorScheme);
2023-12-08 13:49:49 +00:00
const algorithm =
colorScheme === 'dark' ? theme.darkAlgorithm : theme.defaultAlgorithm;
2023-08-31 13:17:57 +00:00
return (
2023-12-08 13:49:49 +00:00
<div
className={clsx('App', {
dark: colorScheme === 'dark',
})}
>
2023-09-27 09:56:32 +00:00
<trpc.Provider client={trpcClient} queryClient={queryClient}>
<QueryClientProvider client={queryClient}>
<BrowserRouter>
2023-12-08 13:49:49 +00:00
<ConfigProvider theme={{ algorithm }}>
<TokenLoginContainer>
<Suspense fallback={<Loading />}>
<AppRoutes />
</Suspense>
2023-12-08 13:49:49 +00:00
</TokenLoginContainer>
</ConfigProvider>
2023-09-27 09:56:32 +00:00
</BrowserRouter>
</QueryClientProvider>
</trpc.Provider>
2023-08-31 13:17:57 +00:00
</div>
);
2023-09-05 07:32:16 +00:00
});
App.displayName = 'App';