"use client"; import * as React from "react"; import { CheckIcon, ClockIcon, CodeIcon, CopyIcon, ExternalLinkIcon, } from "lucide-react"; import { STATEMENT_LOCALES, type StatementLocale } from "@accessibit/types"; import { Button } from "../ui/button"; import { Card } from "../ui/card"; import { CodeBlock } from "../ui/code-block"; import { Label } from "../ui/label"; import { Skeleton } from "../ui/skeleton"; import { StatementLanguageSelect } from "./StatementLanguageSelect"; import { useMessages } from "../context"; /** * Belt-and-braces HTML-attribute escaping. The URLs are server-generated and * `title` is a dict string, so neither should contain unsafe characters * today — but the snippet is copy-pasted into arbitrary customer sites, so a * stray quote in a future translation shouldn't be able to break out of the * attribute. */ function escapeAttr(value: string): string { return value .replace(/&/g, "&") .replace(/"/g, """) .replace(//g, ">"); } function buildResponsiveSnippet(url: string, title: string): string { const u = escapeAttr(url); const t = escapeAttr(title); return `
`; } /** A host's own way to embed the statement, in place of the iframe. */ export interface StatementEmbedCode { label: string; code: string; note: string; } /** How long the link's copy button says it has copied. */ const COPIED_FOR_MS = 2000; /** * The address of the statement as a page of its own, with a button that * copies it and one that opens it. Not a `CodeBlock`: it is one line, and the * thing to do with it is as often to open it as to copy it. */ function DirectLink({ url, title, copyLabel, copiedLabel, openLabel, }: { url: string; title: string; copyLabel: string; copiedLabel: string; openLabel: string; }) { const [copied, setCopied] = React.useState(false); const timer = React.useRef | null>(null); React.useEffect( () => () => { if (timer.current) clearTimeout(timer.current); }, [], ); const copy = async () => { try { await navigator.clipboard.writeText(url); } catch { // The clipboard can be refused, or missing on a page not served over // HTTPS; the address stays there to select by hand. return; } setCopied(true); if (timer.current) clearTimeout(timer.current); timer.current = setTimeout(() => { timer.current = null; setCopied(false); }, COPIED_FOR_MS); }; return (
{url} {/* A button that changes its icon is not announced: said here. */} {copied ? `${copiedLabel}: ${title}` : ""}
); } /** * "Pubblica sul tuo sito": what the customer leaves the statement page with. * The code to paste once, and the address of the statement as a page of its * own. * * **There is deliberately no preview here** (ACC-381). There used to be one, * beside the snippet, and it was the page's second: it was fed the same * document as the preview card above, off the same page-level language state, * so the two frames carried byte-identical `srcDoc` and moved in lockstep as * the user typed. Inside a card about what is published, it showed unsaved * edits. The direct link and "open in a new tab" show the published artifact, * by fetching it, which is the only thing that can. * * Since ACC-260 the statement is published in three languages, and the * snippet always embeds the automatic entry point * (`/api/public/statements/:id`), which resolves the VISITOR's language at * request time: a visitor should land in their own language, not in the * account holder's. * * **The language select here is the page's, not the card's** (ACC-515). It is * on the same page-level state as the preview's switch and the tabs of the * free text: a pick here moves those too, and the card keeps no copy of its * own. The one thing on this card that HAS a language is the direct link. The * snippet stays language-agnostic by design: the select cannot touch it. * * **The direct link is the entry point too, with `?lang=`** (ACC-523). It used * to be the address of the published file on the CDN, which nothing we decide * later can reach: a statement withdrawn with its plan (ACC-516) and a * temporary statement published afterwards (ACC-504) are both answered by the * entry point alone, so a site that had pasted the file's address kept showing * the old document. The panel is therefore not handed the files' addresses at * all, only which languages have one. Addresses pasted before this stay as * they are. */ export function StatementEmbedPanel({ autoUrl, locales, locale, onLocaleChange, temporaryNote, embedCode, }: { /** * `/api/public/statements/:id` — negotiates the visitor's language. This is * what the snippet always embeds: a customer who pastes it once serves every * visitor their own language, and gains any language published later without * touching their site again. */ autoUrl: string; /** * The languages that have a published document: the regular statement's * files, or all three while a temporary statement is active (ACC-504). */ locales: readonly StatementLocale[]; /** The page's language: the preview, the tabs of the text and this card. */ locale: StatementLocale; /** Moves the page's language; the card holds none of its own. */ onLocaleChange: (locale: StatementLocale) => void; /** * Said above the code while a temporary statement is what the code and the * link show (ACC-504): until when. */ temporaryNote?: React.ReactNode; /** * What the host's site embeds instead of the iframe snippet: WordPress's * shortcode, which prints the same frame (ACC-404). */ embedCode?: StatementEmbedCode; }) { const messages = useMessages(); const fields = messages.statement.form.fields; const embed = messages.statement.form.embed; const publishedLocales = STATEMENT_LOCALES.filter((l) => locales.includes(l)); /** * Normally the page's language, verbatim: a save publishes all three, so * every language the page can be on has an artifact here. The clamp is for * the statement generated before ACC-260, which has one published file and * three editable languages — there, linking to the page's language would hand * the customer a URL to someone else's document. */ const selected = publishedLocales.includes(locale) ? locale : publishedLocales[0]; /** * The snippet is the entry point, always. Only this link has a language, * and it asks the same entry point for it. */ const directUrl = selected ? `${autoUrl}?lang=${selected}` : autoUrl; const responsiveSnippet = buildResponsiveSnippet(autoUrl, embed.iframeTitle); return ( {/* Wraps: on a narrow card the select has no room beside the title, and goes under it. */}

{embed.publishTitle}

{embed.subtitle}

{/* Only the published languages: on a statement generated before ACC-260 the page edits three and this card can link to one. With no published language there is nothing to offer. */} {selected && (
)}
{temporaryNote && (

)} {/* The two things the customer leaves with, side by side: the code to paste, and the link to the page itself. */}

{embedCode?.note ?? embed.codeNote}

{/* As tall as the code's label row, whose button is 40px. */} {embed.linkTitle}

{embed.linkNote}

); } /** * The same card before anything is published: there is no code and no link * to hand out yet, and the card says when there will be. * * While the statement is being read (`loading`) it holds the place and says * nothing: "after the first generation" would be false of a statement that * turns out to be published. */ export function StatementEmbedPlaceholder({ loading = false, }: { loading?: boolean; }) { const messages = useMessages(); const embed = messages.statement.form.embed; return (

{embed.publishTitle}

{loading ? (
) : (
)}
); }