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>
This commit is contained in:
parent
b49497cc59
commit
0cc2a3f721
30 changed files with 716 additions and 216 deletions
|
|
@ -53,6 +53,7 @@ import { timeline } from "./admin-schema/timeline.js";
|
|||
import { frontPage } from "./admin-schema/frontPage.js";
|
||||
import { blogPosts } from "./admin-schema/blog.js";
|
||||
import { siteSettings } from "./admin-schema/siteSettings.js";
|
||||
import { siteNav } from "./admin-schema/siteNav.js";
|
||||
import { newsletters } from "./admin-schema/newsletters.js";
|
||||
import { personTagTypes } from "./admin-schema/personTags.js";
|
||||
import { tags } from "./admin-schema/tags.js";
|
||||
|
|
@ -64,6 +65,7 @@ import {
|
|||
resourcesPage,
|
||||
organizationPage,
|
||||
historyPage,
|
||||
shopPage,
|
||||
} from "./admin-schema/sitePages.js";
|
||||
|
||||
export const ENTITIES = {
|
||||
|
|
@ -84,6 +86,8 @@ export const ENTITIES = {
|
|||
resources_page: resourcesPage,
|
||||
organization_page: organizationPage,
|
||||
history_page: historyPage,
|
||||
shop_page: shopPage,
|
||||
navigation: siteNav,
|
||||
site_settings: siteSettings,
|
||||
newsletters,
|
||||
};
|
||||
|
|
|
|||
44
server/src/admin-schema/siteNav.js
Normal file
44
server/src/admin-schema/siteNav.js
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import { text, bool, enumeration } from "./columns.js";
|
||||
|
||||
/* ── Navigation (075_site_nav.sql) ───────────────────────────────
|
||||
Pages → Navigation: the header's and footer's links, in order,
|
||||
and each static page's title, intro and whether it's hidden
|
||||
(siteNav.js reads it for the site). Admins and up: it can take a
|
||||
page off the site. */
|
||||
export const siteNav = {
|
||||
key: "navigation",
|
||||
table: "site_nav",
|
||||
idColumn: "id",
|
||||
idKind: "slug",
|
||||
singleton: "site",
|
||||
concurrency: "updated_at",
|
||||
writeRole: "admin",
|
||||
|
||||
list: { columns: ["id", "updated_at"], filters: [], search: [], order: "id" },
|
||||
|
||||
columns: [],
|
||||
|
||||
children: [
|
||||
{
|
||||
key: "links",
|
||||
table: "site_nav_links",
|
||||
owner: { column: "nav_id" },
|
||||
order: "sort_order",
|
||||
columns: [
|
||||
enumeration("place", ["main", "action", "footer", "legal"], { required: true }),
|
||||
text("label", { required: true }),
|
||||
text("path", { required: true }),
|
||||
text("parent_path"),
|
||||
enumeration("style", ["filled"]),
|
||||
bool("new_tab"),
|
||||
],
|
||||
},
|
||||
{
|
||||
key: "pages",
|
||||
table: "site_nav_pages",
|
||||
owner: { column: "nav_id" },
|
||||
order: "sort_order",
|
||||
columns: [text("path", { required: true }), text("title"), text("description"), bool("is_hidden")],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
|
@ -2,7 +2,8 @@ import { text, url, int, enumeration, featuredCardsChild, blocksChild } from "./
|
|||
|
||||
/* ── The site's own pages (056_site_pages.sql) ───────────────────
|
||||
|
||||
Community, Retreats, Resources, Organization and History: one
|
||||
Community, Retreats, Resources, Organization, History and Shop
|
||||
(075): one
|
||||
site_pages row each, edited as its own singleton, with its
|
||||
sections nested under it and each section's blocks under that
|
||||
(the blocks' items under them, as on any record). A section is
|
||||
|
|
@ -106,3 +107,4 @@ export const retreatsPage = sitePage("retreats");
|
|||
export const resourcesPage = sitePage("resources");
|
||||
export const organizationPage = sitePage("organization");
|
||||
export const historyPage = sitePage("history");
|
||||
export const shopPage = sitePage("shop");
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@ import { text, enumeration } from "./columns.js";
|
|||
|
||||
/* ── Site settings (035_site_settings.sql) ──────────────────────
|
||||
A singleton of overrides for what site.json says: name,
|
||||
description, preview image, icons, search indexing, and static
|
||||
pages' titles and intros. Superadmins only. pageMeta.js merges it
|
||||
description, preview image, icons and search indexing (static
|
||||
pages' titles and intros are Pages → Navigation's, siteNav.js).
|
||||
Superadmins only. pageMeta.js merges it
|
||||
over site.json. */
|
||||
export const siteSettings = {
|
||||
key: "site_settings",
|
||||
|
|
@ -39,13 +40,6 @@ export const siteSettings = {
|
|||
],
|
||||
|
||||
children: [
|
||||
{
|
||||
key: "pages",
|
||||
table: "site_setting_pages",
|
||||
owner: { column: "settings_id" },
|
||||
order: "sort_order",
|
||||
columns: [text("path", { required: true }), text("title"), text("description")],
|
||||
},
|
||||
{
|
||||
key: "colors",
|
||||
table: "site_setting_colors",
|
||||
|
|
|
|||
122
server/src/migrations/075_site_nav.sql
Normal file
122
server/src/migrations/075_site_nav.sql
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
-- ═══════════════════════════════════════════════════════════════
|
||||
-- 075 SITE NAVIGATION
|
||||
--
|
||||
-- Pages → Navigation edits what navConfig.ts used to fix in code:
|
||||
-- the header's links and buttons, the footer's, and each page's
|
||||
-- title, intro and whether it is hidden. navConfig.ts stays as the
|
||||
-- defaults the app starts from before this arrives.
|
||||
--
|
||||
-- site_nav the editor's one row ('site')
|
||||
-- site_nav_links every link, in order, by where it shows:
|
||||
-- main the header's pages (sections drop
|
||||
-- down under each); with a parent, in
|
||||
-- that page's dropdown instead
|
||||
-- action the header's buttons, right side
|
||||
-- (and the phone menu's list under
|
||||
-- the pages); a filled one also sits
|
||||
-- beside the footer's logo
|
||||
-- footer the footer's Get in Touch list
|
||||
-- legal the footer's bottom row
|
||||
-- site_nav_pages a static page's title and intro (the page's
|
||||
-- heading and its search text) over site.json's,
|
||||
-- and is_hidden: a hidden page is out of the nav
|
||||
-- and the sitemap and answers Not found to anyone
|
||||
-- below viewer (pageMeta.js, siteNav.js)
|
||||
--
|
||||
-- site_setting_pages (Site settings → Page titles and intros) moves
|
||||
-- into site_nav_pages and is dropped.
|
||||
--
|
||||
-- The Shop page: site_pages takes 'shop' (a page of sections like
|
||||
-- the others), rebuilt since its CHECK can't change in place. It
|
||||
-- starts hidden, with its header button.
|
||||
-- ═══════════════════════════════════════════════════════════════
|
||||
|
||||
CREATE TABLE site_nav (
|
||||
id TEXT PRIMARY KEY CHECK (id = 'site'),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
) STRICT;
|
||||
|
||||
INSERT INTO site_nav (id) VALUES ('site');
|
||||
|
||||
CREATE TABLE site_nav_links (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
nav_id TEXT NOT NULL REFERENCES site_nav (id) ON DELETE CASCADE,
|
||||
sort_order INTEGER NOT NULL DEFAULT 0,
|
||||
place TEXT NOT NULL CHECK (place IN ('main', 'action', 'footer', 'legal')),
|
||||
label TEXT NOT NULL,
|
||||
-- '/retreats', '/organization#contact', or an https:// address
|
||||
path TEXT NOT NULL,
|
||||
-- main links only: the page whose dropdown it sits in
|
||||
parent_path TEXT,
|
||||
-- action links only: null is the outlined button
|
||||
style TEXT CHECK (style IN ('filled')),
|
||||
new_tab INTEGER NOT NULL DEFAULT 0 CHECK (new_tab IN (0, 1))
|
||||
) STRICT;
|
||||
CREATE INDEX site_nav_links_order_idx ON site_nav_links (nav_id, sort_order);
|
||||
|
||||
INSERT INTO site_nav_links (nav_id, sort_order, place, label, path, parent_path, style, new_tab) VALUES
|
||||
('site', 0, 'main', 'Home', '/', NULL, NULL, 0),
|
||||
('site', 1, 'main', 'Retreats', '/retreats', NULL, NULL, 0),
|
||||
('site', 2, 'main', 'Community', '/community', NULL, NULL, 0),
|
||||
('site', 3, 'main', 'Resources', '/resources', NULL, NULL, 0),
|
||||
('site', 4, 'main', 'Organization', '/organization', NULL, NULL, 0),
|
||||
('site', 5, 'main', 'History', '/history', NULL, NULL, 0),
|
||||
('site', 6, 'action', 'Shop', '/shop', NULL, NULL, 0),
|
||||
('site', 7, 'action', 'Contact Us', '/organization#contact', NULL, NULL, 0),
|
||||
('site', 8, 'action', 'Give', '/give', NULL, 'filled', 1),
|
||||
('site', 9, 'footer', 'Feedback', '/feedback', NULL, NULL, 0),
|
||||
('site', 10, 'footer', 'Request', '/request', NULL, NULL, 0),
|
||||
('site', 11, 'footer', 'Contact Us', '/organization#contact', NULL, NULL, 0),
|
||||
('site', 12, 'legal', 'Privacy Policy', '/privacy', NULL, NULL, 0),
|
||||
('site', 13, 'legal', 'Terms of Service', '/terms', NULL, NULL, 0);
|
||||
|
||||
CREATE TABLE site_nav_pages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
nav_id TEXT NOT NULL REFERENCES site_nav (id) ON DELETE CASCADE,
|
||||
sort_order INTEGER NOT NULL DEFAULT 0,
|
||||
path TEXT NOT NULL, -- '/retreats', a key of site.json's pages
|
||||
title TEXT, -- null: site.json's
|
||||
description TEXT, -- null: site.json's
|
||||
is_hidden INTEGER NOT NULL DEFAULT 0 CHECK (is_hidden IN (0, 1)),
|
||||
UNIQUE (nav_id, path)
|
||||
) STRICT;
|
||||
|
||||
INSERT INTO site_nav_pages (nav_id, sort_order, path, title, description)
|
||||
SELECT 'site', sort_order, path, title, description
|
||||
FROM site_setting_pages WHERE settings_id = 'site';
|
||||
|
||||
INSERT INTO site_nav_pages (nav_id, sort_order, path, is_hidden)
|
||||
VALUES ('site', (SELECT coalesce(max(sort_order) + 1, 0) FROM site_nav_pages), '/shop', 1);
|
||||
|
||||
DROP TABLE site_setting_pages;
|
||||
|
||||
-- site_pages, with 'shop'.
|
||||
CREATE TABLE site_pages_new (
|
||||
id TEXT PRIMARY KEY
|
||||
CHECK (id IN ('community', 'retreats', 'resources', 'organization', 'history', 'shop')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
) STRICT;
|
||||
|
||||
INSERT INTO site_pages_new (id, updated_at) SELECT id, updated_at FROM site_pages;
|
||||
INSERT INTO site_pages_new (id) VALUES ('shop');
|
||||
|
||||
-- page_sections names site_pages; legacy mode leaves it alone
|
||||
-- through the rename.
|
||||
PRAGMA legacy_alter_table = ON;
|
||||
DROP TABLE site_pages;
|
||||
ALTER TABLE site_pages_new RENAME TO site_pages;
|
||||
PRAGMA legacy_alter_table = OFF;
|
||||
|
||||
CREATE TRIGGER site_nav_touch
|
||||
AFTER UPDATE ON site_nav
|
||||
FOR EACH ROW WHEN new.updated_at = old.updated_at
|
||||
BEGIN
|
||||
UPDATE site_nav SET updated_at = datetime('now') WHERE id = new.id;
|
||||
END;
|
||||
|
||||
CREATE TRIGGER site_pages_touch
|
||||
AFTER UPDATE ON site_pages
|
||||
FOR EACH ROW WHEN new.updated_at = old.updated_at
|
||||
BEGIN
|
||||
UPDATE site_pages SET updated_at = datetime('now') WHERE id = new.id;
|
||||
END;
|
||||
|
|
@ -15,7 +15,8 @@
|
|||
/site.json (SITE_CONFIG): the site's name, address, default
|
||||
description and image, icons, and each static page's title and
|
||||
description. Laid over it: Panel → Site settings (the
|
||||
site_settings table, 035), where anything set wins and anything
|
||||
site_settings table, 035) and Pages → Navigation's page titles
|
||||
(site_nav_pages, 075), where anything set wins and anything
|
||||
blank falls back to site.json. siteFor(db) does the merge. The
|
||||
pages read the same merged values (renderPage writes them into
|
||||
the HTML as window.__SITE__, see src/lib/site.ts), so a
|
||||
|
|
@ -28,6 +29,8 @@
|
|||
published, listed indexed, in the sitemap
|
||||
published, unlisted noindex: reachable by link, not by search
|
||||
unpublished, missing 404 and noindex, with the site defaults
|
||||
a hidden static page 404 and noindex below viewer, out of the
|
||||
sitemap (siteNav.js)
|
||||
/admin noindex
|
||||
|
||||
Image paths resolve like src/lib/media.ts does: a bare filename
|
||||
|
|
@ -44,6 +47,7 @@ import { fileURLToPath } from "node:url";
|
|||
import { loadBlocks, paragraphs } from "./shape.js";
|
||||
import { themeCss } from "./theme.js";
|
||||
import { driveReady } from "./googleService.js";
|
||||
import { hiddenFrom, hiddenPaths, navFor } from "./siteNav.js";
|
||||
|
||||
const here = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
|
|
@ -71,15 +75,15 @@ const origin = (site) => (site.url ?? "").replace(/\/$/, "");
|
|||
|
||||
/* site.json with the admin's Site settings laid over it: a value set
|
||||
there wins, a blank one falls back. indexing 'on' / 'off' overrides
|
||||
robots.index for this database only. Page overrides apply only to
|
||||
pages site.json knows. */
|
||||
robots.index for this database only. Page overrides (Pages →
|
||||
Navigation) apply only to pages site.json knows. */
|
||||
export function siteFor(db) {
|
||||
const base = loadSite();
|
||||
const row = db.prepare(`SELECT * FROM site_settings WHERE id = 'site'`).get() ?? {};
|
||||
|
||||
const pages = { ...(base.pages ?? {}) };
|
||||
const overrides = db
|
||||
.prepare(`SELECT path, title, description FROM site_setting_pages WHERE settings_id = 'site'`)
|
||||
.prepare(`SELECT path, title, description FROM site_nav_pages WHERE nav_id = 'site'`)
|
||||
.all();
|
||||
for (const page of overrides) {
|
||||
const known = pages[page.path];
|
||||
|
|
@ -112,13 +116,15 @@ export function siteFor(db) {
|
|||
};
|
||||
}
|
||||
|
||||
/* What the browser app reads for headings and titles. */
|
||||
// requestFiles: whether the Request form can take files (Drive set up).
|
||||
const forBrowser = (site) => ({
|
||||
/* What the browser app reads for headings, titles and its menus.
|
||||
requestFiles: whether the Request form can take files (Drive set
|
||||
up). nav: this visitor's links and the hidden pages (siteNav.js). */
|
||||
const forBrowser = (site, nav) => ({
|
||||
title: site.title,
|
||||
pages: site.pages,
|
||||
privacyEmail: site.privacyEmail,
|
||||
requestFiles: driveReady(),
|
||||
nav,
|
||||
});
|
||||
|
||||
/* robots.txt for this database's setting, as the build writes it. */
|
||||
|
|
@ -358,12 +364,13 @@ function blogMeta(site, db, id, meta) {
|
|||
};
|
||||
}
|
||||
|
||||
/* The metadata for one request path. */
|
||||
export function metaFor(db, rawPath) {
|
||||
/* The metadata for one request path, for this visitor (the signed-in
|
||||
user or null, who decides whether a hidden page is there). */
|
||||
export function metaFor(db, rawPath, user = null) {
|
||||
const site = siteFor(db);
|
||||
// Canonical form: no trailing slash (but "/" itself), no query.
|
||||
const pathname = rawPath.split(/[?#]/)[0].replace(/(.)\/+$/, "$1") || "/";
|
||||
const meta = defaults(site, db, pathname);
|
||||
const meta = { ...defaults(site, db, pathname), nav: navFor(db, user) };
|
||||
|
||||
if (pathname === "/") {
|
||||
// The home page's data, and its first hero photo when the hero is
|
||||
|
|
@ -396,6 +403,7 @@ export function metaFor(db, rawPath) {
|
|||
if (/^\/admin(\/|$)/.test(pathname)) return { ...meta, title: `Admin | ${site.title}`, noindex: true };
|
||||
|
||||
const page = site.pages?.[pathname];
|
||||
if (page && hiddenFrom(db, user, pathname)) return notFound(meta);
|
||||
if (page) {
|
||||
return { ...meta, title: titled(site, page.title), description: plainText(page.description) ?? meta.description };
|
||||
}
|
||||
|
|
@ -520,7 +528,7 @@ export function renderPage(html, meta) {
|
|||
),
|
||||
// Read by src/lib/site.ts before the app's module runs (a plain
|
||||
// script in the head runs during parsing; modules wait).
|
||||
`<script>window.__SITE__ = ${scriptJson(forBrowser(site))}</script>`,
|
||||
`<script>window.__SITE__ = ${scriptJson(forBrowser(site, meta.nav))}</script>`,
|
||||
site.themeCss && `<style id="site-colors">${site.themeCss}</style>`,
|
||||
analyticsTag(site),
|
||||
].filter(Boolean);
|
||||
|
|
@ -542,10 +550,13 @@ export function sitemap(db) {
|
|||
const site = siteFor(db);
|
||||
const base = origin(site);
|
||||
const day = (value) => (value ? String(value).slice(0, 10) : null);
|
||||
const hidden = hiddenPaths(db);
|
||||
|
||||
const entries = [
|
||||
{ loc: "/" },
|
||||
...Object.keys(site.pages ?? {}).map((loc) => ({ loc })),
|
||||
...Object.keys(site.pages ?? {})
|
||||
.filter((loc) => !hidden.has(loc))
|
||||
.map((loc) => ({ loc })),
|
||||
...db
|
||||
.prepare(`SELECT id, updated_at FROM events WHERE is_published = 1 AND is_listed = 1`)
|
||||
.all()
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@
|
|||
can say no while production says yes
|
||||
GET /anything the built index.html, its title, description,
|
||||
preview image, canonical link and structured
|
||||
data filled in for that path (pageMeta.js)
|
||||
data filled in for that path (pageMeta.js),
|
||||
and this visitor's menus (siteNav.js)
|
||||
|
||||
In production nginx serves files that exist (the JS, CSS,
|
||||
uploads) itself and sends everything else here, so a shared link
|
||||
|
|
@ -35,6 +36,7 @@ import { fileURLToPath } from "node:url";
|
|||
|
||||
import { Hono } from "hono";
|
||||
|
||||
import { currentUser } from "../auth.js";
|
||||
import { metaFor, renderPage, robotsTxt, siteFor, sitemap } from "../pageMeta.js";
|
||||
import { CACHE, NO_STORE } from "../shape.js";
|
||||
|
||||
|
|
@ -109,7 +111,8 @@ pages.get("*", (c, next) => {
|
|||
let meta;
|
||||
let page;
|
||||
try {
|
||||
meta = metaFor(c.get("db"), pathname);
|
||||
// Who's asking decides whether a hidden page is there (siteNav.js).
|
||||
meta = metaFor(c.get("db"), pathname, currentUser(c.get("db"), c));
|
||||
page = renderPage(html, meta);
|
||||
} catch (err) {
|
||||
warnOnce(
|
||||
|
|
|
|||
|
|
@ -1,11 +1,15 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
SITE PAGE ROUTES: read-only, mounted under /api
|
||||
|
||||
GET /nav this visitor's header and footer links and
|
||||
the hidden pages (siteNav.js); not cached,
|
||||
since it depends on who's signed in
|
||||
GET /site-pages every site page's section headings, for
|
||||
the header's dropdowns and the side nav
|
||||
GET /site-pages/:id one page's sections, in order: heading,
|
||||
accent, preset (and an events preset's
|
||||
filter and view), and its blocks
|
||||
filter and view), and its blocks; Not
|
||||
found for a hidden page, below viewer
|
||||
|
||||
Edited in the admin as Pages → each page
|
||||
(admin-schema/sitePages.js). Blocks are shaped as on any record
|
||||
|
|
@ -13,10 +17,17 @@
|
|||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { Hono } from "hono";
|
||||
import { json, loadBlocks } from "../shape.js";
|
||||
import { currentUser } from "../auth.js";
|
||||
import { hiddenPaths, navFor, seesHidden } from "../siteNav.js";
|
||||
import { NO_STORE, json, loadBlocks } from "../shape.js";
|
||||
|
||||
const sitePages = new Hono();
|
||||
|
||||
sitePages.get("/nav", (c) => {
|
||||
const db = c.get("db");
|
||||
return c.json({ nav: navFor(db, currentUser(db, c)) }, 200, NO_STORE);
|
||||
});
|
||||
|
||||
sitePages.get("/site-pages", (c) => {
|
||||
const rows = c
|
||||
.get("db")
|
||||
|
|
@ -31,7 +42,8 @@ sitePages.get("/site-pages", (c) => {
|
|||
sitePages.get("/site-pages/:id", (c) => {
|
||||
const db = c.get("db");
|
||||
const id = c.req.param("id");
|
||||
if (!db.prepare(`SELECT 1 FROM site_pages WHERE id = ?`).get(id)) {
|
||||
const hidden = hiddenPaths(db).has(`/${id}`);
|
||||
if (!db.prepare(`SELECT 1 FROM site_pages WHERE id = ?`).get(id) || (hidden && !seesHidden(currentUser(db, c)))) {
|
||||
return c.json({ error: "No such page." }, 404);
|
||||
}
|
||||
|
||||
|
|
@ -53,7 +65,9 @@ sitePages.get("/site-pages/:id", (c) => {
|
|||
picked.set(section_id, [...(picked.get(section_id) ?? []), org_id]);
|
||||
}
|
||||
|
||||
return json(c, {
|
||||
// A hidden page is only for those signed in: never kept.
|
||||
const send = hidden ? (body) => c.json(body, 200, NO_STORE) : (body) => json(c, body);
|
||||
return send({
|
||||
page: {
|
||||
id,
|
||||
sections: rows.map(({ id: sectionId, ...section }) => ({
|
||||
|
|
|
|||
65
server/src/siteNav.js
Normal file
65
server/src/siteNav.js
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
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 };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue