import { LayoutDashboard, LogOut, PanelLeft, Users } from "lucide-react";
import { type CSSProperties, type ReactNode, useEffect, useRef, useState } from "react";
import { useLocation, useNavigate } from "react-router";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
Sidebar,
SidebarContent,
SidebarFooter,
SidebarHeader,
SidebarInset,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarProvider,
SidebarTrigger,
useSidebar,
} from "@/components/ui/sidebar";
import { LOGIN_PATH } from "@/const";
import { useIsMobile } from "@/hooks/use-mobile";
import { useAuth } from "@/hooks/useAuth";
import { AuthLayoutSkeleton } from "./AuthLayoutSkeleton";
import { Button } from "./ui/button";
const menuItems = [
{ icon: LayoutDashboard, label: "Page 1", path: "/" },
{ icon: Users, label: "Page 2", path: "/some-path" },
];
const SIDEBAR_WIDTH_KEY = "sidebar-width";
const DEFAULT_WIDTH = 280;
const MIN_WIDTH = 200;
const MAX_WIDTH = 480;
export default function AuthLayout({
children,
}: {
readonly children: ReactNode;
}) {
const [sidebarWidth, setSidebarWidth] = useState(() => {
const saved = localStorage.getItem(SIDEBAR_WIDTH_KEY);
return saved ? Number.parseInt(saved, 10) : DEFAULT_WIDTH;
});
const { isLoading, user } = useAuth();
useEffect(() => {
localStorage.setItem(SIDEBAR_WIDTH_KEY, sidebarWidth.toString());
}, [sidebarWidth]);
if (isLoading) {
return ;
}
if (!user) {
return (
Sign in to continue
Access to this dashboard requires authentication. Continue to
launch the login flow.
);
}
return (
{children}
);
}
type AuthLayoutContentProps = {
readonly children: ReactNode;
readonly setSidebarWidth: (width: number) => void;
};
function AuthLayoutContent({
children,
setSidebarWidth,
}: AuthLayoutContentProps) {
const { user, logout } = useAuth();
const location = useLocation();
const navigate = useNavigate();
const { state, toggleSidebar } = useSidebar();
const isCollapsed = state === "collapsed";
const [isResizing, setIsResizing] = useState(false);
const sidebarRef = useRef(null);
const activeMenuItem = menuItems.find(item => item.path === location.pathname);
const isMobile = useIsMobile();
useEffect(() => {
if (isCollapsed) {
setIsResizing(false); // eslint-disable-line react-hooks/set-state-in-effect
}
}, [isCollapsed]);
useEffect(() => {
const handleMouseMove = (e: MouseEvent) => {
if (!isResizing) return;
const sidebarLeft = sidebarRef.current?.getBoundingClientRect().left ?? 0;
const newWidth = e.clientX - sidebarLeft;
if (newWidth >= MIN_WIDTH && newWidth <= MAX_WIDTH) {
setSidebarWidth(newWidth);
}
};
const handleMouseUp = () => {
setIsResizing(false);
};
if (isResizing) {
document.addEventListener("mousemove", handleMouseMove);
document.addEventListener("mouseup", handleMouseUp);
document.body.style.cursor = "col-resize";
document.body.style.userSelect = "none";
}
return () => {
document.removeEventListener("mousemove", handleMouseMove);
document.removeEventListener("mouseup", handleMouseUp);
document.body.style.cursor = "";
document.body.style.userSelect = "";
};
}, [isResizing, setSidebarWidth]);
return (
<>
{isCollapsed ? null : (
Navigation
)}
{menuItems.map(item => {
const isActive = location.pathname === item.path;
return (
navigate(item.path)}
tooltip={item.label}
className={`h-10 transition-all font-normal`}
>
{item.label}
);
})}
Sign out
{/* biome-ignore lint/a11y/useSemanticElements: intentional — custom resize handle with mouse + keyboard handlers;
cannot host these handlers */}
with ARIA + keyboard handler instead of
{isMobile && (
{activeMenuItem?.label ?? "Menu"}
)}
{children}
>
);
}