Sharing a chart with its short summary, with link previews
CI / test (push) Successful in 1m10s
CI / build (push) Successful in 4m33s
CI / deploy (push) Skipped

The summary card gets a share button: a public link (/s/<slug>) with the
wheel and the newest short summary, no name or birth data. The page carries
og:/twitter: tags, and /s/<slug>/kep/<version>.png renders a 1200x630 preview
(wheel, title, text, big three) with next/og and bundled fonts. The version
changes with the summary, so Facebook fetches a rewritten one again.

Backend: a shared chart comes with its newest finished summary, and sharing
the same thing again returns the open link.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-19 07:37:06 +00:00
co-authored by Claude Opus 5.5
parent da533e8fa9
commit c7600608d3
21 changed files with 1001 additions and 0 deletions
+81
View File
@@ -0,0 +1,81 @@
// Public share links (`/s/<slug>`): loading the shared content and the
// texts of the page, its link preview and its preview image.
import "server-only";
import { headers } from "next/headers";
import { cache } from "react";
import { BACKEND_URL } from "./server";
import type { ChartResult } from "./types";
export interface PublicSummary {
id: string;
language: string;
title: string | null;
summary: string | null;
completed_at: string | null;
}
/** `GET /v1/public/shares/{slug}` of a shared chart. */
export interface PublicShare {
kind: "chart" | "synastry" | "analysis";
title: string | null;
created_at: string;
chart?: { label: string; chart: ChartResult & { time_known: boolean } };
summary?: PublicSummary | null;
}
const SLUG = /^[A-Za-z0-9_-]{8,64}$/;
/** The shared content, or `null` if the link is unknown or revoked (one backend call per request). */
export const loadShare = cache(async (slug: string): Promise<PublicShare | null> => {
if (!SLUG.test(slug)) return null;
const res = await fetch(`${BACKEND_URL}/v1/public/shares/${slug}`, { cache: "no-store" });
if (res.status === 404) return null;
if (!res.ok) throw new Error(`share ${slug}: HTTP ${res.status}`);
return (await res.json()) as PublicShare;
});
/** The site's own address, for absolute links in the preview tags. */
export async function siteUrl(): Promise<string> {
const fixed = process.env.PUBLIC_BASE_URL;
if (fixed) return fixed.replace(/\/$/, "");
const h = await headers();
const host = h.get("x-forwarded-host") ?? h.get("host") ?? "localhost:3001";
const proto = h.get("x-forwarded-proto") ?? (host.startsWith("localhost") ? "http" : "https");
return `${proto}://${host}`;
}
/** Model text without the `**bold**` marks. */
export function plain(text: string): string {
return text.replace(/\*\*/g, "").replace(/\s+/g, " ").trim();
}
/** At most `max` characters, cut at a word, with an ellipsis. */
export function shorten(text: string, max: number): string {
if (text.length <= max) return text;
const cut = text.slice(0, max - 1);
const space = cut.lastIndexOf(" ");
return `${(space > max * 0.6 ? cut.slice(0, space) : cut).replace(/[\s,;:–-]+$/, "")}…`;
}
/** Changes whenever the summary is rewritten, so link previews fetch the new image. */
export function imageVersion(share: PublicShare): string {
const s = share.summary;
if (!s) return "kep";
const time = s.completed_at ? Date.parse(s.completed_at).toString(36) : "0";
return `${s.id.replace(/-/g, "").slice(-8)}${time}`;
}
/** "Nap Nyilas · Hold Bika · Aszcendens Mérleg" (the ascendant only with a known time). */
export function bigThree(chart: ChartResult & { time_known: boolean }): { label: string; sign: string }[] {
const sign = (id: string) => chart.points.find((p) => p.id === id)?.sign;
const asc = chart.angles?.ascendant;
const out: { label: string; sign: string }[] = [];
const sun = sign("sun");
const moon = sign("moon");
if (sun) out.push({ label: "Nap", sign: sun });
if (moon) out.push({ label: "Hold", sign: moon });
if (chart.time_known && typeof asc === "object" && asc) out.push({ label: "Aszcendens", sign: asc.sign });
return out;
}
+74
View File
@@ -0,0 +1,74 @@
// The chart wheel as a standalone SVG document, for the share preview image.
// It reuses the page's <ChartWheel> and turns its element tree into markup
// with the colours inlined (no page CSS there).
import { Fragment, isValidElement, type ReactElement, type ReactNode } from "react";
import { ChartWheel, SIZE } from "@/components/ChartWheel";
import type { ChartResult } from "@/lib/types";
// The page's wheel rules (globals.css) with the colour tokens resolved.
const STYLE = `
.w-ring{fill:none;stroke:#2c3654;stroke-width:1.2}
.w-faint{stroke:#1f2740}
.w-inner{fill:#0f1426}
.w-sign{stroke:#2c3654;stroke-width:1}
.w-fire{fill:rgba(227,139,106,.09)}
.w-earth{fill:rgba(159,216,176,.07)}
.w-air{fill:rgba(217,180,106,.08)}
.w-water{fill:rgba(127,176,224,.09)}
.w-sign-glyph{fill:#d9b46a;font-family:'DejaVu Sans';font-size:22px}
.w-tick,.w-cusp{stroke:#2c3654;stroke-width:1}
.w-axis{stroke:#d9b46a;stroke-width:1.8}
.w-axis-label{fill:#d9b46a;font-family:Inter;font-weight:600;font-size:14px}
.w-house-num{fill:#a3abc0;font-family:Inter;font-size:12px}
.w-aspect{stroke-linecap:round}
.w-asp-trine{stroke:#7fb0e0}
.w-asp-sextile{stroke:#7fb0e0;stroke-dasharray:5 4}
.w-asp-square{stroke:#f0a35e}
.w-asp-opposition{stroke:#e38b8b}
.w-point-tick{stroke:#ece6d8;stroke-width:1.6}
.w-leader{stroke:#a3abc0;stroke-width:.7;stroke-opacity:.6}
.w-point-glyph{fill:#ece6d8;font-family:'DejaVu Sans';font-size:22px}
.w-point-deg{fill:#a3abc0;font-family:Inter;font-size:10.5px}
.w-end{fill:#c9cfdc}
.w-point-glyph,.w-point-deg,.w-house-num,.w-axis-label{paint-order:stroke;stroke:#111729;stroke-width:4px;stroke-linejoin:round}
`;
// React prop names that are not SVG attributes (or not wanted in the image).
const SKIP = new Set(["children", "key", "role", "id"]);
const escape = (s: string) =>
s.replace(/︎/g, "").replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
function attribute(name: string): string {
if (name === "className") return "class";
if (name === "viewBox") return name;
return name.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
}
function markup(node: ReactNode): string {
if (node === null || node === undefined || typeof node === "boolean") return "";
if (typeof node === "string" || typeof node === "number") return escape(String(node));
if (Array.isArray(node)) return node.map(markup).join("");
if (!isValidElement(node)) return "";
const { type, props } = node as ReactElement<Record<string, unknown>>;
if (type === Fragment) return markup(props.children as ReactNode);
if (typeof type === "function") return markup((type as (p: unknown) => ReactNode)(props));
if (typeof type !== "string" || type === "title" || type === "desc") return "";
const attrs = Object.entries(props)
.filter(([k, v]) => !SKIP.has(k) && !k.startsWith("aria-") && v !== undefined && v !== null && v !== false)
.map(([k, v]) => ` ${attribute(k)}="${escape(String(v))}"`)
.join("");
return `<${type}${attrs}>${markup(props.children as ReactNode)}</${type}>`;
}
/** The wheel as an SVG document of `size` pixels. */
export function wheelSvg(chart: ChartResult, timeKnown: boolean, size = SIZE): string {
const body = markup(ChartWheel({ chart, timeKnown }))
.replace(/^<svg[^>]*>/, "")
.replace(/<\/svg>$/, "");
return (
`<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 ${SIZE} ${SIZE}">` +
`<style>${STYLE}</style>${body}</svg>`
);
}