Files

220 lines
8.5 KiB
TypeScript

// Landing-page blocks and the editor's pure helpers (add-landing-pages), ported from babyboo's cmsPages.ts
// minus the product blocks. Images are page_image ids, served at /api/public/page-images/<id>.
import { Calendar, Cloud, Headset, Lock, Monitor, Phone, ShieldCheck, Star, Tv, Users, Wifi, Wrench, type LucideIcon } from "lucide-react";
export type BlockType = "HERO" | "RICH_TEXT" | "IMAGE_TEXT" | "FAQ" | "FORM" | "HTML" | "FEATURES" | "STEPS" | "TRUST" | "CTA" | "FOOTER" | "TOPBAR" | "TICKETS";
export const BLOCK_TYPES: BlockType[] = ["HERO", "RICH_TEXT", "IMAGE_TEXT", "FEATURES", "STEPS", "TRUST", "CTA", "FAQ", "FORM", "TICKETS", "HTML", "FOOTER", "TOPBAR"];
/** Feature-card icons by the name services-counter accepts (add-page-sections). */
export const ICONS: Record<string, LucideIcon> = {
wifi: Wifi, phone: Phone, tv: Tv, monitor: Monitor, lock: Lock, "shield-check": ShieldCheck,
cloud: Cloud, wrench: Wrench, headset: Headset, users: Users, calendar: Calendar, star: Star,
};
/** Section looks: DEFAULT renders as before; the others are full-width bands. */
export const LOOKS = ["DEFAULT", "MUTED", "DARK", "ACCENT"] as const;
export type Look = (typeof LOOKS)[number];
export const BLOCK_LABELS: Record<BlockType, string> = {
HERO: "PagesBlockHero",
RICH_TEXT: "PagesBlockRichText",
IMAGE_TEXT: "PagesBlockImageText",
FAQ: "PagesBlockFaq",
FORM: "PagesBlockForm",
HTML: "PagesBlockHtml",
FEATURES: "PagesBlockFeatures",
STEPS: "PagesBlockSteps",
TRUST: "PagesBlockTrust",
CTA: "PagesBlockCta",
FOOTER: "PagesBlockFooter",
TOPBAR: "PagesBlockTopBar",
TICKETS: "PagesBlockTickets",
};
/** A FEATURES (icon, title, text), STEPS (title, text) or TRUST (title) card; a HERO's points are title-only cards; a FOOTER's columns are title-and-text cards. */
export interface CmsCard {
icon?: string;
title: string;
text?: string;
}
export interface CmsFaqItem {
question: string;
answer: string;
}
export interface CmsBlock {
type: BlockType;
imageId?: number | null;
heading?: string;
text?: string;
buttonLabel?: string;
buttonUrl?: string;
html?: string;
imagePosition?: "LEFT" | "RIGHT";
items?: CmsFaqItem[];
/** Optional link name: the block's id on the page, the target of a #name link (add-page-anchor-links). */
anchor?: string;
look?: Look;
cards?: CmsCard[];
/** The FORM block's event (a Snowflake uuid, kept as a string). */
eventUuid?: string | null;
/** add-hero-full-width: a HERO across the whole window, and its form card's heading (80) and text (200). */
fullWidth?: boolean;
formHeading?: string;
formText?: string;
/** add-landing-thanks-and-footer: text (200) under the thank-you after a sign-up; FORM, or HERO with an event. */
thanksText?: string;
/** add-landing-page-polish: a short label (40) above the heading of FEATURES, STEPS, IMAGE_TEXT, FAQ and CTA. */
eyebrow?: string;
}
export type PageStatus = "DRAFT" | "PUBLISHED";
export interface CmsPage {
id: number;
companyId: number;
slug: string;
title: string;
metaDescription: string | null;
status: PageStatus;
hideHeader?: boolean;
blocks: CmsBlock[];
updatedAt?: string | null;
/** Unpublished changes of a published page (editor reads only). */
draft?: PageContent | null;
/** List rows: the page has unpublished changes. */
hasDraft?: boolean;
/** The page company's branding, on a single page read (the editor's canvas and preview render with it). */
company?: { code: string; name: string; logoImageId?: number | null; accentColor?: string | null };
}
/** What a draft holds; status and company stay on the page. */
export interface PageContent {
slug: string;
title: string;
metaDescription: string | null;
hideHeader?: boolean;
blocks: CmsBlock[];
}
export interface CmsPageDraft {
id?: number;
companyId?: number;
slug: string;
slugTouched: boolean;
title: string;
metaDescription: string;
status: PageStatus;
/** Hide the company header and the page title (add-custom-html-block). */
hideHeader: boolean;
blocks: CmsBlock[];
}
export const imageSrc = (id: number) => `/api/public/page-images/${id}`;
export const pageAddress = (companyCode: string, slug: string) => `/p/${companyCode}/${slug}`;
export function emptyPage(): CmsPageDraft {
return { slug: "", slugTouched: false, title: "", metaDescription: "", status: "DRAFT", hideHeader: false, blocks: [] };
}
export function emptyBlock(type: BlockType): CmsBlock {
switch (type) {
case "FAQ":
return { type, heading: "", items: [{ question: "", answer: "" }] };
case "IMAGE_TEXT":
return { type, imageId: null, heading: "", html: "", imagePosition: "LEFT", buttonLabel: "", buttonUrl: "" };
case "FORM":
return { type, heading: "", eventUuid: null };
case "TICKETS":
return { type, heading: "", text: "", eventUuid: null };
case "HTML":
return { type, html: "" };
case "FEATURES":
return { type, heading: "", text: "", cards: [{ icon: "star", title: "", text: "" }, { icon: "star", title: "", text: "" }] };
case "STEPS":
return { type, heading: "", cards: [{ title: "", text: "" }, { title: "", text: "" }] };
case "TRUST":
return { type, cards: [{ title: "" }, { title: "" }] };
case "CTA":
return { type, heading: "", text: "", buttonLabel: "", buttonUrl: "" };
case "TOPBAR":
return { type, buttonLabel: "", buttonUrl: "" };
case "FOOTER":
return { type, look: "DARK", text: "", cards: [{ title: "", text: "" }, { title: "", text: "" }] };
default:
return { type };
}
}
/** Lower-case words joined by hyphens: the slug rule the counter enforces. */
export function slugify(title: string): string {
return title.toLowerCase().normalize("NFKD").replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 80);
}
export function withTitle(draft: CmsPageDraft, title: string): CmsPageDraft {
return { ...draft, title, slug: draft.slugTouched ? draft.slug : slugify(title) };
}
const blank = (v: unknown) => v === "" || v === null || v === undefined;
/** The save body: trimmed strings, blank optional fields dropped, FAQ items trimmed. */
export function pageBody(draft: CmsPageDraft) {
return {
companyId: draft.companyId,
slug: draft.slug.trim(),
title: draft.title.trim(),
metaDescription: draft.metaDescription.trim() || null,
status: draft.status,
hideHeader: draft.hideHeader,
blocks: draft.blocks.map((b) => {
const out: Record<string, unknown> = { type: b.type };
for (const [key, val] of Object.entries(b)) {
if (key === "type") continue;
if (key === "items") {
out.items = (val as CmsFaqItem[]).map((it) => ({ question: it.question.trim(), answer: it.answer.trim() }));
continue;
}
if (key === "cards") {
// Trimmed but kept, so a blank card field is refused at its own path.
out.cards = (val as CmsCard[]).map((c) => Object.fromEntries(Object.entries(c).map(([k, x]) => [k, typeof x === "string" ? x.trim() : x])));
continue;
}
if (key === "look" && val === "DEFAULT") continue;
const v = typeof val === "string" ? val.trim() : val;
if (!blank(v)) out[key] = v;
}
return out as unknown as CmsBlock;
}),
};
}
/** Black or white text, whichever contrasts more with a #RRGGBB background (WCAG relative luminance). */
export function contrastText(hex: string): "#000000" | "#FFFFFF" {
const m = /^#([0-9a-f]{6})$/i.exec(hex);
if (!m) return "#FFFFFF";
const [r, g, b] = [0, 2, 4].map((i) => {
const c = parseInt(m[1].slice(i, i + 2), 16) / 255;
return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
});
const lum = 0.2126 * r + 0.7152 * g + 0.0722 * b;
return (lum + 0.05) / 0.05 > 1.05 / (lum + 0.05) ? "#000000" : "#FFFFFF";
}
/** A block link name, as services-counter checks it. "reg-" is reserved: the sign-up form's fields use it as ids. */
export const ANCHOR = /^(?!reg-)[a-z0-9][a-z0-9-]{0,39}$/;
/** The link name when it is valid, for use as an element id. */
export const anchorId = (name: string | undefined) => (name && ANCHOR.test(name) ? name : undefined);
/** The counter's link rule: same-site path, https, mailto, tel or #name. Checked again before rendering a button. */
export function isSafeLink(url: string | undefined): boolean {
if (!url || url.length > 1000 || /[\s\\\u0000-\u001f]/.test(url)) return false;
if (url.startsWith("#")) return ANCHOR.test(url.slice(1));
if (url.startsWith("/")) return !url.startsWith("//");
return /^(https:\/\/|mailto:|tel:)/i.test(url);
}