Spaces:
Sleeping
Sleeping
File size: 9,107 Bytes
9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 9b81983 dd935c0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | 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 (
<Tooltip key={it.to} label={t(it.labelKey)} position="right">
<ActionIcon
component={Link}
to={it.to}
onClick={closeMobile}
variant={isActive(it.to) ? "light" : "subtle"}
color={isActive(it.to) ? "brand" : "gray"}
size="lg"
radius="md"
aria-label={t(it.labelKey)}
>
<Ico size={20} stroke={1.6} />
</ActionIcon>
</Tooltip>
);
}
return (
<NavLink
key={it.to}
component={Link}
to={it.to}
label={t(it.labelKey)}
leftSection={<Ico size={18} stroke={1.6} />}
active={isActive(it.to)}
onClick={closeMobile}
/>
);
});
return (
<AppShell
navbar={{
width: rail ? RAIL_WIDTH : FULL_WIDTH,
breakpoint: "sm",
collapsed: { mobile: !mobileOpened, desktop: false },
}}
padding="md"
styles={{ navbar: { transition: "width 200ms ease" } }}
>
{/* Mobile burger */}
<Box
hiddenFrom="sm"
style={{ position: "fixed", top: 12, left: 12, zIndex: 300 }}
>
<Burger opened={mobileOpened} onClick={toggleMobile} size="sm" aria-label="Menu" />
</Box>
<AppShell.Navbar p="sm">
{/* Top: profile + collapse/expand toggle */}
<AppShell.Section>
{rail ? (
<Stack align="center" gap="xs">
<Tooltip label={t("nav.expandSidebar")} position="right">
<ActionIcon
variant="subtle"
color="gray"
onClick={() => setCollapsed(false)}
aria-label={t("nav.expandSidebar")}
>
<IconLayoutSidebarLeftExpand size={20} />
</ActionIcon>
</Tooltip>
<Tooltip label={profile?.full_name || user?.email || "Profile"} position="right">
<UnstyledButton component={Link} to="/profile" onClick={closeMobile}>
<Avatar src={profile?.avatar_url ?? undefined} radius="xl" color="brand">
{initials}
</Avatar>
</UnstyledButton>
</Tooltip>
</Stack>
) : (
<Group gap="xs" wrap="nowrap" align="center">
<UnstyledButton
component={Link}
to="/profile"
onClick={closeMobile}
style={{ flex: 1, minWidth: 0, borderRadius: 8, padding: 8 }}
>
<Group gap="sm" wrap="nowrap">
<Avatar
src={profile?.avatar_url ?? undefined}
radius="xl"
color="brand"
alt={profile?.full_name || "Profile"}
>
{initials}
</Avatar>
<div style={{ flex: 1, minWidth: 0 }}>
<Text size="sm" fw={600} truncate>
{profile?.full_name || user?.email}
</Text>
<Text size="xs" c="dimmed" truncate>
{user?.email}
</Text>
</div>
</Group>
</UnstyledButton>
<Tooltip label={t("nav.collapseSidebar")} position="right">
<ActionIcon
visibleFrom="sm"
variant="subtle"
color="gray"
onClick={() => setCollapsed(true)}
aria-label={t("nav.collapseSidebar")}
>
<IconLayoutSidebarLeftCollapse size={20} />
</ActionIcon>
</Tooltip>
</Group>
)}
<Divider my="sm" />
</AppShell.Section>
{/* Navigation */}
<AppShell.Section grow component={ScrollArea}>
{rail ? (
<Stack align="center" gap={6}>
{renderNav(NAV)}
{profile?.role === "admin" && (
<>
<Divider my={4} w="60%" />
{renderNav(ADMIN_NAV)}
</>
)}
</Stack>
) : (
<>
{renderNav(NAV)}
{profile?.role === "admin" && (
<>
<Divider my="sm" />
{renderNav(ADMIN_NAV)}
</>
)}
</>
)}
</AppShell.Section>
{/* Bottom: preferences + sign out */}
<AppShell.Section>
<Divider mb="sm" />
{rail ? (
<Stack align="center" gap="xs">
<ThemeToggle />
<Tooltip label={t("nav.signout")} position="right">
<ActionIcon
variant="light"
color="gray"
size="lg"
radius="md"
onClick={() => signOut()}
aria-label={t("nav.signout")}
>
<IconLogout size={18} />
</ActionIcon>
</Tooltip>
</Stack>
) : (
<>
<Group justify="space-between" mb="sm">
<LanguageToggle />
<ThemeToggle />
</Group>
<Button variant="light" fullWidth onClick={() => signOut()}>
{t("nav.signout")}
</Button>
</>
)}
</AppShell.Section>
</AppShell.Navbar>
<AppShell.Main>
<Container size="lg" px={0}>
{/* 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). */}
<Suspense
fallback={
<Center mih="60vh">
<Loader />
</Center>
}
>
<Outlet />
</Suspense>
</Container>
</AppShell.Main>
</AppShell>
);
}
|