NGU-Web/server/src/siteNav.js
Aidan Dona 0cc2a3f721 Navigation in the admin: header and footer links, page titles, hidden pages, Shop
Pages -> Navigation (migration 075) edits the header's pages and buttons
and the footer's links; navConfig.ts is now only the defaults. Page titles
and intros move there from Site settings. A hidden page answers Not found
below viewer (server and app) and is out of the menus and sitemap; signed
in, it shows with a bar. Adds a /shop page of sections, hidden, with its
header button.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 18:01:02 -05:00

65 lines
2.6 KiB
JavaScript

/* ═══════════════════════════════════════════════════════════════
SITE NAVIGATION
Pages → Navigation (075_site_nav.sql): the header's and footer's
links, and which static pages are hidden. Read by pageMeta.js
(window.__SITE__, the page's status, the sitemap) and served as
GET /api/nav for the app (routes/sitePages.js).
A hidden page answers Not found to anyone below viewer, and its
links are left out of what they're sent; a viewer and up sees
both, and the page says it's hidden (Layout.tsx). Who's asking
is the session cookie, so the same page can be hidden from one
visitor and shown to the next: the HTML is no-cache already.
═══════════════════════════════════════════════════════════════ */
import { hasRole } from "./auth.js";
/* Who sees hidden pages. */
export const SEES_HIDDEN = "viewer";
export const seesHidden = (user) => hasRole(user, SEES_HIDDEN);
/* The paths whose pages are hidden, eg. '/shop'. */
export function hiddenPaths(db) {
return new Set(
db
.prepare(`SELECT path FROM site_nav_pages WHERE nav_id = 'site' AND is_hidden = 1`)
.all()
.map((row) => row.path),
);
}
/* A link's page: its path without the #section or ?query, or null
for an address off the site. */
const pageOf = (href) => (/^\//.test(href) ? href.split(/[?#]/)[0].replace(/(.)\/+$/, "$1") : null);
/* Whether this path's page is one the user can't see. */
export function hiddenFrom(db, user, pathname) {
return !seesHidden(user) && hiddenPaths(db).has(pageOf(pathname));
}
/* What the app draws its menus from: every link this user may
follow, and the hidden pages' paths (so the app can answer Not
found, or say a page is hidden, without asking again). */
export function navFor(db, user) {
const hidden = hiddenPaths(db);
const sees = seesHidden(user);
const links = db
.prepare(
`SELECT place, label, path, parent_path, style, new_tab
FROM site_nav_links WHERE nav_id = 'site' ORDER BY sort_order, id`,
)
.all()
.filter((link) => sees || !hidden.has(pageOf(link.path)))
.map((link) => ({
place: link.place,
label: link.label,
path: link.path,
parent: link.parent_path || null,
filled: link.style === "filled",
newTab: link.new_tab === 1,
}));
return { links, hidden: [...hidden], seesHidden: sees };
}