65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
|
|
"use client";
|
||
|
|
|
||
|
|
import { usePathname } from "next/navigation";
|
||
|
|
import { SidebarProvider, SidebarTrigger, SidebarInset } from "@/components/ui/sidebar";
|
||
|
|
import { AppSidebar } from "@/components/app-sidebar";
|
||
|
|
import { useAuth } from "@/hooks/use-auth";
|
||
|
|
import { Loader2 } from "lucide-react";
|
||
|
|
|
||
|
|
// Rutas públicas que no muestran el sidebar
|
||
|
|
const PUBLIC_ROUTES = ["/login"];
|
||
|
|
|
||
|
|
interface MainLayoutProps {
|
||
|
|
children: React.ReactNode;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function MainLayout({ children }: MainLayoutProps) {
|
||
|
|
const pathname = usePathname();
|
||
|
|
const { isLoading, isAuthenticated } = useAuth();
|
||
|
|
|
||
|
|
const isPublicRoute = PUBLIC_ROUTES.includes(pathname);
|
||
|
|
|
||
|
|
// Mostrar loading mientras se verifica la autenticación
|
||
|
|
if (isLoading) {
|
||
|
|
return (
|
||
|
|
<div className="min-h-screen flex items-center justify-center bg-background">
|
||
|
|
<div className="flex flex-col items-center gap-4">
|
||
|
|
<Loader2 className="h-8 w-8 animate-spin text-primary" />
|
||
|
|
<p className="text-sm text-muted-foreground">Cargando...</p>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Rutas públicas: sin sidebar
|
||
|
|
if (isPublicRoute) {
|
||
|
|
return <>{children}</>;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Si no está autenticado y no es ruta pública, el AuthProvider redirigirá
|
||
|
|
// Pero mostramos loading por si acaso
|
||
|
|
if (!isAuthenticated) {
|
||
|
|
return (
|
||
|
|
<div className="min-h-screen flex items-center justify-center bg-background">
|
||
|
|
<div className="flex flex-col items-center gap-4">
|
||
|
|
<Loader2 className="h-8 w-8 animate-spin text-primary" />
|
||
|
|
<p className="text-sm text-muted-foreground">Redirigiendo...</p>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Rutas privadas: con sidebar
|
||
|
|
return (
|
||
|
|
<SidebarProvider>
|
||
|
|
<AppSidebar />
|
||
|
|
<SidebarInset>
|
||
|
|
<main>
|
||
|
|
<SidebarTrigger />
|
||
|
|
{children}
|
||
|
|
</main>
|
||
|
|
</SidebarInset>
|
||
|
|
</SidebarProvider>
|
||
|
|
);
|
||
|
|
}
|