Files
co-lite/solvway-admin-portal/app/components/app/Sidebar.tsx
T
ngqileandClaude Opus 5.5 1071ddaa2a feat(admin-portal): cut babyboo portal down to Co-Lite login, users and roles
Removes every other screen, API route, component, test and the dependencies only
they used. Cookie colite_admin, Co-Lite branding, BO_USERS/BO_ROLES modules only.
The user form no longer loads branches, events or country scope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 21:24:30 +08:00

329 lines
12 KiB
TypeScript

"use client";
import { useEffect, useId, useState } from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { ChevronDown, PanelLeftClose, PanelLeftOpen } from "lucide-react";
import { useAclSidebar } from "@/app/lib/useAclSidebar";
import { currentTrail, type SidebarLink } from "@/app/lib/aclSidebar";
import { useT } from "@/app/lib/i18n/MessagesProvider";
import { iconFor } from "./icon-map";
import { cn } from "@/app/lib/utils";
import { Button } from "@/app/components/ui/button";
import { Popover, PopoverContent, PopoverTrigger } from "@/app/components/ui/popover";
import PoweredBy from "./PoweredBy";
// The wordmark is the brand name: data, not translatable copy.
const BRAND = "Co-Lite";
// Sidebar entries come from the signed-in user's live aclMenu (useAclSidebar,
// Task 5), replacing the old static src/components/app/nav-config.ts list.
// This is a deliberately simpler renderer than the pre-port
// src/components/app/app-sidebar.tsx: no query-string-scoped active matching
// (the aclMenu tree has no "?status=active"-style leaves, only distinct
// urls). The desktop aside can collapse to an icon rail; the
// mobile drawer (Header) always shows the full, labelled tree.
type NavProps = { trail: SidebarLink[]; onNavigate?: () => void };
function NavNode({ link, depth, trail, onNavigate }: { link: SidebarLink; depth: number } & NavProps) {
const t = useT();
const Icon = iconFor(link.iconName);
const hasChildren = link.children.length > 0;
if (!hasChildren) {
const active = trail[trail.length - 1] === link;
return (
<li>
<Link
href={link.url}
aria-current={active ? "page" : undefined}
onClick={onNavigate}
className={cn(
"flex items-center gap-3 rounded-lg px-3 transition-colors",
"focus-visible:ring-2 focus-visible:ring-sidebar-ring focus-visible:outline-none",
depth === 0 ? "min-h-11 text-sm font-medium" : "min-h-9 text-sm",
active
? depth === 0
? "bg-sidebar-primary text-sidebar-primary-foreground"
: "bg-sidebar-accent font-medium text-sidebar-accent-foreground"
: "text-muted-foreground hover:bg-sidebar-accent hover:text-sidebar-accent-foreground",
)}
>
{/* eslint-disable-next-line react-hooks/static-components -- Icon comes from
icon-map.ts's static ICONS lookup table keyed by iconName; the component
reference it returns is stable across renders for a given iconName. */}
{Icon && <Icon aria-hidden="true" className="size-5 shrink-0" />}
<span className="truncate">{t(link.name)}</span>
</Link>
</li>
);
}
return <NavGroup link={link} depth={depth} trail={trail} onNavigate={onNavigate} Icon={Icon} />;
}
function NavGroup({
link,
depth,
trail,
onNavigate,
Icon,
}: {
link: SidebarLink;
depth: number;
Icon: ReturnType<typeof iconFor>;
} & NavProps) {
const t = useT();
const active = trail.includes(link);
const [open, setOpen] = useState(active);
// Open the group when a navigation from outside the sidebar (an in-page link) lands on one
// of its pages. Adjusted during render, not in an effect, per React's "storing information
// from previous renders" pattern; a group the admin opened by hand is left alone.
const [wasActive, setWasActive] = useState(active);
if (active !== wasActive) {
setWasActive(active);
if (active) setOpen(true);
}
return (
<li>
<button
type="button"
onClick={() => setOpen((v) => !v)}
aria-expanded={open}
data-current={active ? "true" : undefined}
className={cn(
"flex w-full items-center gap-3 rounded-lg px-3 transition-colors",
"focus-visible:ring-2 focus-visible:ring-sidebar-ring focus-visible:outline-none",
depth === 0 ? "min-h-11 text-sm font-medium" : "min-h-9 text-sm",
active ? "text-sidebar-accent-foreground" : "text-muted-foreground",
"hover:bg-sidebar-accent hover:text-sidebar-accent-foreground",
)}
>
{Icon && <Icon aria-hidden="true" className="size-5 shrink-0" />}
<span className="truncate">{t(link.name)}</span>
<ChevronDown
aria-hidden="true"
className={cn("ml-auto size-4 shrink-0 transition-transform duration-200", open ? "rotate-0" : "-rotate-90")}
/>
</button>
{open && (
<ul className="mt-1 ml-3 space-y-0.5 border-l pl-3">
{link.children.map((child) => (
<NavNode key={child.url || child.name} link={child} depth={depth + 1} trail={trail} onNavigate={onNavigate} />
))}
</ul>
)}
</li>
);
}
// A top-level entry in the icon rail: the icon only, with the label kept as accessible name and tooltip.
const railItemClass = cn(
"flex size-11 items-center justify-center rounded-lg transition-colors",
"focus-visible:ring-2 focus-visible:ring-sidebar-ring focus-visible:outline-none",
);
const railIdleClass = "text-muted-foreground hover:bg-sidebar-accent hover:text-sidebar-accent-foreground";
const railActiveClass = "bg-sidebar-primary text-sidebar-primary-foreground";
function RailNode({ link, trail }: { link: SidebarLink; trail: SidebarLink[] }) {
const t = useT();
const Icon = iconFor(link.iconName);
const label = t(link.name);
if (link.children.length === 0) {
const active = trail[trail.length - 1] === link;
return (
<li className="flex justify-center">
<Link
href={link.url}
aria-current={active ? "page" : undefined}
aria-label={label}
title={label}
className={cn(railItemClass, active ? railActiveClass : railIdleClass)}
>
{/* eslint-disable-next-line react-hooks/static-components -- stable icon-map lookup, as in NavNode. */}
{Icon && <Icon aria-hidden="true" className="size-5 shrink-0" />}
</Link>
</li>
);
}
return <RailGroup link={link} trail={trail} label={label} Icon={Icon} />;
}
// A group in the rail opens a flyout to its right listing its children with the full sidebar's rendering.
function RailGroup({
link,
trail,
label,
Icon,
}: {
link: SidebarLink;
trail: SidebarLink[];
label: string;
Icon: ReturnType<typeof iconFor>;
}) {
const [open, setOpen] = useState(false);
const headingId = useId();
const active = trail.includes(link);
// Close the flyout when the page changes from outside it (back button, an in-page link). Adjusted
// during render, as in NavGroup; following a link inside the flyout closes it via onNavigate.
const pathname = usePathname();
const [lastPath, setLastPath] = useState(pathname);
if (pathname !== lastPath) {
setLastPath(pathname);
setOpen(false);
}
return (
<li className="flex justify-center">
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<button
type="button"
aria-label={label}
title={label}
data-current={active ? "true" : undefined}
className={cn(railItemClass, active ? railActiveClass : railIdleClass)}
>
{Icon && <Icon aria-hidden="true" className="size-5 shrink-0" />}
</button>
</PopoverTrigger>
<PopoverContent side="right" align="start" aria-labelledby={headingId} className="w-64 bg-sidebar p-2">
<p id={headingId} className="px-3 pt-1 pb-2 text-xs font-semibold text-muted-foreground">
{label}
</p>
<ul className="space-y-0.5">
{link.children.map((child) => (
<NavNode
key={child.url || child.name}
link={child}
depth={1}
trail={trail}
onNavigate={() => setOpen(false)}
/>
))}
</ul>
</PopoverContent>
</Popover>
</li>
);
}
// Shared by the desktop aside and the mobile drawer (Header); onNavigate lets the drawer close on a link.
// Only the desktop aside passes `collapsed`, so the drawer always shows the full, labelled tree.
export function SidebarNav({ onNavigate, collapsed = false }: { onNavigate?: () => void; collapsed?: boolean }) {
const t = useT();
const links = useAclSidebar();
const trail = currentTrail(links, usePathname());
return (
<nav aria-label={t("SidebarMainNav")} className="scrollable flex-1 overflow-x-hidden overflow-y-auto">
<ul className={cn("space-y-1", collapsed ? "px-2 py-3" : "p-3")}>
{links.map((link) =>
collapsed ? (
<RailNode key={link.url || link.name} link={link} trail={trail} />
) : (
<NavNode key={link.url || link.name} link={link} depth={0} trail={trail} onNavigate={onNavigate} />
),
)}
</ul>
</nav>
);
}
export function BrandLink({ onClick }: { onClick?: () => void }) {
return (
<Link
href="/"
onClick={onClick}
className="flex items-center gap-2.5 rounded-md focus-visible:ring-2 focus-visible:ring-sidebar-ring focus-visible:outline-none"
>
<span className="brand-wordmark text-lg">{BRAND}</span>
</Link>
);
}
const COLLAPSED_KEY = "bb_sidebar_collapsed";
function readCollapsed(): boolean {
try {
return localStorage.getItem(COLLAPSED_KEY) === "1";
} catch {
// Private browsing or a blocked storage partition: expanded is the default.
return false;
}
}
function persistCollapsed(collapsed: boolean) {
try {
localStorage.setItem(COLLAPSED_KEY, collapsed ? "1" : "0");
} catch {
// Persisting is a nicety; the in-memory state still applies for this page's life.
}
}
export default function Sidebar() {
const t = useT();
// Expanded on the server and during hydration; the stored choice is applied after mount so both renders agree.
const [collapsed, setCollapsed] = useState(false);
// The width animates only after a toggle: applying a stored collapse on load must not animate
// 272 -> 76px (and reflow the page) on every navigation.
const [animate, setAnimate] = useState(false);
useEffect(() => {
// eslint-disable-next-line react-hooks/set-state-in-effect -- localStorage is only readable after mount; reading it during render would make the hydration render disagree with the server.
if (readCollapsed()) setCollapsed(true);
}, []);
// Derives from state, not storage, so a blocked localStorage still toggles (as in lib/theme.tsx).
const toggle = () => {
const next = !collapsed;
setAnimate(true);
setCollapsed(next);
persistCollapsed(next);
};
return (
<aside
data-collapsed={collapsed ? "true" : "false"}
style={{ width: collapsed ? "var(--sidebar-width-collapsed)" : "var(--sidebar-width)" }}
className={cn(
"sticky top-0 hidden h-svh shrink-0 flex-col border-r bg-sidebar lg:flex",
animate && "transition-[width] duration-200 ease-out motion-reduce:transition-none",
)}
>
{/* Header-high in both states so its border lines up with the header's; the rail shows only the toggle. */}
<div
className={cn(
"flex h-16 shrink-0 items-center border-b",
collapsed ? "justify-center" : "justify-between gap-2 px-4",
)}
>
{!collapsed && <BrandLink />}
<Button
variant="ghost"
size="icon"
onClick={toggle}
aria-label={collapsed ? t("SidebarExpand") : t("SidebarCollapse")}
aria-expanded={!collapsed}
className="text-muted-foreground"
>
{collapsed ? (
<PanelLeftOpen aria-hidden="true" className="size-5" />
) : (
<PanelLeftClose aria-hidden="true" className="size-5" />
)}
</Button>
</div>
<SidebarNav collapsed={collapsed} />
<div className={cn("shrink-0 border-t text-center", collapsed ? "px-1 py-3" : "p-3")}>
<PoweredBy compact={collapsed} />
</div>
</aside>
);
}