About 1,600 more headings, buttons, table columns, status words, placeholders and short messages are translated. Tables of words built when the app loads are now read when they are drawn, so they follow the language, and a test fails if text is translated at load time. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
209 lines
8.4 KiB
TypeScript
209 lines
8.4 KiB
TypeScript
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<string, NavBadge>;
|
|
openGroups: Set<string>;
|
|
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<Flyout | null>(null);
|
|
const flyoutRef = useRef<HTMLDivElement>(null);
|
|
const opener = useRef<HTMLElement | null>(null);
|
|
|
|
// Every page that can be pinned, by path.
|
|
const leaves = new Map<string, NavLeaf>();
|
|
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 ? (
|
|
<span className={`badge ms-auto ${b.className}`} title={b.title}>
|
|
<span aria-hidden="true">{b.text}</span>
|
|
<span className="visually-hidden">{b.title}</span>
|
|
</span>
|
|
) : 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 (
|
|
<button type="button" className="fav-star" aria-pressed={on} aria-label={label} title={label} onClick={() => void toggleFavorite(path)}>
|
|
{on ? <IconStarFilled size={16} /> : <IconStar size={16} />}
|
|
</button>
|
|
);
|
|
};
|
|
|
|
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 (
|
|
<li className="nav-item nav-row" key={`fav-${path}`}>
|
|
<NavLink to={path} className={() => `nav-link ${here ? "active" : ""}`} title={rail ? name : undefined} aria-current={here ? "page" : undefined} onClick={onNavigate}>
|
|
<span className="nav-link-icon d-md-none d-lg-inline-block">{leaf.icon}</span>
|
|
<span className="nav-link-title">
|
|
{t(leaf.label)} <span className="nav-view">{words}</span>
|
|
</span>
|
|
</NavLink>
|
|
{star(leaf, path, name)}
|
|
</li>
|
|
);
|
|
};
|
|
|
|
const leafRow = (leaf: NavLeaf, key: string) => (
|
|
<li className="nav-item nav-row" key={key}>
|
|
<NavLink to={leaf.to} end={leaf.to === "/"} className="nav-link" title={rail ? t(leaf.label) : undefined} onClick={onNavigate}>
|
|
<span className="nav-link-icon d-md-none d-lg-inline-block">{leaf.icon}</span>
|
|
<span className="nav-link-title">{t(leaf.label)}</span>
|
|
{badgeOf(leaf.to)}
|
|
</NavLink>
|
|
{star(leaf)}
|
|
</li>
|
|
);
|
|
|
|
const subRow = (leaf: NavLeaf) => (
|
|
<div className="nav-row" key={leaf.to}>
|
|
<NavLink to={leaf.to} className={({ isActive }) => `dropdown-item d-flex align-items-center ${isActive ? "active" : ""}`} onClick={onNavigate}>
|
|
<span className="menu-item-icon" aria-hidden="true">
|
|
{leaf.icon}
|
|
</span>
|
|
{t(leaf.label)}
|
|
{badgeOf(leaf.to)}
|
|
</NavLink>
|
|
{star(leaf)}
|
|
</div>
|
|
);
|
|
|
|
return (
|
|
<nav aria-label={t("Main menu")}>
|
|
<ul className="navbar-nav pt-lg-3">
|
|
{pinned.length > 0 && (
|
|
<li className="nav-item nav-favourites">
|
|
<div className="nav-heading" id="nav-favourites-heading">
|
|
{t("Favourites")}
|
|
</div>
|
|
<ul className="navbar-nav" aria-labelledby="nav-favourites-heading">
|
|
{pinned.map((p) => (p.search ? viewRow(p.leaf, p.path, p.search) : leafRow(p.leaf, `fav-${p.leaf.to}`)))}
|
|
</ul>
|
|
</li>
|
|
)}
|
|
|
|
{nav.map((entry) => {
|
|
if (!isGroup(entry)) return leafRow(entry, entry.to);
|
|
const open = openGroups.has(entry.id);
|
|
const holdsCurrent = entry.items.some((item) => isCurrent(pathname, item.to));
|
|
const inside = entry.items.map((item) => badges[item.to]).filter(Boolean);
|
|
const showSummary = (!open || rail) && inside.length > 0;
|
|
const worst = inside.find((b) => b.className.includes("bg-red")) ?? inside.find((b) => b.className.includes("bg-yellow")) ?? inside[0];
|
|
const flyOpen = rail && flyout?.group.id === entry.id;
|
|
return (
|
|
<li className="nav-item dropdown" key={entry.id}>
|
|
<button
|
|
type="button"
|
|
className={`nav-link dropdown-toggle w-100 border-0 bg-transparent text-start ${(rail ? flyOpen : open) ? "show" : ""} ${holdsCurrent ? "active" : ""}`}
|
|
aria-expanded={rail ? flyOpen : open}
|
|
title={rail ? t(entry.label) : undefined}
|
|
onClick={(e) => {
|
|
if (!rail) return onToggleGroup(entry.id);
|
|
if (flyOpen) return setFlyout(null);
|
|
const r = e.currentTarget.getBoundingClientRect();
|
|
opener.current = e.currentTarget;
|
|
setFlyout({ group: entry, top: r.top, left: r.right + 6 });
|
|
}}
|
|
>
|
|
<span className="nav-link-icon d-md-none d-lg-inline-block">{entry.icon}</span>
|
|
<span className="nav-link-title">{t(entry.label)}</span>
|
|
{/* A closed group still says that something inside it needs a look. */}
|
|
{showSummary && (
|
|
<span className={`badge ms-auto me-4 ${worst.className}`} title={inside.map((b) => b.title).join(", ")}>
|
|
<span aria-hidden="true">{inside.reduce((n, b) => n + Number(b.text), 0)}</span>
|
|
<span className="visually-hidden">{inside.map((b) => b.title).join(", ")}</span>
|
|
</span>
|
|
)}
|
|
</button>
|
|
<div className={`dropdown-menu ${open ? "show" : ""}`}>{entry.items.map(subRow)}</div>
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
|
|
{rail && flyout && (
|
|
<div ref={flyoutRef} className="rail-flyout" role="group" aria-label={t(flyout.group.label)} style={{ top: flyout.top, left: flyout.left, maxHeight: `calc(100vh - ${flyout.top}px - 0.75rem)` }}>
|
|
<div className="rail-flyout-title">{t(flyout.group.label)}</div>
|
|
{flyout.group.items.map(subRow)}
|
|
</div>
|
|
)}
|
|
</nav>
|
|
);
|
|
}
|