forked from linkong/planet
96 lines
4.4 KiB
TypeScript
96 lines
4.4 KiB
TypeScript
import { Suspense, lazy } from 'react'
|
|
|
|
import { Spin } from 'antd'
|
|
import { Routes, Route, Navigate, useLocation } from 'react-router-dom'
|
|
|
|
import { useAuthStore } from './stores/auth'
|
|
import Login from './pages/Login/Login'
|
|
|
|
const Register = lazy(() => import('./pages/Register/Register'))
|
|
const VerifyEmail = lazy(() => import('./pages/VerifyEmail/VerifyEmail'))
|
|
const ForgotPassword = lazy(() => import('./pages/ForgotPassword/ForgotPassword'))
|
|
const SystemAlerts = lazy(() => import('./pages/Alerts/SystemAlerts'))
|
|
const BGPAlerts = lazy(() => import('./pages/Alerts/BGPAlerts'))
|
|
const SituationalAlerts = lazy(() => import('./pages/Alerts/SituationalAlerts'))
|
|
const Dashboard = lazy(() => import('./pages/Dashboard/Dashboard'))
|
|
const Users = lazy(() => import('./pages/Users/Users'))
|
|
const DataSources = lazy(() => import('./pages/DataSources/DataSources'))
|
|
const DataList = lazy(() => import('./pages/DataList/DataList'))
|
|
const Earth = lazy(() => import('./pages/Earth/Earth'))
|
|
const Settings = lazy(() => import('./pages/Settings/Settings'))
|
|
const AISettings = lazy(() => import('./pages/AISettings/AISettings'))
|
|
const BGP = lazy(() => import('./pages/BGP/BGP'))
|
|
const Logs = lazy(() => import('./pages/Logs/Logs'))
|
|
const Docs = lazy(() => import('./pages/Docs/Docs'))
|
|
const AdminNextRoutes = lazy(() => import('./admin-next/AdminNextRoutes'))
|
|
|
|
const ROOT_ROUTE = '/'
|
|
const EARTH_ROUTE = '/earth'
|
|
const DOCS_ROUTE = '/docs'
|
|
const DOCS_ROUTE_PATTERN = '/docs/:slug'
|
|
const DOCS_ROUTE_PREFIX = `${DOCS_ROUTE}/`
|
|
const AUTH_ROUTES = new Set(['/login', '/register', '/verify-email', '/forgot-password'])
|
|
const PUBLIC_EXACT_ROUTES = new Set([ROOT_ROUTE, EARTH_ROUTE, DOCS_ROUTE, ...AUTH_ROUTES])
|
|
|
|
function isPublicPath(pathname: string) {
|
|
return PUBLIC_EXACT_ROUTES.has(pathname) || pathname.startsWith(DOCS_ROUTE_PREFIX)
|
|
}
|
|
|
|
function AdminNextCompatRedirect() {
|
|
const { pathname, search, hash } = useLocation()
|
|
const nextPath = pathname.replace(/^\/admin-next/, '') || ''
|
|
return <Navigate to={`${!nextPath || nextPath === '/' ? '/admin' : nextPath}${search}${hash}`} replace />
|
|
}
|
|
|
|
function App() {
|
|
const { token } = useAuthStore()
|
|
const { pathname } = useLocation()
|
|
const isPublicRoute = isPublicPath(pathname)
|
|
|
|
if (!token && !isPublicRoute) {
|
|
return <Login />
|
|
}
|
|
|
|
return (
|
|
<Suspense
|
|
fallback={(
|
|
<div className="app-route-loading">
|
|
<Spin size="large" />
|
|
</div>
|
|
)}
|
|
>
|
|
<Routes>
|
|
<Route path="/login" element={<Login />} />
|
|
<Route path="/register" element={<Register />} />
|
|
<Route path="/verify-email" element={<VerifyEmail />} />
|
|
<Route path="/forgot-password" element={<ForgotPassword />} />
|
|
<Route path={ROOT_ROUTE} element={<Navigate to={EARTH_ROUTE} replace />} />
|
|
<Route path={EARTH_ROUTE} element={<Earth />} />
|
|
<Route path={DOCS_ROUTE} element={<Docs />} />
|
|
<Route path={DOCS_ROUTE_PATTERN} element={<Docs />} />
|
|
<Route path="/legacy/admin" element={<Dashboard />} />
|
|
<Route path="/legacy/admin/users" element={<Users />} />
|
|
<Route path="/legacy/admin/datasources" element={<DataSources />} />
|
|
<Route path="/legacy/admin/earth-content" element={<Settings />} />
|
|
<Route path="/legacy/admin/collection-management" element={<Settings />} />
|
|
<Route path="/legacy/admin/data" element={<DataList />} />
|
|
<Route path="/legacy/admin/alerts" element={<Navigate to="/legacy/admin/alerts/system" replace />} />
|
|
<Route path="/legacy/admin/alerts/system" element={<SystemAlerts />} />
|
|
<Route path="/legacy/admin/alerts/bgp" element={<BGPAlerts />} />
|
|
<Route path="/legacy/admin/alerts/situational" element={<SituationalAlerts />} />
|
|
<Route path="/legacy/admin/bgp" element={<BGP />} />
|
|
<Route path="/legacy/admin/ai" element={<AISettings />} />
|
|
<Route path="/legacy/admin/playground" element={<Navigate to="/legacy/admin/ai?tab=playground" replace />} />
|
|
<Route path="/legacy/admin/logs" element={<Logs />} />
|
|
<Route path="/legacy/admin/settings" element={<Settings />} />
|
|
<Route path="/legacy/admin/*" element={<Navigate to="/legacy/admin" replace />} />
|
|
<Route path="/admin-next/*" element={<AdminNextCompatRedirect />} />
|
|
<Route path="/playground" element={<Navigate to="/ai?section=playground" replace />} />
|
|
<Route path="/*" element={<AdminNextRoutes />} />
|
|
</Routes>
|
|
</Suspense>
|
|
)
|
|
}
|
|
|
|
export default App
|