Add a setting to leave unused integration pages out of the menu
Settings -> Display -> Menu: when on, the menu, command palette and shortcuts omit the pages of integration kinds nobody has added (off by default, the same for everyone). Pages useful without an integration always stay; adding an integration brings its page back at once. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8f5068c6e0
commit
587e8856a0
13 files changed
+346
-7
No files matched your search
@@ -560,6 +560,8 @@ export type DateFormat = "ymd" | "dmy" | "mdy";
|
||||
export type TimeFormat = "24h" | "12h";
|
||||
|
||||
export interface DisplaySettings {
|
||||
/** Leave out of the menu the pages of integrations nobody has added. */
|
||||
hideUnusedPages: boolean;
|
||||
dateFormat: DateFormat;
|
||||
timeFormat: TimeFormat;
|
||||
pageSize: number;
|
||||
|
||||
@@ -24,6 +24,7 @@ import { DENSITIES, type Density } from "../utils/density";
|
||||
import { readableError } from "../utils/errors";
|
||||
import { toast } from "../utils/toast";
|
||||
import { SETTINGS_INDEX } from "../utils/settingsIndex";
|
||||
import { useHiddenPages } from "../hooks/useMenuHiding";
|
||||
import { SHOW_SETUP_EVENT } from "./SetupGuide";
|
||||
|
||||
const SECRET_TYPE_LABELS: Record<SecretType, string> = {
|
||||
@@ -160,6 +161,7 @@ export default function CommandPalette({
|
||||
const navigate = useNavigate();
|
||||
const { pathname } = useLocation();
|
||||
const favorites = useFavorites();
|
||||
const hiddenPages = useHiddenPages();
|
||||
const canAct = user.role === "admin" || user.role === "operator";
|
||||
|
||||
// Ctrl/Cmd+K opens it from anywhere in the app; Escape closes it.
|
||||
@@ -382,7 +384,7 @@ export default function CommandPalette({
|
||||
}
|
||||
return items;
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [user.role, user.id, canAct, targets, windows, alertsToAck, updatable, assets, onToggleTheme, onSetDensity, pathname, favorites]);
|
||||
}, [user.role, user.id, canAct, targets, windows, alertsToAck, updatable, assets, onToggleTheme, onSetDensity, pathname, favorites, hiddenPages]);
|
||||
|
||||
const terms = query.trim().toLowerCase().split(/\s+/).filter(Boolean);
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { useEffect, useSyncExternalStore } from "react";
|
||||
import { api } from "../api/client";
|
||||
import { getHiddenPages, setHiddenPages, subscribeHiddenPages, unusedPages } from "../utils/navVisibility";
|
||||
|
||||
/** Said by the pages that change what the menu depends on (adding or removing an integration, the Display setting), so it is worked out again at once. */
|
||||
export const MENU_CHANGED = "menu-changed";
|
||||
|
||||
/** Which pages the menu leaves out, as a set that changes when it does — for a component that draws or lists the menu. */
|
||||
export function useHiddenPages(): ReadonlySet<string> {
|
||||
return useSyncExternalStore(subscribeHiddenPages, getHiddenPages, getHiddenPages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the menu's hidden pages up to date: if the Display setting asks for pages of integrations nobody has added to be left out, they are; otherwise nothing is.
|
||||
* Used once, by the shell. When it cannot find out (offline, an error) it leaves the menu as it was — never hiding a page on a guess.
|
||||
*/
|
||||
export function useMenuHiding(): void {
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
const load = () =>
|
||||
Promise.all([api.settings.display(), api.integrations.list()])
|
||||
.then(([d, i]) => {
|
||||
if (!cancelled) setHiddenPages(d.display.hideUnusedPages ? unusedPages(i.integrations.map((x) => x.type)) : []);
|
||||
})
|
||||
.catch(() => {});
|
||||
void load();
|
||||
const timer = setInterval(load, 5 * 60_000);
|
||||
window.addEventListener(MENU_CHANGED, load);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
clearInterval(timer);
|
||||
window.removeEventListener(MENU_CHANGED, load);
|
||||
};
|
||||
}, []);
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { useNavigate } from "react-router-dom";
|
||||
import type { CurrentUser } from "../api/client";
|
||||
import { visibleNav, isGroup } from "../layout/nav";
|
||||
import { GO_SHORTCUTS, SHOW_SHORTCUTS_EVENT } from "../layout/shortcuts";
|
||||
import { useHiddenPages } from "./useMenuHiding";
|
||||
|
||||
/** Whether the keyboard is typing into something, in which case a letter is a letter and not a shortcut. */
|
||||
function isTyping(target: EventTarget | null): boolean {
|
||||
@@ -18,6 +19,7 @@ function isTyping(target: EventTarget | null): boolean {
|
||||
*/
|
||||
export function useShortcuts(user: CurrentUser): void {
|
||||
const navigate = useNavigate();
|
||||
const hiddenPages = useHiddenPages();
|
||||
|
||||
useEffect(() => {
|
||||
const allowed = new Set<string>(["/", "/wall"]);
|
||||
@@ -80,5 +82,5 @@ export function useShortcuts(user: CurrentUser): void {
|
||||
window.removeEventListener("keydown", onKeyDown);
|
||||
reset();
|
||||
};
|
||||
}, [user.role, navigate]);
|
||||
}, [user.role, navigate, hiddenPages]);
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import { alertBadge, useAlertsReport } from "../hooks/useAlertsReport";
|
||||
import { useMyWork } from "../hooks/useMyWork";
|
||||
import { useStackedTables } from "../hooks/useStackedTables";
|
||||
import { useShortcuts } from "../hooks/useShortcuts";
|
||||
import { useHiddenPages, useMenuHiding } from "../hooks/useMenuHiding";
|
||||
import { useFormLabels } from "../hooks/useFormLabels";
|
||||
import ShortcutHelp from "../components/ShortcutHelp";
|
||||
import Avatar from "../components/Avatar";
|
||||
@@ -85,6 +86,8 @@ export default function AppShell({ user, children }: { user: CurrentUser; childr
|
||||
const [away, setAway] = useState<AwayView | null>(null);
|
||||
const environment = useEnvironment();
|
||||
useStackedTables();
|
||||
useMenuHiding();
|
||||
useHiddenPages(); // so the menu is drawn again when the pages it leaves out change
|
||||
useShortcuts(user);
|
||||
useFormLabels();
|
||||
const mainRef = useRef<HTMLElement>(null);
|
||||
|
||||
@@ -64,6 +64,7 @@ import {
|
||||
IconReportAnalytics,
|
||||
} from "@tabler/icons-react";
|
||||
import type { CurrentUser } from "../api/client";
|
||||
import { isPageHidden } from "../utils/navVisibility";
|
||||
|
||||
// The menu: what pages there are, how they are grouped, and which role is needed to see each. Used by the sidebar, the command
|
||||
// palette, the keyboard shortcuts and the breadcrumbs, so they always agree.
|
||||
@@ -222,9 +223,9 @@ export const NAV: NavEntry[] = [
|
||||
|
||||
const roleRank: Record<CurrentUser["role"], number> = { viewer: 0, operator: 1, admin: 2 };
|
||||
|
||||
/** What this role can see: pages it isn't allowed are dropped, an emptied group disappears, and a group down to one page becomes that page's own link. */
|
||||
/** What this role can see: pages it isn't allowed are dropped, so are pages of integrations nobody has added when the Display setting says to leave those out, an emptied group disappears, and a group down to one page becomes that page's own link. */
|
||||
export function visibleNav(role: CurrentUser["role"]): NavEntry[] {
|
||||
const allowed = (leaf: NavLeaf) => !leaf.minRole || roleRank[role] >= roleRank[leaf.minRole];
|
||||
const allowed = (leaf: NavLeaf) => (!leaf.minRole || roleRank[role] >= roleRank[leaf.minRole]) && !isPageHidden(leaf.to);
|
||||
const out: NavEntry[] = [];
|
||||
for (const entry of NAV) {
|
||||
if (!isGroup(entry)) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import DiagLogView from "../views/DiagLogView";
|
||||
import IntegrationEditForm from "../components/IntegrationEditForm";
|
||||
import TableToolbar from "../components/TableToolbar";
|
||||
import { useTableSearch } from "../hooks/useTableSearch";
|
||||
import { MENU_CHANGED } from "../hooks/useMenuHiding";
|
||||
import { downloadCsv } from "../utils/csv";
|
||||
import { confirmDialog } from "../utils/dialogs";
|
||||
import { ErrorNotice } from "../components/Notice";
|
||||
@@ -109,6 +110,7 @@ function IntegrationsList({ user, tabs }: { user: CurrentUser; tabs: ReactNode }
|
||||
.list()
|
||||
.then((res) => {
|
||||
setIntegrations(res.integrations);
|
||||
window.dispatchEvent(new Event(MENU_CHANGED)); // the menu may leave out pages of integrations nobody has added
|
||||
// Deep-link from global search (?editId=) — jump straight to that integration's edit form.
|
||||
const editId = Number(searchParams.get("editId"));
|
||||
const target = res.integrations.find((i) => i.id === editId);
|
||||
|
||||
@@ -2,6 +2,9 @@ import { useEffect, useState } from "react";
|
||||
import { api, type AppSettings, type DateFormat, type TimeFormat } from "../../api/client";
|
||||
import { setDateTimeSettings } from "../../utils/date";
|
||||
import { setPageSize } from "../../utils/pageSize";
|
||||
import { MENU_CHANGED } from "../../hooks/useMenuHiding";
|
||||
import { PAGE_KINDS, unusedPages } from "../../utils/navVisibility";
|
||||
import { NAV, isGroup } from "../../layout/nav";
|
||||
import { ErrorNotice } from "../../components/Notice";
|
||||
import { PageSkeleton } from "../../components/Skeleton";
|
||||
|
||||
@@ -21,6 +24,9 @@ export default function DisplaySettings() {
|
||||
const [dateFormat, setDateFormatState] = useState<DateFormat>("ymd");
|
||||
const [timeFormat, setTimeFormatState] = useState<TimeFormat>("24h");
|
||||
const [pageSize, setPageSizeState] = useState(20);
|
||||
const [hideUnusedPages, setHideUnusedPages] = useState(false);
|
||||
// The kinds of integration that have been added, to say which pages the setting would leave out.
|
||||
const [added, setAdded] = useState<string[] | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [saved, setSaved] = useState(false);
|
||||
const [saveError, setSaveError] = useState<string | null>(null);
|
||||
@@ -32,9 +38,14 @@ export default function DisplaySettings() {
|
||||
setDateFormatState(res.settings.display.dateFormat);
|
||||
setTimeFormatState(res.settings.display.timeFormat);
|
||||
setPageSizeState(res.settings.display.pageSize);
|
||||
setHideUnusedPages(res.settings.display.hideUnusedPages);
|
||||
})
|
||||
.catch((err) => setLoadError(err instanceof Error ? err.message : String(err)))
|
||||
.finally(() => setLoading(false));
|
||||
api.integrations
|
||||
.list()
|
||||
.then((res) => setAdded(res.integrations.map((i) => i.type)))
|
||||
.catch(() => {});
|
||||
}, []);
|
||||
|
||||
async function handleSave() {
|
||||
@@ -42,9 +53,10 @@ export default function DisplaySettings() {
|
||||
setSaveError(null);
|
||||
setSaved(false);
|
||||
try {
|
||||
const res = await api.settings.update({ display: { dateFormat, timeFormat, pageSize } });
|
||||
const res = await api.settings.update({ display: { dateFormat, timeFormat, pageSize, hideUnusedPages } });
|
||||
setDateTimeSettings(res.settings.display);
|
||||
setPageSize(res.settings.display.pageSize);
|
||||
window.dispatchEvent(new Event(MENU_CHANGED));
|
||||
setSaved(true);
|
||||
setTimeout(() => setSaved(false), 3000);
|
||||
} catch (err) {
|
||||
@@ -56,6 +68,8 @@ export default function DisplaySettings() {
|
||||
|
||||
if (loading) return <PageSkeleton />;
|
||||
|
||||
// The menu's pages the setting would leave out, by name, with what has been added so far.
|
||||
const wouldHide = added === null ? null : menuLabelsOf(unusedPages(added));
|
||||
const dateExample = DATE_FORMAT_OPTIONS.find((o) => o.value === dateFormat)?.example ?? "";
|
||||
|
||||
return (
|
||||
@@ -138,7 +152,47 @@ export default function DisplaySettings() {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="col-12">
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">Menu</h3>
|
||||
</div>
|
||||
<div className="card-body">
|
||||
<label className="form-check">
|
||||
<input type="checkbox" className="form-check-input" checked={hideUnusedPages} onChange={(e) => setHideUnusedPages(e.target.checked)} />
|
||||
<span className="form-check-label">Hide the pages of integrations I have not added</span>
|
||||
</label>
|
||||
<div className="form-hint mt-1">
|
||||
A page for something that is not in your homelab (Jellyfin, Gitea, n8n …) is left out of the menu, the command palette and the keyboard shortcuts until you add that integration under
|
||||
Administration → Integrations. It is still there by its address, and pages that are useful without an integration (Servers, Alerts, DNS, Secrets …) always stay. A group with nothing left disappears.
|
||||
</div>
|
||||
{wouldHide !== null && (
|
||||
<div className="mt-3 small" aria-live="polite">
|
||||
{wouldHide.length === 0 ? (
|
||||
<span className="text-secondary">With what you have added, there is nothing to leave out.</span>
|
||||
) : (
|
||||
<>
|
||||
<span className="text-secondary">{hideUnusedPages ? "Left out now" : "Would leave out"} ({wouldHide.length}): </span>
|
||||
{wouldHide.join(", ")}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
/** The menu's names for pages, in the menu's order, for the pages given (by path). */
|
||||
function menuLabelsOf(pages: Set<string>): string[] {
|
||||
const out: string[] = [];
|
||||
for (const entry of NAV) {
|
||||
const leaves = isGroup(entry) ? entry.items : [entry];
|
||||
for (const leaf of leaves) if (pages.has(leaf.to.split("?")[0]) && PAGE_KINDS[leaf.to.split("?")[0]]) out.push(leaf.label);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import type { IntegrationType } from "../api/client";
|
||||
import { TYPE_PAGES } from "./integrationTypes";
|
||||
|
||||
/**
|
||||
* Which pages of the menu belong to an integration, so that a homelab without that integration need not see them.
|
||||
*
|
||||
* A page is only ever hidden when it exists for the sake of integrations (Proxmox, Jellyfin, Gitea …). A page that has uses of its own
|
||||
* (Servers, Alerts, DNS, Secrets, Duplicati …) is never in this map, so it is never hidden. The page is still there by its address either way.
|
||||
*/
|
||||
|
||||
/** The path of a menu link, without a query ("/backups?view=pbs" is "/backups"). */
|
||||
export function pagePath(to: string): string {
|
||||
const cut = to.search(/[?#]/);
|
||||
return cut === -1 ? to : to.slice(0, cut);
|
||||
}
|
||||
|
||||
/** For each page, the kinds of integration that give it something to show. Built from the page each kind gets, plus what a page also reads from elsewhere. */
|
||||
export const PAGE_KINDS: Record<string, IntegrationType[]> = (() => {
|
||||
const out: Record<string, IntegrationType[]> = {};
|
||||
for (const [type, page] of Object.entries(TYPE_PAGES) as [IntegrationType, string][]) {
|
||||
const path = pagePath(page);
|
||||
(out[path] ??= []).push(type);
|
||||
}
|
||||
// The backups page lists Proxmox's own backups as well as the Backup Server's, so a Proxmox is reason enough for it.
|
||||
(out["/backups"] ??= []).push("proxmox");
|
||||
return out;
|
||||
})();
|
||||
|
||||
/** The pages with nothing to show: every one of the kinds behind it is missing from what has been added. Pure. */
|
||||
export function unusedPages(added: Iterable<string>): Set<string> {
|
||||
const have = new Set(added);
|
||||
const hidden = new Set<string>();
|
||||
for (const [page, kinds] of Object.entries(PAGE_KINDS)) {
|
||||
if (!kinds.some((k) => have.has(k))) hidden.add(page);
|
||||
}
|
||||
return hidden;
|
||||
}
|
||||
|
||||
// What the menu leaves out right now. Held here, not in a component, so the sidebar, the command palette and the keyboard shortcuts — which all ask the
|
||||
// menu what there is — agree without being handed it (the same way the date and page-size settings are held).
|
||||
let hidden: ReadonlySet<string> = new Set();
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
/** Sets which pages the menu leaves out (an empty set shows them all). Tells whoever is listening only when it has changed. */
|
||||
export function setHiddenPages(pages: Iterable<string>): void {
|
||||
const next = new Set(pages);
|
||||
if (next.size === hidden.size && [...next].every((p) => hidden.has(p))) return;
|
||||
hidden = next;
|
||||
listeners.forEach((l) => l());
|
||||
}
|
||||
|
||||
export function getHiddenPages(): ReadonlySet<string> {
|
||||
return hidden;
|
||||
}
|
||||
|
||||
export function subscribeHiddenPages(listener: () => void): () => void {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
}
|
||||
|
||||
/** Is this menu link one the menu leaves out? */
|
||||
export function isPageHidden(to: string): boolean {
|
||||
return hidden.has(pagePath(to));
|
||||
}
|
||||
Reference in new issue
Block a user