Files
co-lite/solvway-admin-portal/app/components/pages/CmsBlocks.tsx
T
ngqileandClaude Opus 5.5 36dbb4c949 fix(pages): keep the frame's sandbox policy past the config headers
next.config's baseline Content-Security-Policy replaced the frame route's
own header, so a frame address opened directly ran without the sandbox.
A later headers rule for the two frame paths restores it. Also drop the
gap above a header-less page.

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

133 lines
6.7 KiB
TypeScript

import type { CSSProperties, ReactNode } from "react";
import { contrastText, imageSrc, isSafeLink, type CmsBlock } from "@/app/lib/cmsPages";
import type { RegistrationInfo } from "@/app/lib/services/publicRegistration";
import { SafeHtml } from "./SafeHtml";
import { LandingForm } from "./LandingForm";
import { HtmlFrame } from "./HtmlFrame";
// 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";
function ButtonLink({ block }: { block: CmsBlock }) {
if (!block.buttonLabel || !isSafeLink(block.buttonUrl)) return null;
const external = /^https:/i.test(block.buttonUrl!);
return (
<a href={block.buttonUrl} {...(external ? { target: "_blank", rel: "noopener noreferrer" } : {})}
className="inline-flex h-11 items-center rounded-md bg-primary px-5 text-sm font-medium text-primary-foreground hover:opacity-90 focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none">
{block.buttonLabel}
</a>
);
}
function BlockImage({ id, className }: { id?: number | null; className: string }) {
// eslint-disable-next-line @next/next/no-img-element -- an uploaded image of unknown size
return id ? <img src={imageSrc(id)} alt="" className={className} /> : null;
}
function Block({ block, index, company, slug, preview }: { block: PublicBlock; index: number; company: string; slug: string; preview?: boolean }) {
switch (block.type) {
case "HERO":
return (
<section className="relative overflow-hidden rounded-lg bg-muted">
<BlockImage id={block.imageId} className="absolute inset-0 h-full w-full object-cover" />
<div className={block.imageId ? "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" : "flex flex-col gap-3 p-6"}>
{block.heading ? <h2 className="text-3xl font-semibold sm:text-4xl">{block.heading}</h2> : null}
{block.text ? <p className="max-w-2xl text-lg">{block.text}</p> : null}
<div><ButtonLink block={block} /></div>
</div>
</section>
);
case "RICH_TEXT":
return block.html ? <section><SafeHtml html={block.html} className={PROSE} /></section> : null;
case "IMAGE_TEXT":
return (
<section className="grid items-center gap-6 md:grid-cols-2">
<BlockImage id={block.imageId} className={`w-full rounded-lg object-cover ${block.imagePosition === "RIGHT" ? "md:order-2" : ""}`} />
<div className="space-y-3">
{block.heading ? <h2 className="text-2xl font-semibold">{block.heading}</h2> : null}
{block.html ? <SafeHtml html={block.html} className={PROSE} /> : null}
<ButtonLink block={block} />
</div>
</section>
);
case "FAQ":
return block.items?.length ? (
<section className="space-y-3">
{block.heading ? <h2 className="text-2xl font-semibold">{block.heading}</h2> : null}
<div className="divide-y rounded-lg border">
{block.items.map((item, i) => (
<details key={i} name={`faq-${index}`} className="group p-4">
<summary className="cursor-pointer font-medium">{item.question}</summary>
<p className="mt-2 whitespace-pre-line text-muted-foreground">{item.answer}</p>
</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">
{block.heading ? <h2 className="text-2xl font-semibold">{block.heading}</h2> : null}
<LandingForm companyCode={company} slug={slug} index={index} info={block.closed ? undefined : block.form} preview={preview} />
</section>
);
default:
return null;
}
}
const WIDTH = "mx-auto w-full max-w-5xl px-4";
/**
* 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 }: {
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;
}) {
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;
return (
<div style={style} className="min-h-svh bg-background">
{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 className="space-y-10">
{page.blocks.map((block, i) => block.type === "HTML"
? <HtmlFrame key={i} src={`${frames}/${i}`} title={page.title} />
: <div key={i} className={WIDTH}><Block block={block} index={i} company={company.code} slug={page.slug} preview={preview} /></div>)}
</div>
</main>
</div>
);
}