import { Suspense, useEffect, useRef, useState } from "react"; import { ActionIcon, AppShell, Avatar, Box, Burger, Button, Center, Container, Divider, Group, Loader, NavLink, ScrollArea, Stack, Text, Tooltip, UnstyledButton, useMantineColorScheme, } from "@mantine/core"; import { useDisclosure, useMediaQuery } from "@mantine/hooks"; import { IconCalendarEvent, IconLayoutDashboard, IconLayoutSidebarLeftCollapse, IconLayoutSidebarLeftExpand, IconLogout, IconMessage2, IconPhoneCall, IconSettings, IconShieldLock, } from "@tabler/icons-react"; import { Link, Outlet, useLocation } from "react-router-dom"; import { useAuth } from "../auth/AuthProvider"; import { useI18n } from "../i18n"; import LanguageToggle from "./LanguageToggle"; import ThemeToggle from "./ThemeToggle"; type IconCmp = typeof IconPhoneCall; interface NavItem { to: string; labelKey: string; icon: IconCmp; } const NAV: NavItem[] = [ { to: "/", labelKey: "nav.train", icon: IconPhoneCall }, { to: "/dashboard", labelKey: "nav.dashboard", icon: IconLayoutDashboard }, { to: "/schedule", labelKey: "nav.schedule", icon: IconCalendarEvent }, { to: "/settings", labelKey: "nav.settings", icon: IconSettings }, { to: "/feedback", labelKey: "nav.feedback", icon: IconMessage2 }, ]; const ADMIN_NAV: NavItem[] = [ { to: "/admin", labelKey: "nav.admin", icon: IconShieldLock }, ]; const FULL_WIDTH = 260; const RAIL_WIDTH = 72; export default function Shell() { const [mobileOpened, { toggle: toggleMobile, close: closeMobile }] = useDisclosure(false); const [collapsed, setCollapsed] = useState(false); // desktop rail mode const isDesktop = useMediaQuery("(min-width: 48em)", true); const rail = isDesktop && collapsed; // icon-only rail (desktop, collapsed) const { user, profile, signOut } = useAuth(); const { t } = useI18n(); const location = useLocation(); const { setColorScheme } = useMantineColorScheme(); // Seed the saved theme ONCE when the profile first loads. After that the // toggle/settings own the live scheme — re-applying here on every profile // change is what caused the flicker/lag when switching modes. const themeSeeded = useRef(false); useEffect(() => { if (!themeSeeded.current && profile?.theme) { setColorScheme(profile.theme === "system" ? "auto" : profile.theme); themeSeeded.current = true; } }, [profile?.theme, setColorScheme]); const isActive = (to: string) => to === "/" ? location.pathname === "/" : location.pathname.startsWith(to); const initials = (profile?.full_name || user?.email || "?") .split(" ") .map((s) => s[0]) .slice(0, 2) .join("") .toUpperCase(); // Expanded: full NavLink with label. Rail: centered icon-only button + tooltip. const renderNav = (items: NavItem[]) => items.map((it) => { const Ico = it.icon; if (rail) { return ( ); } return ( } active={isActive(it.to)} onClick={closeMobile} /> ); }); return ( {/* Mobile burger */} {/* Top: profile + collapse/expand toggle */} {rail ? ( setCollapsed(false)} aria-label={t("nav.expandSidebar")} > {initials} ) : ( {initials}
{profile?.full_name || user?.email} {user?.email}
setCollapsed(true)} aria-label={t("nav.collapseSidebar")} >
)}
{/* Navigation */} {rail ? ( {renderNav(NAV)} {profile?.role === "admin" && ( <> {renderNav(ADMIN_NAV)} )} ) : ( <> {renderNav(NAV)} {profile?.role === "admin" && ( <> {renderNav(ADMIN_NAV)} )} )} {/* Bottom: preferences + sign out */} {rail ? ( signOut()} aria-label={t("nav.signout")} > ) : ( <> )}
{/* Suspense lives INSIDE the shell so switching sections only shows a loader in the content area — the sidebar stays put (no full-page flash while a lazy route chunk loads). */} } >
); }