import { useEffect, useRef, useState, type ReactNode } from "react"; import { NavLink, useLocation } from "react-router-dom"; import { IconStar, IconStarFilled } from "@tabler/icons-react"; import { isCurrent, isGroup, type NavEntry, type NavGroup, type NavLeaf } from "./nav"; import { toggleFavorite, useFavorites } from "../utils/favorites"; import { canonicalSearch, describeView, splitView } from "../utils/savedViews"; import { t } from "../i18n"; export interface NavBadge { text: string; className: string; title: string; } interface Flyout { group: NavGroup; top: number; left: number; } /** * The menu in the sidebar: this person's pinned pages first, then the pages and groups. Wide, a group opens in place; narrow (the * icon-only strip) it opens to the side as a small menu instead, because there is no room beside an icon for a list. */ export default function SidebarNav({ nav, pathname, badges, openGroups, onToggleGroup, rail, onNavigate, }: { nav: NavEntry[]; pathname: string; badges: Record; openGroups: Set; onToggleGroup: (id: string) => void; /** The sidebar is the narrow, icon-only strip. */ rail: boolean; /** Called when a link is followed (to close the menu on a phone). */ onNavigate: () => void; }) { const favorites = useFavorites(); const location = useLocation(); const [flyout, setFlyout] = useState(null); const flyoutRef = useRef(null); const opener = useRef(null); // Every page that can be pinned, by path. const leaves = new Map(); for (const entry of nav) { if (isGroup(entry)) entry.items.forEach((i) => leaves.set(i.to, i)); else leaves.set(entry.to, entry); } // A pinned page, or a pinned view of one ("/servers?status=offline"), which is shown under the page's name and what narrows it. const pinned = favorites.flatMap((path) => { const [base, search] = splitView(path); const leaf = leaves.get(base); return leaf ? [{ leaf, path, search }] : []; }); // The side menu goes away when the page changes, when the strip is widened, on Escape, and on a click anywhere else. useEffect(() => setFlyout(null), [pathname, rail]); useEffect(() => { if (!flyout) return; const onDown = (e: MouseEvent) => { const t = e.target as Node; if (flyoutRef.current?.contains(t) || opener.current?.contains(t)) return; setFlyout(null); }; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") { setFlyout(null); opener.current?.focus(); } }; document.addEventListener("mousedown", onDown); document.addEventListener("keydown", onKey); return () => { document.removeEventListener("mousedown", onDown); document.removeEventListener("keydown", onKey); }; }, [flyout]); const badgeOf = (to: string): ReactNode => { const b = badges[to]; return b ? ( {b.title} ) : null; }; const star = (leaf: NavLeaf, path: string = leaf.to, name: string = t(leaf.label)) => { const on = favorites.includes(path); const label = on ? t("Remove {name} from favourites", { name }) : t("Add {name} to favourites", { name }); return ( ); }; const viewRow = (leaf: NavLeaf, path: string, search: string) => { const words = describeView(search); const name = `${t(leaf.label)}: ${words}`; const here = location.pathname === leaf.to && canonicalSearch(location.search) === search; return (
  • `nav-link ${here ? "active" : ""}`} title={rail ? name : undefined} aria-current={here ? "page" : undefined} onClick={onNavigate}> {leaf.icon} {t(leaf.label)} {words} {star(leaf, path, name)}
  • ); }; const leafRow = (leaf: NavLeaf, key: string) => (
  • {leaf.icon} {t(leaf.label)} {badgeOf(leaf.to)} {star(leaf)}
  • ); const subRow = (leaf: NavLeaf) => (
    `dropdown-item d-flex align-items-center ${isActive ? "active" : ""}`} onClick={onNavigate}> {t(leaf.label)} {badgeOf(leaf.to)} {star(leaf)}
    ); return ( ); }