Drafts of every record, previewed on the real page

Migration 077 keeps one draft per record of any entity. Saving a draft
checks it by applying it in a transaction that is rolled back; Preview
opens the record's page in a tab that sends X-Draft-Preview, and the
server answers those reads from a second connection holding the applied
draft, then rolls back, so nobody else sees it. Publish is the ordinary
save, which drops the draft. tx() now nests as a savepoint.

Also commits TODO.md: the answered questions, and parts 1 to 3 moved to
Done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aidan Dona 2026-10-16 18:21:53 -05:00
parent e4c7e165d1
commit a6c66d4607
13 changed files with 576 additions and 65 deletions

View file

@ -18,6 +18,7 @@
and never fills in reads as real input instead of being skipped.
═══════════════════════════════════════════════════════════════ */
import { awardHref, blogHref, eventHref, orgHref, personHref, teamHref } from "./hrefs.ts";
import { organizations } from "./adminSchema/organizations.ts";
import { events } from "./adminSchema/events.ts";
import { people } from "./adminSchema/people.ts";
@ -64,6 +65,31 @@ export const ADMIN_ENTITIES: Record<string, any> = {
site_settings: siteSettings,
};
/* Where each entity's record shows on the site, for its draft's
Preview (EntityEdit.tsx opens it with ?preview=, which api.ts sends
on as X-Draft-Preview; server/src/drafts.js). An entity without
one has drafts but nothing to preview them on. */
export const PREVIEW_PATHS: Record<string, (row: any) => string | null> = {
organizations: (row) => (row.kind === "ministry" ? null : orgHref(row.id, row.kind)),
events: (row) => eventHref(row.id),
people: (row) => personHref(row.id),
person_tag_types: (row) => `/directory/${encodeURIComponent(row.id)}`,
teams: (row) => teamHref(row.id),
awards: (row) => awardHref(row.id),
timeline: () => "/history",
front_page: () => "/",
blog_posts: (row) => blogHref(row.id),
community_page: () => "/community",
retreats_page: () => "/retreats",
resources_page: () => "/resources",
organization_page: () => "/organization",
history_page: () => "/history",
shop_page: () => "/shop",
// The menus are on every page.
navigation: () => "/",
site_settings: () => "/",
};
/* The /admin/options request for the option lists `part` of a
manifest names (any `optionsFrom` inside it, at any depth), so a
form gets only the lists its selects use. An empty request would

View file

@ -21,6 +21,36 @@ import { visionColor } from "./color.ts";
const BASE = import.meta.env?.VITE_API_BASE ?? "/api";
const DEFAULT_TTL = 60_000;
/* A draft's preview (server/src/drafts.js): a tab opened from the
editor's Preview with ?preview=<entity>/<id> sends it with every
read of the site's data, for as long as the tab lasts, and the
server answers with that draft applied (to a signed-in viewer and
up; anyone else gets the live site). Never for the admin's own
reads. */
const PREVIEW_KEY = "site-draft-preview";
export const draftPreview: string | null = (() => {
if (typeof window === "undefined") return null;
try {
const asked = new URLSearchParams(window.location.search).get("preview");
if (asked) sessionStorage.setItem(PREVIEW_KEY, asked);
return asked ?? sessionStorage.getItem(PREVIEW_KEY);
} catch {
return null;
}
})();
export function stopPreview() {
try {
sessionStorage.removeItem(PREVIEW_KEY);
} catch {
// Nothing kept to clear.
}
const url = new URL(window.location.href);
url.searchParams.delete("preview");
window.location.replace(url.toString());
}
const cache = new Map(); // path → { at, promise }
const settled = new Map(); // path → the last body it answered with
@ -44,9 +74,11 @@ const NOT_THE_API: Record<number, string> = {
};
async function request(path, options: RequestInit = {}) {
const preview: Record<string, string> =
draftPreview && !String(path).startsWith("/admin") ? { "X-Draft-Preview": draftPreview } : {};
const response = await fetch(`${BASE}${path}`, {
...options,
headers: { Accept: "application/json", ...options.headers },
headers: { Accept: "application/json", ...preview, ...(options.headers as Record<string, string>) },
});
if (response.status === 204) return null;
@ -151,6 +183,14 @@ export function patch(path, data) {
});
}
export function put(path, data) {
return request(path, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
}
export function del(path) {
return request(path, { method: "DELETE" });
}