Page management, step 1: every page in a registry, new pages, hidden and disabled
Migration 080 makes site_pages the list of every page: address, title, intro, Hidden, Disabled (out of menus, sitemap and search) and card order. site.json's pages and Navigation's page list move into it. Pages -> Page management (one editor in place of six) makes new pages of sections at any free address; the admin home opens Pages with a banner and a card per page. A catch-all route draws whatever page is at an address; the fixed pages take their words from the registry and can't be deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
56acedf926
commit
1342691f49
36 changed files with 584 additions and 405 deletions
|
|
@ -411,6 +411,10 @@ export function deleteRow(db, entity, rawId) {
|
|||
}
|
||||
|
||||
const id = normalizeId(entity, rawId);
|
||||
// Records the site can't do without (the pages it draws in code).
|
||||
if (entity.undeletable?.includes(id)) {
|
||||
throw new HttpError(405, "The site needs this one: it can be hidden or disabled, not deleted.");
|
||||
}
|
||||
const result = wrapDbErrors(() =>
|
||||
db.prepare(`DELETE FROM ${entity.table} WHERE ${entity.idColumn} = ?`).run(id),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@
|
|||
on an extension (an organization's contacts)
|
||||
singleton the one id this entity ever has; the engine
|
||||
refuses create and delete (see front_page)
|
||||
undeletable ids the engine refuses to delete (the pages the
|
||||
site draws in code)
|
||||
check (db, id) => void, run after a save has written
|
||||
everything, inside its transaction: throw an
|
||||
HttpError to refuse it (site settings' colors)
|
||||
|
|
@ -59,14 +61,7 @@ import { personTagTypes } from "./admin-schema/personTags.js";
|
|||
import { tags } from "./admin-schema/tags.js";
|
||||
import { contactTypes } from "./admin-schema/contactTypes.js";
|
||||
import { redirects } from "./admin-schema/redirects.js";
|
||||
import {
|
||||
communityPage,
|
||||
retreatsPage,
|
||||
resourcesPage,
|
||||
organizationPage,
|
||||
historyPage,
|
||||
shopPage,
|
||||
} from "./admin-schema/sitePages.js";
|
||||
import { pages } from "./admin-schema/sitePages.js";
|
||||
|
||||
export const ENTITIES = {
|
||||
organizations,
|
||||
|
|
@ -81,12 +76,7 @@ export const ENTITIES = {
|
|||
timeline,
|
||||
front_page: frontPage,
|
||||
blog_posts: blogPosts,
|
||||
community_page: communityPage,
|
||||
retreats_page: retreatsPage,
|
||||
resources_page: resourcesPage,
|
||||
organization_page: organizationPage,
|
||||
history_page: historyPage,
|
||||
shop_page: shopPage,
|
||||
pages,
|
||||
navigation: siteNav,
|
||||
site_settings: siteSettings,
|
||||
newsletters,
|
||||
|
|
@ -116,6 +106,8 @@ export const OPTION_QUERIES = {
|
|||
// Link fields with `redirects: true` pick from these (068).
|
||||
redirects: "SELECT id, id || ' (' || target_url || ')' AS label FROM redirects ORDER BY id",
|
||||
blog_posts: "SELECT id, title AS label FROM blog_posts ORDER BY published_on DESC, title",
|
||||
// Navigation's Under page: a page by its address (080).
|
||||
site_pages: "SELECT path AS id, title || ' (' || path || ')' AS label FROM site_pages ORDER BY sort_order, title",
|
||||
|
||||
// org_id rides along so the affiliation row can filter the list
|
||||
// down to teams of the organization it already names.
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
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. */
|
||||
Pages → Navigation: the header's and footer's links, in order
|
||||
(siteNav.js reads them for the site; the pages themselves are
|
||||
Page management's, sitePages.js). Admins and up. */
|
||||
export const siteNav = {
|
||||
key: "navigation",
|
||||
table: "site_nav",
|
||||
|
|
@ -33,12 +32,5 @@ export const siteNav = {
|
|||
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")],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,18 +1,19 @@
|
|||
import { text, url, int, enumeration, featuredCardsChild, blocksChild } from "./columns.js";
|
||||
import { HttpError } from "../admin-crud.js";
|
||||
import { text, url, int, bool, enumeration, featuredCardsChild, blocksChild } from "./columns.js";
|
||||
|
||||
/* ── The site's own pages (056_site_pages.sql) ───────────────────
|
||||
/* ── The site's pages (056_site_pages.sql, 080_page_registry.sql) ──
|
||||
|
||||
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
|
||||
Pages → Page management: every page of the site, one site_pages
|
||||
row each (its address, title, intro, Hidden and Disabled), 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
|
||||
either its blocks or a preset, a prebuilt part of the site
|
||||
(SitePage.tsx draws both).
|
||||
(SitePage.tsx draws both). New pages are made here.
|
||||
|
||||
Community also carries the blog section's settings: the
|
||||
community_page row (same id) as an extension, and its pinned
|
||||
posts, cards and feeds, all owned by that id. */
|
||||
posts, cards and feeds, all owned by that id; only for the page
|
||||
whose id is community. */
|
||||
|
||||
const sectionsChild = {
|
||||
key: "sections",
|
||||
|
|
@ -60,27 +61,62 @@ const sectionsChild = {
|
|||
],
|
||||
};
|
||||
|
||||
const sitePage = (id, more = {}) => ({
|
||||
key: `${id}_page`,
|
||||
|
||||
/* Pages the site draws in code (src/pages/fixedPages.ts): they can be
|
||||
renamed, moved, hidden or disabled, never deleted. */
|
||||
export const FIXED_PAGES = ["home", "give", "feedback", "request", "qr", "privacy", "terms"];
|
||||
|
||||
/* Addresses a page can't take: the admin, the API, the uploads, the
|
||||
records' own pages and the account pages. */
|
||||
const RESERVED = [
|
||||
"admin", "api", "uploads", "assets", "pa", "go",
|
||||
"events", "regions", "groups", "partners", "organizations", "teams", "awards", "people", "blog", "directory",
|
||||
"sign-in", "account", "sitemap.xml", "robots.txt",
|
||||
];
|
||||
|
||||
/* After a save: the address is one a page may have. */
|
||||
function checkPage(db, id) {
|
||||
const page = db.prepare(`SELECT path FROM site_pages WHERE id = ?`).get(id);
|
||||
const first = page?.path.split("/")[1];
|
||||
if (first && RESERVED.includes(first)) {
|
||||
throw new HttpError(422, "Validation failed", { path: `/${first} belongs to another part of the site. Pick another address.` });
|
||||
}
|
||||
if (id === "home" && page.path !== "/") throw new HttpError(422, "Validation failed", { path: "The front page stays at /." });
|
||||
if (id !== "home" && page.path === "/") throw new HttpError(422, "Validation failed", { path: "/ is the front page." });
|
||||
}
|
||||
|
||||
const isCommunity = { column: "id", value: "community" };
|
||||
|
||||
export const pages = {
|
||||
key: "pages",
|
||||
table: "site_pages",
|
||||
idColumn: "id",
|
||||
idKind: "slug",
|
||||
singleton: id,
|
||||
concurrency: "updated_at",
|
||||
undeletable: FIXED_PAGES,
|
||||
|
||||
list: { columns: ["id", "updated_at"], filters: [], search: [], order: "id" },
|
||||
list: {
|
||||
columns: ["title", "path", "is_hidden", "is_disabled", "updated_at"],
|
||||
filters: [],
|
||||
search: ["title", "path"],
|
||||
order: "sort_order, title",
|
||||
},
|
||||
|
||||
columns: [],
|
||||
...more,
|
||||
children: [sectionsChild, ...(more.children ?? [])],
|
||||
});
|
||||
columns: [
|
||||
text("path", { required: true }),
|
||||
text("title", { required: true }),
|
||||
text("description"),
|
||||
bool("is_hidden"),
|
||||
bool("is_disabled"),
|
||||
int("sort_order"),
|
||||
],
|
||||
|
||||
export const communityPage = sitePage("community", {
|
||||
extensions: [
|
||||
{
|
||||
key: "blog",
|
||||
table: "community_page",
|
||||
idColumn: "id",
|
||||
when: isCommunity,
|
||||
columns: [
|
||||
text("internal_title"),
|
||||
int("internal_slots"),
|
||||
|
|
@ -91,27 +127,27 @@ export const communityPage = sitePage("community", {
|
|||
],
|
||||
},
|
||||
],
|
||||
|
||||
children: [
|
||||
sectionsChild,
|
||||
{
|
||||
key: "pinned_posts",
|
||||
table: "community_page_pinned_posts",
|
||||
owner: { column: "page_id" },
|
||||
order: "sort_order",
|
||||
when: isCommunity,
|
||||
columns: [text("post_id", { required: true })],
|
||||
},
|
||||
featuredCardsChild("community_page"),
|
||||
{ ...featuredCardsChild("community_page"), when: isCommunity },
|
||||
{
|
||||
key: "feeds",
|
||||
table: "community_page_feeds",
|
||||
owner: { column: "page_id" },
|
||||
order: "sort_order",
|
||||
when: isCommunity,
|
||||
columns: [url("url", { required: true }), text("label")],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
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");
|
||||
check: checkPage,
|
||||
};
|
||||
|
|
|
|||
93
server/src/migrations/080_page_registry.sql
Normal file
93
server/src/migrations/080_page_registry.sql
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
-- ═══════════════════════════════════════════════════════════════
|
||||
-- 080 PAGE REGISTRY
|
||||
--
|
||||
-- Every page of the site is a site_pages row: its address, title
|
||||
-- and intro (the page's heading and its search text), and two
|
||||
-- switches, all edited in Pages → Page management, where new pages
|
||||
-- are made too. site.json's "pages" and Navigation's page list
|
||||
-- (site_nav_pages, 075) both move here and go: this is the one
|
||||
-- place a page's words live.
|
||||
--
|
||||
-- path '/retreats'; a new page's own, eg. '/library'.
|
||||
-- Unique; '/' is the front page.
|
||||
-- is_hidden Not found to everyone below viewer, out of the
|
||||
-- menus and sitemap (siteNav.js)
|
||||
-- is_disabled out of the menus, the sitemap and search; the
|
||||
-- address still works
|
||||
-- sort_order the order of the admin home's page cards
|
||||
--
|
||||
-- A page is its sections (page_sections), except the few the site
|
||||
-- still draws in code (src/pages/fixedPages.ts): the front page,
|
||||
-- Feedback, Request, Give, QR codes, Privacy and Terms. Those take
|
||||
-- their title and intro from here all the same, and can't be
|
||||
-- deleted (admin-schema/pages.js).
|
||||
--
|
||||
-- The id stays a slug, as before (page sections, the Community
|
||||
-- blog's settings and the drafts key on it), so an existing page
|
||||
-- keeps its id; its address can change.
|
||||
-- ═══════════════════════════════════════════════════════════════
|
||||
|
||||
CREATE TABLE site_pages_new (
|
||||
id TEXT PRIMARY KEY
|
||||
CONSTRAINT "A page's URL ID can use only lowercase letters, numbers and hyphens."
|
||||
CHECK (id <> '' AND id NOT GLOB '*[^a-z0-9-]*'),
|
||||
path TEXT NOT NULL UNIQUE
|
||||
CONSTRAINT "A page's address starts with / and uses only lowercase letters, numbers, hyphens and /, eg. /library."
|
||||
CHECK (path = '/' OR (path GLOB '/[a-z0-9]*' AND path NOT GLOB '*[^a-z0-9/-]*' AND path NOT GLOB '*/')),
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
is_hidden INTEGER NOT NULL DEFAULT 0 CHECK (is_hidden IN (0, 1)),
|
||||
is_disabled INTEGER NOT NULL DEFAULT 0 CHECK (is_disabled IN (0, 1)),
|
||||
sort_order INTEGER NOT NULL DEFAULT 0,
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
) STRICT;
|
||||
|
||||
INSERT INTO site_pages_new (id, path, title, description, sort_order) VALUES
|
||||
('home', '/', 'Home', NULL, 0),
|
||||
('retreats', '/retreats', 'Retreats', 'Retreats hosted by Next Generation of Unity, our regions, and our partners throughout the year.', 1),
|
||||
('community', '/community', 'Community', 'Groups, regions and the people who make up Next Generation of Unity.', 2),
|
||||
('resources', '/resources', 'Resources', 'Teachings, brand assets, and material for groups and partners.', 3),
|
||||
('organization', '/organization', 'Organization', 'Our mission, vision and values, the people who lead Next Generation of Unity, and how to reach us.', 4),
|
||||
('history', '/history', 'History', 'A near-complete timeline of Next Generation of Unity (NGU), and other Unity Young Adult programs.', 5),
|
||||
('shop', '/shop', 'Shop', 'Shirts, stickers and more from Next Generation of Unity. Every purchase supports our retreats and programs.', 6),
|
||||
('give', '/give', 'Giving', 'Support the work of Next Generation of Unity.', 7),
|
||||
('feedback', '/feedback', 'Feedback', 'Tell us what''s working, what isn''t, and what you''d like to see next.', 8),
|
||||
('request', '/request', 'Request', 'Ask for a blog post, an event, or a new or updated group, region, partner, team, award or person on the site.', 9),
|
||||
('qr', '/qr', 'QR Codes', 'Codes for flyers, slides and signs, pointing at any page on the site or any link, in your choice of colors.', 10),
|
||||
('privacy', '/privacy', 'Privacy Policy', 'How Next Generation of Unity collects, uses, and protects your information.', 11),
|
||||
('terms', '/terms', 'Terms of Service', 'The terms that apply when you use this site and our programs.', 12);
|
||||
|
||||
-- Each page's sections' save time comes along.
|
||||
UPDATE site_pages_new
|
||||
SET updated_at = (SELECT updated_at FROM site_pages WHERE site_pages.id = site_pages_new.id)
|
||||
WHERE id IN (SELECT id FROM site_pages);
|
||||
|
||||
-- What Navigation (or before it Site settings) changed.
|
||||
UPDATE site_pages_new
|
||||
SET title = COALESCE((SELECT n.title FROM site_nav_pages n WHERE n.path = site_pages_new.path), title),
|
||||
description = COALESCE((SELECT n.description FROM site_nav_pages n WHERE n.path = site_pages_new.path), description),
|
||||
is_hidden = COALESCE((SELECT n.is_hidden FROM site_nav_pages n WHERE n.path = site_pages_new.path), is_hidden);
|
||||
|
||||
DROP TABLE site_nav_pages;
|
||||
|
||||
-- The admin edits every page as one entity now ('pages'), so a
|
||||
-- page's draft (077) moves under it.
|
||||
UPDATE drafts
|
||||
SET entity = 'pages', record_id = substr(entity, 1, length(entity) - 5)
|
||||
WHERE entity IN ('community_page', 'retreats_page', 'resources_page', 'organization_page', 'history_page', 'shop_page');
|
||||
|
||||
-- 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 INDEX site_pages_order_idx ON site_pages (sort_order);
|
||||
|
||||
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;
|
||||
|
|
@ -14,10 +14,11 @@
|
|||
── Where it comes from ──
|
||||
/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) 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
|
||||
default description and image, and icons. Laid over it: Panel →
|
||||
Site settings (the site_settings table, 035), where anything set
|
||||
wins and anything blank falls back to site.json. siteFor(db) does
|
||||
the merge. Each page's title and intro are its site_pages row's
|
||||
(Pages → Page management, 080). The
|
||||
pages read the same merged values (renderPage writes them into
|
||||
the HTML as window.__SITE__, see src/lib/site.ts), so a
|
||||
heading and its search title can't drift.
|
||||
|
|
@ -47,7 +48,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";
|
||||
import { allPages, hiddenFrom, navFor, pageAt } from "./siteNav.js";
|
||||
|
||||
const here = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
|
|
@ -75,24 +76,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 (Pages →
|
||||
Navigation) apply only to pages site.json knows. */
|
||||
robots.index for this database only. pages: every page's title
|
||||
and intro, by path (site_pages). */
|
||||
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_nav_pages WHERE nav_id = 'site'`)
|
||||
.all();
|
||||
for (const page of overrides) {
|
||||
const known = pages[page.path];
|
||||
if (!known) continue;
|
||||
pages[page.path] = {
|
||||
title: page.title || known.title,
|
||||
description: page.description || known.description,
|
||||
};
|
||||
}
|
||||
const pages = Object.fromEntries(
|
||||
allPages(db).map((page) => [page.path, { title: page.title, description: page.description ?? "" }]),
|
||||
);
|
||||
|
||||
return {
|
||||
...base,
|
||||
|
|
@ -410,10 +402,17 @@ export function metaFor(db, rawPath, user = null) {
|
|||
// in particular, so never indexed or in the sitemap.
|
||||
if (ACCOUNT_PAGES[pathname]) return { ...meta, title: titled(site, ACCOUNT_PAGES[pathname]), noindex: true };
|
||||
|
||||
const page = site.pages?.[pathname];
|
||||
// A page of the site (site_pages): hidden is Not found below
|
||||
// viewer; disabled stays out of search.
|
||||
const page = pageAt(db, 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 };
|
||||
return {
|
||||
...meta,
|
||||
title: titled(site, page.title),
|
||||
description: plainText(page.description) ?? meta.description,
|
||||
noindex: page.is_disabled === 1,
|
||||
};
|
||||
}
|
||||
|
||||
// An event's schedule presentation (SchedulePresent.tsx): the
|
||||
|
|
@ -558,13 +557,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 ?? {})
|
||||
.filter((loc) => !hidden.has(loc))
|
||||
.map((loc) => ({ loc })),
|
||||
// Pages that are neither hidden nor disabled.
|
||||
...allPages(db)
|
||||
.filter((page) => page.path !== "/" && page.is_hidden !== 1 && page.is_disabled !== 1)
|
||||
.map((page) => ({ loc: page.path })),
|
||||
...db
|
||||
.prepare(`SELECT id, updated_at FROM events WHERE is_published = 1 AND is_listed = 1`)
|
||||
.all()
|
||||
|
|
|
|||
|
|
@ -4,9 +4,11 @@
|
|||
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,
|
||||
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
|
||||
sections, in order: heading,
|
||||
accent, preset (and an events preset's
|
||||
filter and view), and its blocks; Not
|
||||
found for a hidden page, below viewer
|
||||
|
|
@ -18,7 +20,7 @@
|
|||
|
||||
import { Hono } from "hono";
|
||||
import { currentUser } from "../auth.js";
|
||||
import { hiddenPaths, navFor, seesHidden } from "../siteNav.js";
|
||||
import { navFor, seesHidden } from "../siteNav.js";
|
||||
import { NO_STORE, json, loadBlocks } from "../shape.js";
|
||||
|
||||
const sitePages = new Hono();
|
||||
|
|
@ -31,19 +33,23 @@ sitePages.get("/nav", (c) => {
|
|||
sitePages.get("/site-pages", (c) => {
|
||||
const rows = c
|
||||
.get("db")
|
||||
.prepare(`SELECT page_id, anchor, title FROM page_sections ORDER BY page_id, sort_order, id`)
|
||||
.prepare(
|
||||
`SELECT p.path, s.anchor, s.title FROM page_sections s JOIN site_pages p ON p.id = s.page_id
|
||||
ORDER BY p.path, s.sort_order, s.id`,
|
||||
)
|
||||
.all();
|
||||
|
||||
const pages = {};
|
||||
for (const row of rows) (pages[row.page_id] ??= []).push({ anchor: row.anchor, title: row.title });
|
||||
for (const row of rows) (pages[row.path] ??= []).push({ anchor: row.anchor, title: row.title });
|
||||
return json(c, { pages });
|
||||
});
|
||||
|
||||
sitePages.get("/site-pages/:id", (c) => {
|
||||
const db = c.get("db");
|
||||
const id = c.req.param("id");
|
||||
const hidden = hiddenPaths(db).has(`/${id}`);
|
||||
if (!db.prepare(`SELECT 1 FROM site_pages WHERE id = ?`).get(id) || (hidden && !seesHidden(currentUser(db, c)))) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
@ -81,6 +87,8 @@ sitePages.get("/site-pages/:id", (c) => {
|
|||
return send({
|
||||
page: {
|
||||
id,
|
||||
title: found.title,
|
||||
description: found.description,
|
||||
sections: rows.map(({ id: sectionId, ...section }) => ({
|
||||
...section,
|
||||
blocks: blocks.get(String(sectionId)) ?? [],
|
||||
|
|
|
|||
|
|
@ -1,16 +1,21 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
SITE NAVIGATION
|
||||
SITE NAVIGATION AND PAGES
|
||||
|
||||
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).
|
||||
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).
|
||||
|
||||
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.
|
||||
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";
|
||||
|
|
@ -20,38 +25,63 @@ 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_nav_pages WHERE nav_id = 'site' AND is_hidden = 1`)
|
||||
.prepare(`SELECT path FROM site_pages WHERE 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). */
|
||||
/* 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 hidden = hiddenPaths(db);
|
||||
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) => sees || !hidden.has(pageOf(link.path)))
|
||||
.filter((link) => {
|
||||
const page = pageOf(link.path);
|
||||
return !disabled.has(page) && (sees || !hidden.has(page));
|
||||
})
|
||||
.map((link) => ({
|
||||
place: link.place,
|
||||
label: link.label,
|
||||
|
|
@ -61,5 +91,17 @@ export function navFor(db, user) {
|
|||
newTab: link.new_tab === 1,
|
||||
}));
|
||||
|
||||
return { links, hidden: [...hidden], seesHidden: sees };
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue