import { lazy, Suspense } from "react"; import { Navigate, Route, Routes, useLocation } from "react-router-dom"; import { useAuthStore } from "./store/auth"; import { ErrorBoundary } from "./components/ErrorBoundary"; import { RequirePmProjects } from "./components/RequirePmProjects"; import { MobileTabBar } from "./components/layout/MobileTabBar"; // هر صفحه با React.lazy لود می‌شود (code splitting) const DashboardPage = lazy(() => import("./pages/Dashboard/DashboardPage").then((m) => ({ default: m.DashboardPage })), ); const WizardPage = lazy(() => import("./pages/Wizard/WizardPage").then((m) => ({ default: m.WizardPage }))); const SuppliersPage = lazy(() => import("./pages/Suppliers/SuppliersPage").then((m) => ({ default: m.SuppliersPage })), ); const CategoryPage = lazy(() => import("./pages/Suppliers/CategoryPage").then((m) => ({ default: m.CategoryPage })), ); const SupplierDetailPage = lazy(() => import("./pages/Suppliers/SupplierDetailPage").then((m) => ({ default: m.SupplierDetailPage })), ); const ProjectsPage = lazy(() => import("./pages/Projects/ProjectsPage").then((m) => ({ default: m.ProjectsPage })), ); const UnitsPage = lazy(() => import("./pages/Units/UnitsPage").then((m) => ({ default: m.UnitsPage }))); const LoginPage = lazy(() => import("./pages/Auth/LoginPage").then((m) => ({ default: m.LoginPage }))); const InventoryPage = lazy(() => import("./pages/Inventory/InventoryPage").then((m) => ({ default: m.InventoryPage })), ); const ArrivalsPage = lazy(() => import("./pages/Inventory/ArrivalsPage").then((m) => ({ default: m.ArrivalsPage }))); const ChangeInventoryPage = lazy(() => import("./pages/Inventory/ChangeInventoryPage").then((m) => ({ default: m.ChangeInventoryPage })), ); const DailyControlPage = lazy(() => import("./pages/Inventory/DailyControlPage").then((m) => ({ default: m.DailyControlPage })), ); const WarehousesPage = lazy(() => import("./pages/Inventory/WarehousesPage").then((m) => ({ default: m.WarehousesPage })), ); const OrganizationPage = lazy(() => import("./pages/Organization/OrganizationPage").then((m) => ({ default: m.OrganizationPage })), ); const AdminPanelPage = lazy(() => import("./pages/Admin/AdminPanel").then((m) => ({ default: m.AdminPanel })), ); function Loading() { return (
در حال بارگذاری...
); } // همه‌ی صفحات پشت ورود هستند — بدون نشست، به صفحه‌ی ورود می‌رویم. function RequireAuth({ children }: { children: React.ReactNode }) { const user = useAuthStore((s) => s.user); const { pathname } = useLocation(); if (!user) return ; if (user.role === "supervisor") { // اگر سرپرست پروژه‌ای نداشته باشد، دسترسی مسدود می‌شود if (!user.projects || user.projects.length === 0) { return (

پروژه تخصیص داده نشده است

شما به عنوان سرپرست پروژه در سیستم ثبت شده‌اید اما هنوز هیچ پروژه‌ای به شما تخصیص داده نشده است. تا زمان تخصیص پروژه توسط مدیر سیستم، اجازه ورود به سامانه را ندارید.

); } // سرپرست پروژه فقط به انبار و PM دسترسی دارد if (!pathname.startsWith("/inventory") && pathname !== "/panel/pm") { return ; } } return <>{children}; } // صفحه‌ی اصلی بر اساس نقش: سرپرست پروژه → پنل مدیریت پروژه (PM)؛ مدیران و ادمین → دفتر تدارکات function HomeRoute() { const user = useAuthStore((s) => s.user); if (user?.role === "supervisor") return ; return ; } // دسترسی ایجاد استعلام/پیشنهاد (ویزارد): مدیر تدارکات، مدیر پروژه و ادمین function RequireProcurementWrite({ children }: { children: React.ReactNode }) { const user = useAuthStore((s) => s.user); if (user?.role !== "admin" && user?.role !== "procurement_manager" && user?.role !== "pm") { return ; } return <>{children}; } // پنل «مدیریت پروژه (PM)» — اپ PM روی همان دامنه زیر مسیر /pm سرو می‌شود function PmPanelPage() { return (