/* ═══════════════════════════════════════════════════════════════ 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 }; }