2023-08-31 16:11:47 +00:00
|
|
|
import { BrowserRouter, Route, Routes, Navigate } from 'react-router-dom';
|
|
|
|
import { Layout } from './pages/Layout';
|
2023-11-12 15:49:02 +00:00
|
|
|
import { DashboardPage } from './pages/Dashboard';
|
2023-08-31 16:11:47 +00:00
|
|
|
import { Login } from './pages/Login';
|
2023-09-20 15:11:03 +00:00
|
|
|
import { SettingsPage } from './pages/Settings';
|
2023-08-31 16:11:47 +00:00
|
|
|
import { Servers } from './pages/Servers';
|
2023-09-03 13:05:22 +00:00
|
|
|
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';
|
2024-02-17 16:47:22 +00:00
|
|
|
import React, { useRef } 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';
|
2023-10-20 16:18:30 +00:00
|
|
|
import { useGlobalConfig } from './hooks/useConfig';
|
|
|
|
import { useInjectWebsiteScript } from './hooks/useInjectWebsiteScript';
|
2023-12-08 13:49:49 +00:00
|
|
|
import { ConfigProvider, theme } from 'antd';
|
2024-03-17 16:57:02 +00:00
|
|
|
import { useColorSchema } from './store/settings';
|
2023-12-15 16:33:36 +00:00
|
|
|
import { StatusPage } from './pages/Status';
|
2024-02-17 16:47:22 +00:00
|
|
|
import { TelemetryPage } from './pages/Telemetry';
|
2024-03-17 16:57:02 +00:00
|
|
|
import { isDev } from './utils/env';
|
2024-03-20 18:00:23 +00:00
|
|
|
import { RouterProvider, createRouter } from '@tanstack/react-router';
|
|
|
|
import { routeTree } from './routeTree.gen';
|
2024-03-25 12:10:01 +00:00
|
|
|
import { DefaultNotFound } from './components/DefaultNotFound';
|
2024-03-20 18:00:23 +00:00
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
routeTree,
|
|
|
|
context: {
|
|
|
|
userInfo: undefined,
|
|
|
|
},
|
2024-03-25 12:10:01 +00:00
|
|
|
defaultNotFoundComponent: DefaultNotFound,
|
2024-03-20 18:00:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Register the router instance for type safety
|
|
|
|
declare module '@tanstack/react-router' {
|
|
|
|
interface Register {
|
|
|
|
router: typeof router;
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 07:32:16 +00:00
|
|
|
|
|
|
|
export const AppRoutes: React.FC = React.memo(() => {
|
2024-02-22 16:07:30 +00:00
|
|
|
const { info: userInfo } = useUserStore();
|
2023-10-20 16:18:30 +00:00
|
|
|
const { allowRegister } = useGlobalConfig();
|
|
|
|
|
|
|
|
useInjectWebsiteScript();
|
2023-09-03 13:05:22 +00:00
|
|
|
|
2023-09-05 07:32:16 +00:00
|
|
|
return (
|
|
|
|
<Routes>
|
2024-02-22 16:07:30 +00:00
|
|
|
{userInfo ? (
|
2024-03-25 12:10:01 +00:00
|
|
|
<Route element={<Layout />}>
|
2023-11-12 15:49:02 +00:00
|
|
|
<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 />} />
|
2024-02-17 16:47:22 +00:00
|
|
|
<Route path="/telemetry/*" element={<TelemetryPage />} />
|
2023-09-20 15:11:03 +00:00
|
|
|
<Route path="/settings/*" element={<SettingsPage />} />
|
2023-09-05 07:32:16 +00:00
|
|
|
</Route>
|
|
|
|
) : (
|
|
|
|
<Route>
|
|
|
|
<Route path="/login" element={<Login />} />
|
2023-10-20 16:18:30 +00:00
|
|
|
{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={
|
2024-02-22 16:07:30 +00:00
|
|
|
<Navigate to={userInfo ? '/dashboard' : '/login'} replace={true} />
|
2023-09-05 07:32:16 +00:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Routes>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
AppRoutes.displayName = 'AppRoutes';
|
|
|
|
|
|
|
|
export const App: React.FC = React.memo(() => {
|
2024-01-07 16:01:49 +00:00
|
|
|
const rootRef = useRef<HTMLDivElement | null>(null);
|
2024-03-17 16:57:02 +00:00
|
|
|
const colorScheme = useColorSchema();
|
2023-12-08 13:49:49 +00:00
|
|
|
const algorithm =
|
|
|
|
colorScheme === 'dark' ? theme.darkAlgorithm : theme.defaultAlgorithm;
|
2024-03-20 18:00:23 +00:00
|
|
|
const { info: userInfo } = useUserStore();
|
2023-12-08 13:49:49 +00:00
|
|
|
|
2023-08-31 13:17:57 +00:00
|
|
|
return (
|
2024-03-17 16:57:02 +00:00
|
|
|
<div ref={rootRef} className="App">
|
2023-09-27 09:56:32 +00:00
|
|
|
<trpc.Provider client={trpcClient} queryClient={queryClient}>
|
|
|
|
<QueryClientProvider client={queryClient}>
|
2024-03-20 18:00:23 +00:00
|
|
|
<ConfigProvider
|
|
|
|
theme={{ algorithm }}
|
|
|
|
getPopupContainer={() => rootRef.current!}
|
|
|
|
>
|
|
|
|
<TokenLoginContainer>
|
|
|
|
{isDev ? (
|
2024-03-23 11:38:39 +00:00
|
|
|
// Compatible with old routes
|
|
|
|
<BrowserRouter>
|
|
|
|
<RouterProvider router={router} context={{ userInfo }} />
|
|
|
|
</BrowserRouter>
|
2024-03-20 18:00:23 +00:00
|
|
|
) : (
|
|
|
|
<BrowserRouter>
|
|
|
|
<AppRoutes />
|
|
|
|
</BrowserRouter>
|
|
|
|
)}
|
|
|
|
</TokenLoginContainer>
|
|
|
|
</ConfigProvider>
|
2023-09-27 09:56:32 +00:00
|
|
|
</QueryClientProvider>
|
|
|
|
</trpc.Provider>
|
2023-08-31 13:17:57 +00:00
|
|
|
</div>
|
|
|
|
);
|
2023-09-05 07:32:16 +00:00
|
|
|
});
|
|
|
|
App.displayName = 'App';
|