Vion / frontend /src /components /Shell.tsx
MonicaCarter's picture
Claude Opus 4.8
Rebrand (blue+orange), split-panel auth with brand photo, favorites/status/search, retry, typed-delete, dashboard own-sessions, sidebar rail, AI end-call, code-splitting + a11y polish
9b81983
Raw History Blame Contribute Delete
9.11 kB
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>
);
}