Files
co-lite/solvway-admin-portal/app/components/pages/CmsBlocks.tsx
T
ngqileandClaude Opus 5.5 405dab55dd fix(pages): fit the top bar, hero and trust strip on one desktop screen
A trust strip band uses less padding, the full-width hero less padding, and the
landing-page form tighter gaps with the consent in small text. Controls keep
their 44 px height. On the INS page at 1920x1080 the trust strip now ends at
937 px instead of 1077 px.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-04 12:44:55 +08:00

356 lines
20 KiB
TypeScript

import type { CSSProperties, ReactNode } from "react";
import { ICONS, LOOKS, anchorId, contrastText, imageSrc, isSafeLink, type CmsBlock, type Look } from "@/app/lib/cmsPages";
import type { RegistrationInfo } from "@/app/lib/services/publicRegistration";
import { SafeHtml } from "./SafeHtml";
import { LandingForm } from "./LandingForm";
import { HtmlFrame } from "./HtmlFrame";
import { EditContext, EditSection, HeroShade, HtmlSpot, ImageSpot, RichSpot, Text, stopNavigation, type PageEdit } from "./CmsEdit";
// Landing-page blocks, ported from babyboo's CmsBlocks minus the product blocks. HTML only goes through SafeHtml;
// images are this company's uploads; buttons render only for a safe link, checked again here. A Custom HTML block is
// never rendered here: it is a sealed frame loaded from its own address (add-custom-html-block).
export type PublicBlock = CmsBlock & { form?: RegistrationInfo; closed?: boolean };
export interface Branding {
code: string;
name: string;
logoImageId?: number | null;
accentColor?: string | null;
}
const PROSE = "space-y-3 text-base leading-relaxed [&_a]:text-primary [&_a]:underline [&_blockquote]:border-l-2 [&_blockquote]:pl-3 [&_h2]:text-2xl [&_h2]:font-semibold [&_h3]:text-xl [&_h3]:font-semibold [&_ol]:list-decimal [&_ol]:pl-5 [&_ul]:list-disc [&_ul]:pl-5";
// add-page-sections: non-default looks are full-width bands. On an accent band the button flips to stay visible.
const BANDS: Record<Exclude<Look, "DEFAULT">, string> = {
MUTED: "bg-muted text-foreground",
DARK: "bg-foreground text-background",
ACCENT: "bg-primary text-primary-foreground",
};
const bandOf = (look: unknown) => (LOOKS.includes(look as Look) && look !== "DEFAULT" ? (look as Exclude<Look, "DEFAULT">) : null);
function ButtonLink({ block, index, edit }: { block: CmsBlock; index: number; edit?: boolean }) {
// In edit mode a button with a label shows whatever its link, so its label can be typed in place.
if (!block.buttonLabel || (!edit && !isSafeLink(block.buttonUrl))) return null;
const external = /^https:/i.test(block.buttonUrl!);
const colours = bandOf(block.look) === "ACCENT" ? "bg-background text-foreground" : "bg-primary text-primary-foreground";
return (
<a href={block.buttonUrl} {...(external ? { target: "_blank", rel: "noopener noreferrer" } : {})}
className={`inline-flex h-11 items-center rounded-md px-5 text-sm font-medium hover:opacity-90 focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none ${colours}`}>
<Text as="span" index={index} path="buttonLabel" value={block.buttonLabel} max={40} code="PagesButtonLabel" />
</a>
);
}
// add-landing-page-polish: a section's serif heading, with its optional label above. The label is the accent colour,
// darkened toward the text colour on light backgrounds, where a pale accent would be hard to read.
const LABEL_COLOUR: Record<string, string> = {
DARK: "text-primary",
ACCENT: "opacity-80",
LIGHT: "text-[color-mix(in_srgb,var(--primary)_60%,var(--foreground))]",
};
const LABELLED = ["FEATURES", "STEPS", "IMAGE_TEXT", "FAQ", "CTA"];
function SectionHeading({ block, index, className }: { block: CmsBlock; index: number; className: string }) {
const band = bandOf(block.look);
return (
<>
{LABELLED.includes(block.type) && <Text as="p" index={index} path="eyebrow" value={block.eyebrow} max={40} code="PagesEyebrow"
className={`mb-2 text-xs font-semibold tracking-widest uppercase ${LABEL_COLOUR[band === "DARK" || band === "ACCENT" ? band : "LIGHT"]}`} />}
<Text as="h2" index={index} path="heading" value={block.heading} max={120} code="PagesHeading" className={`font-serif font-semibold ${className}`} />
</>
);
}
function Block({ block, index, company, slug, preview, edit }: { block: PublicBlock; index: number; company: string; slug: string; preview?: boolean; edit?: boolean }) {
const image = block.imageId ? imageSrc(block.imageId) : undefined;
const button = <ButtonLink block={block} index={index} edit={edit} />;
switch (block.type) {
case "HERO": {
const wide = !!block.fullWidth;
const points = block.cards?.length ? (
<ul className="space-y-2">
{block.cards.map((card, i) => (
<li key={i} className="flex items-baseline gap-3 before:size-2 before:shrink-0 before:rounded-full before:bg-primary">
<Text as="span" index={index} path={`cards.${i}.title`} value={card.title} max={80} code="PagesPointN" n={i + 1} />
</li>
))}
</ul>
) : null;
const words = (
<>
<Text as="h2" index={index} path="heading" value={block.heading} max={120} code="PagesHeading"
className={wide ? "font-serif text-4xl leading-tight font-semibold sm:text-5xl" : "text-3xl font-semibold sm:text-4xl"} />
<Text as="p" index={index} path="text" value={block.text} max={300} code="PagesText" className="max-w-2xl text-lg" />
{points}
<div>{button}</div>
</>
);
// add-hero-signup-form: with an event, the sign-up form sits in a card beside the text (below it on phones).
// add-hero-full-width: the card may open with its own heading and text.
const card = block.eventUuid ? (
<div className="space-y-3 rounded-lg border bg-card p-6 text-card-foreground shadow-card">
{block.formHeading || block.formText || edit ? (
<div className="space-y-1">
<Text as="h3" index={index} path="formHeading" value={block.formHeading} max={80} code="PagesFormHeading" className="font-serif text-2xl font-semibold" />
<Text as="p" index={index} path="formText" value={block.formText} max={200} code="PagesFormText" className="text-sm text-muted-foreground" />
</div>
) : null}
<LandingForm companyCode={company} slug={slug} index={index} info={block.closed ? undefined : block.form} preview={preview} thanks={block.thanksText} />
</div>
) : null;
const back = bandOf(block.look) ? "" : "bg-muted";
if (wide) {
// Edge to edge; the shade is darkest behind the text and clears toward the far side. The text is centred in the
// hero's opening height, not beside a long form, so it stays in the first screen.
const grid = `mx-auto grid w-full max-w-6xl items-start gap-8 px-4 pt-8 pb-8 ${card ? "md:grid-cols-[minmax(0,1fr)_minmax(0,28rem)]" : ""}`;
return (
<section className={`relative overflow-hidden ${back}`}>
<ImageSpot index={index} src={image} className="absolute inset-0 h-full w-full object-cover" corner />
<HeroShade index={index} image={!!image} shaded="relative bg-gradient-to-r from-black/60 to-black/60 text-white md:from-black/55 md:to-black/10 md:to-70%" plain="">
<div className={grid}>
<div className="flex flex-col justify-center gap-4 md:min-h-[32rem]">{words}</div>
{card}
</div>
</HeroShade>
</section>
);
}
if (card) {
// Top padding leaves room for the editor's image button in the corner.
const grid = "grid items-start gap-6 px-6 pt-16 pb-6 md:grid-cols-[minmax(0,1fr)_minmax(0,26rem)] md:px-10 md:pb-10";
return (
<section className={`relative overflow-hidden rounded-lg ${back}`}>
<ImageSpot index={index} src={image} className="absolute inset-0 h-full w-full object-cover" corner />
<HeroShade index={index} image={!!image} shaded={`relative ${grid} bg-gradient-to-r from-black/80 via-black/60 to-black/40 text-white sm:min-h-96`} plain={grid}>
<div className="flex flex-col gap-3">{words}</div>
{card}
</HeroShade>
</section>
);
}
return (
<section className={`relative overflow-hidden rounded-lg ${back}`}>
<ImageSpot index={index} src={image} className="absolute inset-0 h-full w-full object-cover" corner />
<HeroShade index={index} image={!!image}
shaded="relative flex min-h-72 flex-col justify-end gap-3 bg-gradient-to-t from-black/80 via-black/50 to-transparent p-6 text-white sm:min-h-96"
plain="flex flex-col gap-3 p-6">
{words}
</HeroShade>
</section>
);
}
case "RICH_TEXT":
return block.html || edit ? (
<section><RichSpot index={index} html={block.html} className={PROSE}>{block.html ? <SafeHtml html={block.html} className={PROSE} /> : null}</RichSpot></section>
) : null;
case "IMAGE_TEXT":
return (
<section className="grid items-center gap-6 md:grid-cols-2">
{image || edit ? (
<div className={block.imagePosition === "RIGHT" ? "md:order-2" : undefined}>
<ImageSpot index={index} src={image} className="w-full rounded-lg object-cover" />
</div>
) : null}
<div className="space-y-3">
<SectionHeading block={block} index={index} className="text-2xl" />
<RichSpot index={index} html={block.html} className={PROSE}>{block.html ? <SafeHtml html={block.html} className={PROSE} /> : null}</RichSpot>
{button}
</div>
</section>
);
case "FAQ":
return block.items?.length ? (
<section className="space-y-3">
<SectionHeading block={block} index={index} className="text-2xl" />
<div className="divide-y rounded-lg border">
{block.items.map((item, i) => {
const question = <Text as="span" index={index} path={`items.${i}.question`} value={item.question} max={300} code="PagesQuestionN" n={i + 1} />;
const answer = <Text as="p" index={index} path={`items.${i}.answer`} value={item.answer} max={2000} code="PagesAnswerN" n={i + 1} multiline className="mt-2 whitespace-pre-line text-muted-foreground" />;
// In edit mode every answer shows: a <summary> would toggle on a click or a typed space.
return edit ? (
<div key={i} className="p-4"><p className="font-medium">{question}</p>{answer}</div>
) : (
<details key={i} name={`faq-${index}`} className="group p-4">
<summary className="cursor-pointer font-medium">{item.question}</summary>
{answer}
</details>
);
})}
</div>
</section>
) : null;
case "FORM":
return (
<section className="mx-auto w-full max-w-md space-y-4 rounded-lg border bg-card p-6 shadow-card">
<SectionHeading block={block} index={index} className="text-2xl" />
<LandingForm companyCode={company} slug={slug} index={index} info={block.closed ? undefined : block.form} preview={preview} thanks={block.thanksText} />
</section>
);
case "FEATURES":
return (
<section className="space-y-6">
<div className="max-w-2xl space-y-2">
<SectionHeading block={block} index={index} className="text-2xl sm:text-3xl" />
<Text as="p" index={index} path="text" value={block.text} max={300} code="PagesText" className="text-lg opacity-80" />
</div>
<ul className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
{(block.cards ?? []).map((card, i) => {
const Icon = card.icon ? ICONS[card.icon] : undefined;
return (
<li key={i} className="space-y-2 rounded-lg border bg-card p-5 text-card-foreground shadow-card">
{Icon ? <Icon aria-hidden="true" className="size-7 text-primary" /> : null}
<Text as="h3" index={index} path={`cards.${i}.title`} value={card.title} max={80} code="PagesCardTitle" className="text-lg font-semibold" />
<Text as="p" index={index} path={`cards.${i}.text`} value={card.text} max={300} code="PagesCardText" className="text-muted-foreground" />
</li>
);
})}
</ul>
</section>
);
case "STEPS":
return (
<section className="space-y-6">
<SectionHeading block={block} index={index} className="text-2xl sm:text-3xl" />
<ol className="grid gap-6 sm:grid-cols-2 lg:grid-cols-3">
{(block.cards ?? []).map((card, i) => (
<li key={i} className="flex gap-4">
<span aria-hidden="true" className="flex size-10 shrink-0 items-center justify-center rounded-full border-2 border-current text-lg font-semibold">{i + 1}</span>
<div className="space-y-1">
<Text as="h3" index={index} path={`cards.${i}.title`} value={card.title} max={80} code="PagesCardTitle" className="text-lg font-semibold" />
<Text as="p" index={index} path={`cards.${i}.text`} value={card.text} max={300} code="PagesCardText" className="opacity-80" />
</div>
</li>
))}
</ol>
</section>
);
case "TRUST":
return (
<section>
<ul className="flex flex-wrap items-center justify-center gap-x-8 gap-y-2 text-center font-medium">
{(block.cards ?? []).map((card, i) => <li key={i}><Text as="span" index={index} path={`cards.${i}.title`} value={card.title} max={80} code="PagesCardTitle" /></li>)}
</ul>
</section>
);
case "CTA":
return (
<section className="flex flex-col items-center gap-4 py-4 text-center">
<SectionHeading block={block} index={index} className="text-2xl sm:text-3xl" />
<Text as="p" index={index} path="text" value={block.text} max={300} code="PagesText" className="max-w-2xl text-lg opacity-80" />
{button}
</section>
);
case "FOOTER":
// add-landing-thanks-and-footer: columns of title and text with contact links, the legal line under a divider.
return (
<footer role="contentinfo" className="text-sm [&_a]:text-primary [&_a]:hover:underline">
<ul className={`grid gap-8 ${["", "", "md:grid-cols-2", "md:grid-cols-3", "md:grid-cols-4"][block.cards?.length ?? 0]}`}>
{(block.cards ?? []).map((card, i) => (
<li key={i} className="space-y-1.5">
<Text as="h2" index={index} path={`cards.${i}.title`} value={card.title} max={80} code="PagesCardTitle" className="font-semibold" />
<Text as="p" index={index} path={`cards.${i}.text`} value={card.text} max={300} code="PagesCardText" multiline links className="whitespace-pre-line opacity-80" />
</li>
))}
</ul>
<Text as="p" index={index} path="text" value={block.text} max={300} code="PagesFooterLegal" className="mt-8 border-t border-muted-foreground/30 pt-4 text-xs opacity-70" />
</footer>
);
default:
return null;
}
}
const WIDTH = "mx-auto w-full max-w-5xl px-4";
/** add-landing-page-polish: the company logo (or name) and an optional button, kept at the top of the window. */
function TopBar({ block, index, company, edit }: { block: CmsBlock; index: number; company: Branding; edit: boolean }) {
return (
<header role="banner" id={anchorId(block.anchor)} className="border-b bg-card/95 backdrop-blur">
<div className="mx-auto flex max-w-6xl items-center justify-between gap-3 px-4 py-3">
{company.logoImageId ? (
// eslint-disable-next-line @next/next/no-img-element -- the company's uploaded logo
<img src={imageSrc(company.logoImageId)} alt={company.name} className="h-10 w-auto" />
) : (
<span className="text-lg font-semibold">{company.name}</span>
)}
<ButtonLink block={block} index={index} edit={edit} />
</div>
</header>
);
}
/**
* Blocks that run the full window width: Custom HTML, a full-width hero, and a coloured band (add-compact-landing-form).
* Two of them in a row join with no gap.
*/
const bleeds = (block: PublicBlock) => block.type === "HTML" || block.type === "TOPBAR" || (block.type === "HERO" && !!block.fullWidth) || !!bandOf(block.look);
/** A block in the page width; with a non-default look, inside a full-width band of that colour. */
function Section({ block, index, children }: { block: PublicBlock; index: number; children: ReactNode }) {
const band = bandOf(block.look);
// add-hero-full-width: a full-width hero sets its own width.
if (block.type === "HERO" && block.fullWidth) return <EditSection index={index}><div id={anchorId(block.anchor)}>{children}</div></EditSection>;
// add-landing-page-polish: a #name jump lands clear of a sticky top bar.
if (!band) return <EditSection index={index}><div id={anchorId(block.anchor)} className={`scroll-mt-24 ${WIDTH}`}>{children}</div></EditSection>;
return (
<EditSection index={index}>
<div id={anchorId(block.anchor)} data-look={band} className={`scroll-mt-16 ${block.type === "TRUST" ? "py-5" : "py-12"} ${BANDS[band]}`}>
<div className={WIDTH}>{children}</div>
</div>
</EditSection>
);
}
/**
* A whole landing page: the company header (logo, accent), the title as the only h1, then the blocks. Custom HTML
* blocks run full width; with hideHeader the header goes and the title is for screen readers only.
*/
export function LandingPage({ company, page, banner, preview, frameBase, edit }: {
company: Branding;
page: { slug: string; title: string; hideHeader?: boolean; blocks: PublicBlock[] };
banner?: ReactNode;
/** The editor's draft preview: forms show a note instead of taking sign-ups. */
preview?: boolean;
/** Where block frames load from; the public page's frame address by default. */
frameBase?: string;
/** The on-page editor's canvas (add-page-visual-editor): text edits in place and nothing navigates. */
edit?: PageEdit;
}) {
const frames = frameBase ?? `/p/${encodeURIComponent(company.code)}/${encodeURIComponent(page.slug)}/frame`;
const accent = company.accentColor && /^#[0-9A-Fa-f]{6}$/.test(company.accentColor) ? company.accentColor : null;
const style = accent ? ({ "--primary": accent, "--primary-foreground": contrastText(accent), "--ring": accent } as CSSProperties) : undefined;
const body = (
<div style={style} className="min-h-svh bg-background" onClickCapture={edit ? stopNavigation : undefined}>
{banner}
{page.hideHeader ? null : <header className="border-b bg-card">
<div className="mx-auto flex max-w-5xl items-center gap-3 px-4 py-3">
{company.logoImageId ? (
// eslint-disable-next-line @next/next/no-img-element -- the company's uploaded logo
<img src={imageSrc(company.logoImageId)} alt={company.name} className="h-10 w-auto" />
) : (
<span className="text-lg font-semibold">{company.name}</span>
)}
</div>
</header>}
<main className={page.hideHeader ? "pb-8" : "py-8"}>
<h1 className={page.hideHeader ? "sr-only" : `${WIDTH} mb-10 text-3xl font-semibold sm:text-4xl`}>{page.title}</h1>
<div>
{page.blocks.map((block, i) => (
// A top bar's row is what sticks: a sticky element only sticks inside its parent, and the rows share the page's.
<div key={i} className={block.type === "TOPBAR" ? "sticky top-0 z-20" : i > 0 && !(bleeds(block) && bleeds(page.blocks[i - 1])) ? "mt-10" : undefined}>
{block.type === "TOPBAR"
? <EditSection index={i}><TopBar block={block} index={i} company={company} edit={!!edit} /></EditSection>
: block.type === "HTML"
? <HtmlSpot index={i}><HtmlFrame id={anchorId(block.anchor)} src={`${frames}/${i}${edit ? `?v=${edit.frameVersion}` : ""}`} title={page.title} /></HtmlSpot>
: <Section block={block} index={i}><Block block={block} index={i} company={company.code} slug={page.slug} preview={preview} edit={!!edit} /></Section>}
</div>
))}
</div>
</main>
</div>
);
return edit ? <EditContext.Provider value={edit}>{body}</EditContext.Provider> : body;
}