2025-06-28 21:34:28 +00:00
|
|
|
import React, { useMemo } from 'react'
|
|
|
|
|
import { Routes, Route, Navigate } from 'react-router-dom'
|
|
|
|
|
import { mapDynamicRoutes } from './dynamicRouteLoader'
|
|
|
|
|
import { useDynamicRoutes } from './dynamicRoutesContext'
|
|
|
|
|
import NotFound from '@/views/NotFound'
|
|
|
|
|
import ProtectedRoute from '@/components/route/ProtectedRoute'
|
|
|
|
|
import PermissionGuard from '@/components/route/PermissionGuard'
|
|
|
|
|
import PageContainer from '@/components/template/PageContainer'
|
|
|
|
|
import { ROUTES_ENUM } from './route.constant'
|
|
|
|
|
|
|
|
|
|
export const DynamicRouter: React.FC = () => {
|
|
|
|
|
const { routes, loading, error } = useDynamicRoutes()
|
|
|
|
|
|
|
|
|
|
const dynamicRoutes = useMemo(() => mapDynamicRoutes(routes), [routes])
|
|
|
|
|
|
|
|
|
|
if (loading) return <div>Loading...</div>
|
|
|
|
|
if (error) return <div>Hata: {error}</div>
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Routes>
|
2025-08-11 06:34:44 +00:00
|
|
|
{/* Protected Admin Routes */}
|
|
|
|
|
<Route path="/admin/*" element={<ProtectedRoute />}>
|
2025-06-28 21:34:28 +00:00
|
|
|
{dynamicRoutes
|
|
|
|
|
.filter((r) => r.routeType === 'protected')
|
|
|
|
|
.map((route) => {
|
|
|
|
|
const Component = route.getComponent()
|
|
|
|
|
return (
|
|
|
|
|
<Route
|
|
|
|
|
key={route.key}
|
2025-08-11 06:34:44 +00:00
|
|
|
path={route.path.replace(/^\/admin\/?/, '') || '.'} // Remove /admin prefix for nested routes
|
2025-06-28 21:34:28 +00:00
|
|
|
element={
|
|
|
|
|
<PermissionGuard permissions={route.authority}>
|
|
|
|
|
<PageContainer>
|
|
|
|
|
<React.Suspense fallback={<div>Loading {route.path}...</div>}>
|
|
|
|
|
<Component />
|
|
|
|
|
</React.Suspense>
|
|
|
|
|
</PageContainer>
|
|
|
|
|
</PermissionGuard>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
})}
|
2025-08-11 06:34:44 +00:00
|
|
|
{/* /admin -> /admin/home yönlendirmesi */}
|
|
|
|
|
<Route index element={<Navigate to={ROUTES_ENUM.protected.dashboard} replace />} />
|
2025-06-28 21:34:28 +00:00
|
|
|
</Route>
|
|
|
|
|
|
2025-08-11 06:34:44 +00:00
|
|
|
{/* Public Routes (dinamik) */}
|
2025-06-28 21:34:28 +00:00
|
|
|
{dynamicRoutes
|
2025-08-11 06:34:44 +00:00
|
|
|
.filter((r) => r.routeType !== 'protected')
|
2025-06-28 21:34:28 +00:00
|
|
|
.map((route) => {
|
|
|
|
|
const Component = route.getComponent()
|
|
|
|
|
return (
|
|
|
|
|
<Route
|
|
|
|
|
key={route.key}
|
|
|
|
|
path={route.path}
|
|
|
|
|
element={
|
|
|
|
|
<React.Suspense fallback={<div>Loading {route.path}...</div>}>
|
|
|
|
|
<Component />
|
|
|
|
|
</React.Suspense>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
})}
|
|
|
|
|
|
2025-08-11 06:34:44 +00:00
|
|
|
{/* / yönlendirmesi → /home */}
|
|
|
|
|
<Route path="/" element={<Navigate to="/home" replace />} />
|
|
|
|
|
|
2025-06-28 21:34:28 +00:00
|
|
|
{/* Not Found */}
|
|
|
|
|
<Route path="*" element={<NotFound />} />
|
|
|
|
|
</Routes>
|
|
|
|
|
)
|
|
|
|
|
}
|