Spaces:
Running
Running
Z User
chore(biome): eliminate the organizeImports class repo-wide + close every remaining biome error
18afd52 Download src/components/AuthLayout.tsx from ahmdelbaz28/AHMDRTAP: direct link, hf CLI and curl.
- Browser
- Download file 10.4 kB
-
https://huggingface.co/spaces/ahmdelbaz28/AHMDRTAP/resolve/main/src/components/AuthLayout.tsx
- Command line
-
hf download hf://spaces/ahmdelbaz28/AHMDRTAP/src/components/AuthLayout.tsx
-
curl -L -o AuthLayout.tsx https://huggingface.co/spaces/ahmdelbaz28/AHMDRTAP/resolve/main/src/components/AuthLayout.tsx
10.4 kB
| 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 <AuthLayoutSkeleton />; | |
| } | |
| if (!user) { | |
| return ( | |
| <div className="flex items-center justify-center min-h-screen"> | |
| <div className="flex flex-col items-center gap-8 p-8 max-w-md w-full"> | |
| <div className="flex flex-col items-center gap-6"> | |
| <h1 className="text-2xl font-semibold tracking-tight text-center"> | |
| Sign in to continue | |
| </h1> | |
| <p className="text-sm text-muted-foreground text-center max-w-sm"> | |
| Access to this dashboard requires authentication. Continue to | |
| launch the login flow. | |
| </p> | |
| </div> | |
| <Button | |
| onClick={() => { | |
| globalThis.location.href = LOGIN_PATH; | |
| }} | |
| size="lg" | |
| className="w-full shadow-lg hover:shadow-xl transition-all" | |
| > | |
| Sign in | |
| </Button> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| return ( | |
| <SidebarProvider | |
| style={ | |
| { | |
| "--sidebar-width": `${sidebarWidth}px`, | |
| } as CSSProperties | |
| } | |
| > | |
| <AuthLayoutContent setSidebarWidth={setSidebarWidth}> | |
| {children} | |
| </AuthLayoutContent> | |
| </SidebarProvider> | |
| ); | |
| } | |
| 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<HTMLDivElement>(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 ( | |
| <> | |
| <div className="relative" ref={sidebarRef}> | |
| <Sidebar | |
| collapsible="icon" | |
| className="border-r-0" | |
| > | |
| <SidebarHeader className="h-16 justify-center"> | |
| <div className="flex items-center gap-3 px-2 transition-all w-full"> | |
| <button | |
| onClick={toggleSidebar} | |
| className="h-8 w-8 flex items-center justify-center hover:bg-accent rounded-lg transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-ring shrink-0" | |
| aria-label="Toggle navigation" | |
| > | |
| <PanelLeft className="h-4 w-4 text-muted-foreground" /> | |
| </button> | |
| {isCollapsed ? null : ( | |
| <div className="flex items-center gap-2 min-w-0"> | |
| <span className="font-semibold tracking-tight truncate"> | |
| Navigation | |
| </span> | |
| </div> | |
| )} | |
| </div> | |
| </SidebarHeader> | |
| <SidebarContent className="gap-0"> | |
| <SidebarMenu className="px-2 py-1"> | |
| {menuItems.map(item => { | |
| const isActive = location.pathname === item.path; | |
| return ( | |
| <SidebarMenuItem key={item.path}> | |
| <SidebarMenuButton | |
| isActive={isActive} | |
| onClick={() => navigate(item.path)} | |
| tooltip={item.label} | |
| className={`h-10 transition-all font-normal`} | |
| > | |
| <item.icon | |
| className={`h-4 w-4 ${isActive ? "text-primary" : ""}`} | |
| /> | |
| <span>{item.label}</span> | |
| </SidebarMenuButton> | |
| </SidebarMenuItem> | |
| ); | |
| })} | |
| </SidebarMenu> | |
| </SidebarContent> | |
| <SidebarFooter className="p-3"> | |
| <DropdownMenu> | |
| <DropdownMenuTrigger asChild> | |
| <button className="flex items-center gap-3 rounded-lg px-1 py-1 hover:bg-accent/50 transition-colors w-full text-left group-data-[collapsible=icon]:justify-center focus:outline-none focus-visible:ring-2 focus-visible:ring-ring"> | |
| <Avatar className="h-9 w-9 border shrink-0"> | |
| {/* ✅ AVATAR (owner request 2026-11-26): show the profile | |
| photo when set; the initials fallback stays for users | |
| who never uploaded one. */} | |
| <AvatarImage src={user?.avatar ?? undefined} alt={user?.name ?? ""} /> | |
| <AvatarFallback className="text-xs font-medium"> | |
| {user?.name?.charAt(0).toUpperCase()} | |
| </AvatarFallback> | |
| </Avatar> | |
| <div className="flex-1 min-w-0 group-data-[collapsible=icon]:hidden"> | |
| <p className="text-sm font-medium truncate leading-none"> | |
| {user?.name || "-"} | |
| </p> | |
| <p className="text-xs text-muted-foreground truncate mt-1.5"> | |
| {user?.email || "-"} | |
| </p> | |
| </div> | |
| </button> | |
| </DropdownMenuTrigger> | |
| <DropdownMenuContent align="end" className="w-48"> | |
| <DropdownMenuItem | |
| onClick={logout} | |
| className="cursor-pointer text-destructive focus:text-destructive" | |
| > | |
| <LogOut className="mr-2 h-4 w-4" /> | |
| <span>Sign out</span> | |
| </DropdownMenuItem> | |
| </DropdownMenuContent> | |
| </DropdownMenu> | |
| </SidebarFooter> | |
| </Sidebar> | |
| {/* biome-ignore lint/a11y/useSemanticElements: intentional — custom resize handle with mouse + keyboard handlers; <hr> cannot host these handlers */} | |
| <div // NOSONAR — S6819/S6845/S6847: interactive resize handle; using <div> with ARIA + keyboard handler instead of <button> to preserve pixel-perfect resize cursor and prevent default button styling | |
| className={`absolute top-0 right-0 w-1 h-full cursor-col-resize hover:bg-primary/20 transition-colors ${isCollapsed ? "hidden" : ""}`} | |
| role="separator" | |
| aria-orientation="vertical" | |
| aria-label="Resize sidebar" | |
| aria-valuenow={50} | |
| aria-valuemin={0} | |
| aria-valuemax={100} | |
| tabIndex={0} // NOSONAR — tabIndex needed for keyboard accessibility on interactive resize widget | |
| onMouseDown={() => { | |
| if (isCollapsed) return; | |
| setIsResizing(true); | |
| }} | |
| onKeyDown={(e) => { | |
| // Allow keyboard users to start resize with Enter/Space | |
| if ((e.key === "Enter" || e.key === " ") && !isCollapsed) { | |
| e.preventDefault(); | |
| setIsResizing(true); | |
| } | |
| }} | |
| style={{ zIndex: 50 }} | |
| /> | |
| </div> | |
| <SidebarInset> | |
| {isMobile && ( | |
| <div className="flex border-b h-14 items-center justify-between bg-background/95 px-2 backdrop-blur supports-backdrop-filter:backdrop-blur sticky top-0 z-40"> | |
| <div className="flex items-center gap-2"> | |
| <SidebarTrigger className="h-9 w-9 rounded-lg bg-background" /> | |
| <div className="flex items-center gap-3"> | |
| <div className="flex flex-col gap-1"> | |
| <span className="tracking-tight text-foreground"> | |
| {activeMenuItem?.label ?? "Menu"} | |
| </span> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| )} | |
| <main className="flex-1 p-4">{children}</main> | |
| </SidebarInset> | |
| </> | |
| ); | |
| } | |