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">