Files
co-lite/solvway-admin-portal/app/components/pages/CmsBlocks.tsx
T

207 lines
10 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";
// 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 }: { block: CmsBlock }) {
if (!block.buttonLabel || !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}`}>
{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 ${bandOf(block.look) ? "" : "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>
);
case "FEATURES":
return (
<section className="space-y-6">
<div className="max-w-2xl space-y-2">
{block.heading ? <h2 className="text-2xl font-semibold sm:text-3xl">{block.heading}</h2> : null}
{block.text ? <p className="text-lg opacity-80">{block.text}</p> : null}
</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}
<h3 className="text-lg font-semibold">{card.title}</h3>
<p className="text-muted-foreground">{card.text}</p>
</li>
);
})}
</ul>
</section>
);
case "STEPS":
return (
<section className="space-y-6">
{block.heading ? <h2 className="text-2xl font-semibold sm:text-3xl">{block.heading}</h2> : null}
<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">
<h3 className="text-lg font-semibold">{card.title}</h3>
<p className="opacity-80">{card.text}</p>
</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}>{card.title}</li>)}
</ul>
</section>
);
case "CTA":
return (
<section className="flex flex-col items-center gap-4 py-4 text-center">
<h2 className="text-2xl font-semibold sm:text-3xl">{block.heading}</h2>
{block.text ? <p className="max-w-2xl text-lg opacity-80">{block.text}</p> : null}
<ButtonLink block={block} />
</section>
);
default:
return null;
}
}
const WIDTH = "mx-auto w-full max-w-5xl px-4";
/** A block in the page width; with a non-default look, inside a full-width band of that colour. */
function Section({ block, children }: { block: PublicBlock; children: ReactNode }) {
const band = bandOf(block.look);
if (!band) return <div id={anchorId(block.anchor)} className={WIDTH}>{children}</div>;
return (
<div id={anchorId(block.anchor)} data-look={band} className={`py-12 ${BANDS[band]}`}>
<div className={WIDTH}>{children}</div>
</div>
);
}
/**
* 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} id={anchorId(block.anchor)} src={`${frames}/${i}`} title={page.title} />
: <Section key={i} block={block}><Block block={block} index={i} company={company.code} slug={page.slug} preview={preview} /></Section>)}
</div>
</main>
</div>
);
}