/* ═══════════════════════════════════════════════════════════════ SITE NAVIGATION AND PAGES The site's pages (site_pages, 080_page_registry.sql: Pages → Page management) and the header's and footer's links (Pages → Navigation, 075). Read by pageMeta.js (window.__SITE__, each page's status and title, the sitemap) and served as GET /api/nav for the app (routes/sitePages.js). hidden Not found to anyone below viewer, and left out of what they're sent, links included; a viewer and up sees it, and the page says it's hidden (Layout.tsx) disabled out of the menus, the sitemap and search, for everyone; its address still works 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); /* A link's page: its path without the #section or ?query, or null for an address off the site. */ export const pageOf = (href) => /^\//.test(href) ? href.split(/[?#]/)[0].replace(/(.)\/+$/, "$1") || "/" : null; /* Every page, in the admin's order. */ export function allPages(db) { return db .prepare( `SELECT id, path, title, description, is_hidden, is_disabled FROM site_pages ORDER BY sort_order, title`, ) .all(); } /* The page at this path, or null. */ export function pageAt(db, path) { return ( db .prepare(`SELECT id, path, title, description, is_hidden, is_disabled FROM site_pages WHERE path = ?`) .get(pageOf(path) ?? path) ?? null ); } /* The paths whose pages are hidden, eg. '/shop'. */ export function hiddenPaths(db) { return new Set( db .prepare(`SELECT path FROM site_pages WHERE is_hidden = 1`) .all() .map((row) => row.path), ); } /* 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 and pages from: every page this user may open, every link they may follow (none to a disabled page), and which pages are hidden (so the app can say so). */ export function navFor(db, user) { const sees = seesHidden(user); const pages = allPages(db).filter((page) => sees || page.is_hidden !== 1); const hidden = new Set(allPages(db).filter((p) => p.is_hidden === 1).map((p) => p.path)); const disabled = new Set(pages.filter((p) => p.is_disabled === 1).map((p) => p.path)); 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) => { const page = pageOf(link.path); return !disabled.has(page) && (sees || !hidden.has(page)); }) .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, pages: pages.map((page) => ({ id: page.id, path: page.path, title: page.title, description: page.description, hidden: page.is_hidden === 1, disabled: page.is_disabled === 1, })), hidden: sees ? [...hidden] : [], seesHidden: sees, }; }