72 lines
2.6 KiB
TypeScript
72 lines
2.6 KiB
TypeScript
import { Suspense, lazy, useEffect } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
import { Routes, Route, Navigate, useLocation } from 'react-router-dom'
|
|
|
|
import { useAuthStore } from './stores/auth'
|
|
import Login from './pages/Login/Login'
|
|
import { AdminErrorBoundary } from './admin/components/AdminErrorBoundary'
|
|
import LegacyI18nBridge from './i18n/LegacyI18nBridge'
|
|
|
|
const Register = lazy(() => import('./pages/Register/Register'))
|
|
const VerifyEmail = lazy(() => import('./pages/VerifyEmail/VerifyEmail'))
|
|
const ForgotPassword = lazy(() => import('./pages/ForgotPassword/ForgotPassword'))
|
|
const Earth = lazy(() => import('./pages/Earth/Earth'))
|
|
const Docs = lazy(() => import('./pages/Docs/Docs'))
|
|
const AdminRoutes = lazy(() => import('./admin/AdminRoutes'))
|
|
|
|
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 App() {
|
|
const { t } = useTranslation()
|
|
const { token } = useAuthStore()
|
|
const { pathname } = useLocation()
|
|
const isPublicRoute = isPublicPath(pathname)
|
|
|
|
useEffect(() => {
|
|
document.title = t('app.title')
|
|
}, [t])
|
|
|
|
if (!token && !isPublicRoute) {
|
|
return <Login />
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<LegacyI18nBridge />
|
|
<Suspense
|
|
fallback={(
|
|
<div className="app-route-loading">
|
|
<div className="app-route-loading__spinner" aria-label={t('app.routeLoading')} />
|
|
</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="/playground" element={<Navigate to="/ai?section=playground" replace />} />
|
|
<Route path="/*" element={<AdminErrorBoundary><AdminRoutes /></AdminErrorBoundary>} />
|
|
</Routes>
|
|
</Suspense>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default App
|