NGU-Web/server/src/routes/sitePages.js
Aidan Dona f1a0b06ac2 Page management, step 3: section types with their own settings; the front page as sections
Migration 083 gives the section types that need more than blocks their
own tables (hero, slides, countdown, stats, paths, featured cards, the
Community blog's slots, pinned posts and feeds) and turns the front page
into Home's sections: the hero, then its bands in order. Any section can
be hidden. The Front page editor, its tables and the Community page's
are gone; /api/front-page and /api/community-blog fold into the page's
sections (sectionData.js). The admin engine reads and writes a child
collection's own side tables and gated nested collections.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 00:34:56 -05:00

107 lines
4.2 KiB
JavaScript

/* ═══════════════════════════════════════════════════════════════
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 page's section headings, by the
page's path, for the header's dropdowns
and the side nav
GET /site-pages/:id one page's title and intro and its shown
sections, in order, each with what its type
shows (sectionData.js): heading,
accent, preset (and an events preset's
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
page (loadBlocks), so ContentBlocks draws them.
═══════════════════════════════════════════════════════════════ */
import { Hono } from "hono";
import { currentUser } from "../auth.js";
import { navFor, seesHidden } from "../siteNav.js";
import { NO_STORE, json, loadBlocks } from "../shape.js";
import { withSectionData } from "../sectionData.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")
.prepare(
`SELECT p.path, s.anchor, s.title, s.preset FROM page_sections s JOIN site_pages p ON p.id = s.page_id
WHERE s.is_hidden = 0
ORDER BY p.path, s.sort_order, s.id`,
)
.all();
const pages = {};
for (const row of rows) (pages[row.path] ??= []).push({ anchor: row.anchor, title: row.title, preset: row.preset });
return json(c, { pages });
});
sitePages.get("/site-pages/:id", async (c) => {
const db = c.get("db");
const id = c.req.param("id");
const found = db.prepare(`SELECT title, description, is_hidden FROM site_pages WHERE id = ?`).get(id);
const hidden = found?.is_hidden === 1;
if (!found || (hidden && !seesHidden(currentUser(db, c)))) {
return c.json({ error: "No such page." }, 404);
}
const rows = db
.prepare(
`SELECT id, anchor, title, blurb, accent, preset, scope_id, event_type, view, org_kinds
FROM page_sections WHERE page_id = ? AND is_hidden = 0 ORDER BY sort_order, id`,
)
.all(id);
const blocks = loadBlocks(db, "page_section", rows.map((row) => String(row.id)));
const picked = new Map();
for (const { section_id, org_id } of db
.prepare(
`SELECT o.section_id, o.org_id FROM page_section_orgs o
JOIN page_sections s ON s.id = o.section_id
WHERE s.page_id = ? ORDER BY o.section_id, o.sort_order, o.id`,
)
.all(id)) {
picked.set(section_id, [...(picked.get(section_id) ?? []), org_id]);
}
// A People directory's tags (078), in order.
const tags = new Map();
for (const { section_id, tag_id } of db
.prepare(
`SELECT t.section_id, t.tag_id FROM page_section_tags t
JOIN page_sections s ON s.id = t.section_id
WHERE s.page_id = ? ORDER BY t.section_id, t.sort_order, t.id`,
)
.all(id)) {
tags.set(section_id, [...(tags.get(section_id) ?? []), tag_id]);
}
const sections = await withSectionData(db, rows);
// 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,
title: found.title,
description: found.description,
sections: sections.map(({ id: sectionId, ...section }) => ({
...section,
blocks: blocks.get(String(sectionId)) ?? [],
org_ids: picked.get(sectionId) ?? [],
tag_ids: tags.get(sectionId) ?? [],
})),
},
});
});
export default sitePages;