2026-02-24 20:44:16 +00:00
|
|
|
|
// DynamicRouter.tsx
|
|
|
|
|
|
import React from 'react'
|
2026-04-23 10:36:51 +00:00
|
|
|
|
import { Routes, Route, Navigate, useLocation } from 'react-router-dom'
|
2026-02-24 20:44:16 +00:00
|
|
|
|
import { mapDynamicRoutes } from './dynamicRouteLoader'
|
|
|
|
|
|
import { useDynamicRoutes } from './dynamicRoutesContext'
|
|
|
|
|
|
import { useComponents } from '@/contexts/ComponentContext'
|
|
|
|
|
|
import ProtectedRoute from '@/components/route/ProtectedRoute'
|
|
|
|
|
|
import PermissionGuard from '@/components/route/PermissionGuard'
|
|
|
|
|
|
import PageContainer from '@/components/template/PageContainer'
|
|
|
|
|
|
import { ROUTES_ENUM } from './route.constant'
|
|
|
|
|
|
import { hasSubdomain } from '@/utils/subdomain'
|
|
|
|
|
|
|
|
|
|
|
|
// AccessDenied ve NotFound'u dinamiklikten çıkarıyoruz
|
|
|
|
|
|
const AccessDenied = React.lazy(() => import('@/views/AccessDenied'))
|
|
|
|
|
|
const NotFound = React.lazy(() => import('@/views/NotFound'))
|
2026-04-23 10:36:51 +00:00
|
|
|
|
const DatabaseSetup = React.lazy(() => import('@/views/setup/DatabaseSetup'))
|
2026-02-24 20:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
export const DynamicRouter: React.FC = () => {
|
|
|
|
|
|
const { routes, loading, error } = useDynamicRoutes()
|
|
|
|
|
|
const { registeredComponents, renderComponent, isComponentRegistered } = useComponents()
|
2026-04-23 10:36:51 +00:00
|
|
|
|
const location = useLocation()
|
2026-02-24 20:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
const dynamicRoutes = React.useMemo(() => mapDynamicRoutes(routes), [routes])
|
|
|
|
|
|
|
2026-04-23 10:36:51 +00:00
|
|
|
|
// /setup path'inde loading bekleme — setup route her zaman erişilebilir olmalı
|
|
|
|
|
|
if (loading && location.pathname !== '/setup') return <div>Loading...</div>
|
|
|
|
|
|
if (error && location.pathname !== '/setup') return <div>Hata: {error}</div>
|
2026-02-24 20:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Routes>
|
|
|
|
|
|
{/* ADMIN */}
|
|
|
|
|
|
<Route path="/admin/*" element={<ProtectedRoute />}>
|
|
|
|
|
|
{dynamicRoutes
|
|
|
|
|
|
.filter((r) => r.routeType === 'protected')
|
|
|
|
|
|
.map((route) => {
|
|
|
|
|
|
const Component = route.getComponent(registeredComponents, renderComponent, isComponentRegistered)
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Route
|
|
|
|
|
|
key={route.key}
|
|
|
|
|
|
path={route.path.replace(/^\/admin\/?/, '') || '.'}
|
|
|
|
|
|
element={
|
|
|
|
|
|
<PermissionGuard permissions={route.authority}>
|
|
|
|
|
|
<PageContainer>
|
|
|
|
|
|
<React.Suspense fallback={<div>Loading {route.path}...</div>}>
|
|
|
|
|
|
<Component />
|
|
|
|
|
|
</React.Suspense>
|
|
|
|
|
|
</PageContainer>
|
|
|
|
|
|
</PermissionGuard>
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)
|
|
|
|
|
|
})}
|
|
|
|
|
|
|
|
|
|
|
|
{/* admin default */}
|
|
|
|
|
|
<Route index element={<Navigate to={ROUTES_ENUM.protected.dashboard} replace />} />
|
|
|
|
|
|
|
|
|
|
|
|
{/* admin access denied (statik) */}
|
|
|
|
|
|
<Route
|
|
|
|
|
|
path="access-denied"
|
|
|
|
|
|
element={
|
|
|
|
|
|
<PageContainer>
|
|
|
|
|
|
<React.Suspense fallback={<div>Loading...</div>}>
|
|
|
|
|
|
<AccessDenied />
|
|
|
|
|
|
</React.Suspense>
|
|
|
|
|
|
</PageContainer>
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
{/* admin not found (statik) */}
|
|
|
|
|
|
<Route
|
|
|
|
|
|
path="*"
|
|
|
|
|
|
element={
|
|
|
|
|
|
<PageContainer>
|
|
|
|
|
|
<React.Suspense fallback={<div>Loading...</div>}>
|
|
|
|
|
|
<NotFound />
|
|
|
|
|
|
</React.Suspense>
|
|
|
|
|
|
</PageContainer>
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</Route>
|
|
|
|
|
|
|
|
|
|
|
|
{/* Auth/Public dinamik rotalar */}
|
|
|
|
|
|
{dynamicRoutes
|
|
|
|
|
|
.filter((r) =>
|
|
|
|
|
|
hasSubdomain() ? r.routeType === 'authenticated' : r.routeType !== 'protected',
|
|
|
|
|
|
)
|
|
|
|
|
|
.map((route) => {
|
|
|
|
|
|
const Component = route.getComponent(registeredComponents, renderComponent, isComponentRegistered)
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Route
|
|
|
|
|
|
key={route.key}
|
|
|
|
|
|
path={route.path}
|
|
|
|
|
|
element={
|
|
|
|
|
|
<React.Suspense fallback={<div>Loading {route.path}...</div>}>
|
|
|
|
|
|
<Component />
|
|
|
|
|
|
</React.Suspense>
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)
|
|
|
|
|
|
})}
|
|
|
|
|
|
|
|
|
|
|
|
{/* root redirect */}
|
|
|
|
|
|
<Route
|
|
|
|
|
|
path="/"
|
|
|
|
|
|
element={
|
|
|
|
|
|
<Navigate
|
|
|
|
|
|
to={hasSubdomain() ? ROUTES_ENUM.authenticated.login : ROUTES_ENUM.public.home}
|
|
|
|
|
|
replace
|
|
|
|
|
|
/>
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
{/* public access denied (statik) */}
|
|
|
|
|
|
<Route
|
|
|
|
|
|
path="/access-denied"
|
|
|
|
|
|
element={
|
|
|
|
|
|
<React.Suspense fallback={<div>Loading...</div>}>
|
|
|
|
|
|
<AccessDenied />
|
|
|
|
|
|
</React.Suspense>
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
{/* public not found (statik) */}
|
|
|
|
|
|
<Route
|
|
|
|
|
|
path="*"
|
|
|
|
|
|
element={
|
|
|
|
|
|
<React.Suspense fallback={<div>Loading...</div>}>
|
|
|
|
|
|
<NotFound />
|
|
|
|
|
|
</React.Suspense>
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
2026-04-23 10:36:51 +00:00
|
|
|
|
|
|
|
|
|
|
{/* İlk kurulum — veritabanı mevcut değilse gösterilir */}
|
|
|
|
|
|
<Route
|
|
|
|
|
|
path={ROUTES_ENUM.setup}
|
|
|
|
|
|
element={
|
|
|
|
|
|
<React.Suspense fallback={<div>Loading...</div>}>
|
|
|
|
|
|
<DatabaseSetup />
|
|
|
|
|
|
</React.Suspense>
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
2026-02-24 20:44:16 +00:00
|
|
|
|
</Routes>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|