import { useState, useEffect } from "react"; import { NavLink, Outlet, useNavigate, useLocation } from "react-router-dom"; import { Separator } from "@/components/ui/separator"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { SidebarProvider, Sidebar, SidebarContent, SidebarHeader, SidebarFooter, SidebarMenu, SidebarMenuItem, SidebarMenuButton, SidebarTrigger, SidebarInset } from "@/components/ui/sidebar"; import { Breadcrumb, BreadcrumbList, BreadcrumbItem, BreadcrumbLink, BreadcrumbPage, BreadcrumbSeparator, } from "@/components/ui/breadcrumb"; import { LayoutDashboard, ClipboardList, Package, LogOut, Wrench, Store, Settings, PlusCircle, Users, ChevronDown, KeyRound, ShoppingCart } from "lucide-react"; import { toast } from "sonner"; import { api } from "@/services/api"; import { ShieldAlert } from "lucide-react"; export default function DashboardLayout() { const navigate = useNavigate(); const location = useLocation(); const user = JSON.parse(localStorage.getItem("repairit_user") || "{}"); const isSuperAdmin = user.role === "superadmin"; const [venues, setVenues] = useState([]); const [activeVenue, setActiveVenue] = useState(null); useEffect(() => { const token = localStorage.getItem("repairit_token"); if (!token) { if (window.location.hostname.includes("repairit.cloud")) { window.location.href = "https://repairit.cloud/login"; } else { navigate("/login"); } return; } if (!isSuperAdmin) { const loadVenues = async () => { try { const profile = await api.auth.getProfile(); setActiveVenue(profile); const data = await api.auth.getVenues(); setVenues(data); } catch (err) { console.error("Error al cargar sucursales en header:", err.message); } }; loadVenues(); } }, [isSuperAdmin]); const handleHeaderSwitchVenue = async (venueId) => { try { const updatedUser = await api.auth.switchVenue(venueId); const localUser = JSON.parse(localStorage.getItem("repairit_user") || "{}"); localStorage.setItem("repairit_user", JSON.stringify({ ...localUser, venueId: updatedUser.venueId })); toast.success("Sucursal cambiada correctamente"); setTimeout(() => { window.location.reload(); }, 500); } catch (err) { toast.error("Error al cambiar de sucursal", { description: err.message }); } }; const handleLogout = async () => { try { await api.auth.logout(); toast.success("Sesión cerrada correctamente."); } catch (err) { console.error("Error al cerrar sesión:", err); } finally { localStorage.clear(); if (window.location.hostname.includes("repairit.cloud")) { window.location.href = "https://repairit.cloud"; } else { window.location.href = "/"; } } }; // Determinar los Breadcrumbs basados en la ruta actual const getBreadcrumbs = () => { const path = location.pathname; if (isSuperAdmin) { if (path === "/dashboard/usuarios") { return ( Panel Gestión de Usuarios ); } return ( Suscripciones de Clientes ); } if (path === "/dashboard/nuevo-ingreso") { return ( Panel Ingreso de Equipos ); } if (path === "/dashboard/ordenes") { return ( Panel Órdenes de Servicio ); } if (path === "/dashboard/clientes") { return ( Panel Directorio de Clientes ); } if (path === "/dashboard/inventario") { return ( Panel Inventario de Insumos ); } if (path === "/dashboard/perfil") { return ( Panel Perfil de Taller ); } return ( Inicio ); }; return (
{/* Sidebar Oficial de shadcn/ui */} {/* Logo de la Empresa */}
RepairIT
{isSuperAdmin ? ( <> Suscripciones Usuarios ) : ( <> Inicio Nuevo Ingreso Órdenes de Servicio Clientes Inventario de Insumos Caja {user.role === "admin" && user.subscriptionPlan === "Multi-Taller Pro" && ( Cuentas de Acceso )} )}
{isSuperAdmin ? "SA" : activeVenue && activeVenue.name ? activeVenue.name.split(" ").map(n => n[0]).join("").substring(0, 2).toUpperCase() : "TL"}
{isSuperAdmin ? "Plataforma Central" : activeVenue ? activeVenue.name : "Cargando Taller..."} {isSuperAdmin ? "Super Admin" : `${user.name || "Usuario"} (${user.role === "admin" ? "Admin" : "Técnico"})`}
{!isSuperAdmin && ( )}
{/* Sidebar Inset para contener el área de trabajo y la barra de navegación superior */} {/* Cabecera Superior con Trigger y Breadcrumbs */}
{getBreadcrumbs()}
{isSuperAdmin ? (
Plataforma RepairIT • SuperAdmin
) : (
Sucursal: {venues.length > 1 ? ( {venues.map((v) => ( handleHeaderSwitchVenue(v._id)} className="text-[10px] transition-colors cursor-pointer flex items-center justify-between gap-4" > {v.name} {v._id === activeVenue?._id && ✓} ))} ) : ( {activeVenue ? activeVenue.name : "Cargando..."} )}
)}
{/* Contenido Dinámico de las Páginas del Dashboard */}
); }