Favourite pages that follow the user, and a narrow icon-only sidebar

A star beside each page in the sidebar pins it under "Favourites" at the top of
the menu; the list is saved with the user (users.favorites, migration 0020), so
it follows them to any browser, is offered first in the search box, and can be
changed from there too. A button at the foot of the sidebar, or the [ key,
narrows it to a strip of icons, where a group's pages open in a small menu
beside its icon and the alert and maintenance counts stay visible; the choice
is kept per browser and applies only on wide screens. Menu text on a coloured
(environment) sidebar is full strength for contrast, and menu badges sit in
line on a phone.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-15 22:38:48 +02:00
1 parent 57fd4f8c52
commit 65263a64e3
17 files changed
+2736 -86

No files matched your search

+57 -74
View File
@@ -1,8 +1,10 @@
import { useEffect, useRef, useState, type ReactNode } from "react";
import { Link, NavLink, useLocation } from "react-router-dom";
import { IconMenu2, IconSun, IconMoon, IconShieldLock, IconTool, IconChevronUp, IconLogout, IconDeviceDesktop } from "@tabler/icons-react";
import { IconMenu2, IconSun, IconMoon, IconShieldLock, IconTool, IconChevronUp, IconLogout, IconDeviceDesktop, IconChevronsLeft, IconChevronsRight } from "@tabler/icons-react";
import { api, type CurrentUser, type MaintenanceWindow } from "../api/client";
import { isCurrent, isGroup, visibleNav, type NavGroup } from "./nav";
import SidebarNav, { type NavBadge } from "./SidebarNav";
import { initFavorites } from "../utils/favorites";
import { formatRemaining } from "../utils/duration";
import { getTheme, setTheme, type Theme } from "../utils/theme";
import { DENSITIES, getDensity, setDensity, type Density } from "../utils/density";
@@ -15,10 +17,19 @@ import { useFormLabels } from "../hooks/useFormLabels";
import ShortcutHelp from "../components/ShortcutHelp";
import { useBreadcrumbTailValue } from "../utils/breadcrumb";
interface NavBadge {
text: string;
className: string;
title: string;
const RAIL_KEY = "hlm-sidebar-rail";
/** Whether the screen is at least this wide; follows the window as it is resized. */
function useMediaQuery(query: string): boolean {
const [matches, setMatches] = useState(() => window.matchMedia(query).matches);
useEffect(() => {
const list = window.matchMedia(query);
const onChange = () => setMatches(list.matches);
list.addEventListener("change", onChange);
onChange();
return () => list.removeEventListener("change", onChange);
}, [query]);
return matches;
}
/** Up to two initials for the little avatar in the user menu. */
@@ -42,6 +53,33 @@ export default function AppShell({ user, children }: { user: CurrentUser; childr
const [sidebarOpen, setSidebarOpen] = useState(false);
const [theme, setThemeState] = useState<Theme>(() => getTheme());
const [density, setDensityState] = useState<Density>(() => getDensity());
// This person's pinned pages come with the user; load them before the menu is first drawn.
useState(() => {
initFavorites(user.favorites);
return null;
});
// The narrow, icon-only sidebar: remembered per browser (a laptop wants it, a big monitor may not), and only on wide screens.
const [collapsed, setCollapsed] = useState(() => {
try {
return localStorage.getItem(RAIL_KEY) === "1";
} catch {
return false;
}
});
const wide = useMediaQuery("(min-width: 992px)");
const rail = collapsed && wide;
useEffect(() => {
try {
localStorage.setItem(RAIL_KEY, collapsed ? "1" : "0");
} catch {
// not remembered; it still applies for this visit
}
}, [collapsed]);
useEffect(() => {
const toggle = () => setCollapsed((c) => !c);
window.addEventListener("toggle-sidebar", toggle);
return () => window.removeEventListener("toggle-sidebar", toggle);
}, []);
const [maintenance, setMaintenance] = useState<MaintenanceWindow[]>([]);
const environment = useEnvironment();
useStackedTables();
@@ -158,16 +196,6 @@ export default function AppShell({ user, children }: { user: CurrentUser; childr
title: `${maintenance.length} maintenance window${maintenance.length === 1 ? "" : "s"} open`,
};
}
const badgeOf = (to: string) => {
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;
};
function toggleTheme() {
const next: Theme = theme === "dark" ? "light" : "dark";
setTheme(next);
@@ -175,7 +203,7 @@ export default function AppShell({ user, children }: { user: CurrentUser; childr
}
return (
<div className="page">
<div className={`page ${rail ? "sidebar-rail" : ""}`}>
<a
className="skip-link"
href="#main"
@@ -202,6 +230,7 @@ export default function AppShell({ user, children }: { user: CurrentUser; childr
</button>
<h1 className="navbar-brand navbar-brand-autodark">
<NavLink to="/" aria-label="Homelab Manager">
<img className="rail-mark" src="/icon-192.png" alt="" width={44} height={44} />
<span className="logo-frame">
<img src="/logo.webp" alt="Homelab Manager" width={901} height={460} />
</span>
@@ -219,65 +248,19 @@ export default function AppShell({ user, children }: { user: CurrentUser; childr
}}
/>
</div>
<nav aria-label="Main menu">
<ul className="navbar-nav pt-lg-3">
{nav.map((entry) => {
if (!isGroup(entry)) {
return (
<li className="nav-item" key={entry.to}>
<NavLink to={entry.to} end={entry.to === "/"} className="nav-link" onClick={() => setSidebarOpen(false)}>
<span className="nav-link-icon d-md-none d-lg-inline-block">{entry.icon}</span>
<span className="nav-link-title">{entry.label}</span>
{badgeOf(entry.to)}
</NavLink>
</li>
);
}
const open = openGroups.has(entry.id);
const holdsCurrent = entry.items.some((item) => isCurrent(pathname, item.to));
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 ${open ? "show" : ""} ${holdsCurrent ? "active" : ""}`}
aria-expanded={open}
onClick={() => toggleGroup(entry.id)}
>
<span className="nav-link-icon d-md-none d-lg-inline-block">{entry.icon}</span>
<span className="nav-link-title">{entry.label}</span>
{/* A closed group still says that something inside it needs a look. */}
{!open &&
(() => {
const inside = entry.items.map((item) => badges[item.to]).filter(Boolean);
if (inside.length === 0) return null;
const worst = inside.find((b) => b.className.includes("bg-red")) ?? inside.find((b) => b.className.includes("bg-yellow")) ?? inside[0];
return (
<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((item) => (
<NavLink
key={item.to}
to={item.to}
className={({ isActive }) => `dropdown-item d-flex align-items-center ${isActive ? "active" : ""}`}
onClick={() => setSidebarOpen(false)}
>
{item.label}
{badgeOf(item.to)}
</NavLink>
))}
</div>
</li>
);
})}
</ul>
</nav>
<SidebarNav nav={nav} pathname={pathname} badges={badges} openGroups={openGroups} onToggleGroup={toggleGroup} rail={rail} onNavigate={() => setSidebarOpen(false)} />
<div className="navbar-footer px-3 user-menu" ref={menuRef}>
<button
type="button"
className="rail-toggle d-none d-lg-flex"
aria-expanded={!collapsed}
aria-label={collapsed ? "Widen the sidebar" : "Narrow the sidebar to icons"}
title={collapsed ? "Widen the sidebar ([)" : "Narrow the sidebar to icons ([)"}
onClick={() => setCollapsed((c) => !c)}
>
{collapsed ? <IconChevronsRight size={16} /> : <IconChevronsLeft size={16} />}
<span className="rail-toggle-text">Narrow sidebar</span>
</button>
{menuOpen && (
<div className="user-menu-list" role="menu">
<div className="user-menu-density" role="group" aria-label="How tightly pages are packed">
+180
View File
@@ -0,0 +1,180 @@
import { useEffect, useRef, useState, type ReactNode } from "react";
import { NavLink } 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";
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 [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);
}
const pinned = favorites.map((p) => leaves.get(p)).filter((l): l is NavLeaf => !!l);
// 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) => {
const on = favorites.includes(leaf.to);
const label = on ? `Remove ${leaf.label} from favourites` : `Add ${leaf.label} to favourites`;
return (
<button type="button" className="fav-star" aria-pressed={on} aria-label={label} title={label} onClick={() => void toggleFavorite(leaf.to)}>
{on ? <IconStarFilled size={16} /> : <IconStar size={16} />}
</button>
);
};
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 ? 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">{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}>
{leaf.label}
{badgeOf(leaf.to)}
</NavLink>
{star(leaf)}
</div>
);
return (
<nav aria-label="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">
Favourites
</div>
<ul className="navbar-nav" aria-labelledby="nav-favourites-heading">
{pinned.map((leaf) => leafRow(leaf, `fav-${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 ? 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">{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={flyout.group.label} style={{ top: flyout.top, left: flyout.left, maxHeight: `calc(100vh - ${flyout.top}px - 0.75rem)` }}>
<div className="rail-flyout-title">{flyout.group.label}</div>
{flyout.group.items.map(subRow)}
</div>
)}
</nav>
);
}
+1
View File
@@ -22,6 +22,7 @@ export const GO_SHORTCUTS: GoShortcut[] = [
export const OTHER_SHORTCUTS: { keys: string; label: string }[] = [
{ keys: "Ctrl K", label: "Search, and run actions (silence a server, switch theme…)" },
{ keys: "/", label: "Jump to the search box of the table on this page" },
{ keys: "[", label: "Narrow the sidebar to icons, or widen it again" },
{ keys: "?", label: "Show this list" },
{ keys: "Esc", label: "Close a dialog, the search or this list" },
];