"use client"; import * as React from "react"; import { ClockIcon, CloudUploadIcon, EyeIcon, Loader2Icon } from "lucide-react"; import { TEMPORARY_STATEMENT_NOTICE_DAYS, isIsoDay, isoDaysBetween, romeDay, type StatementLocale, type TemporaryStatementView, } from "@accessibit/types"; import { useSiteAdmin, type SiteAdminFormat } from "../context"; import { fill, fillFor } from "../lib/fill"; import { Badge } from "../ui/badge"; import { Button } from "../ui/button"; import { Card } from "../ui/card"; import { Input } from "../ui/input"; import { Notice } from "../ui/notice"; import { StatementField } from "./StatementField"; import { StatementPreviewDialog } from "./StatementPreview"; /** * A calendar day for the reader. Formatted in UTC: the value is a day, not an * instant, and the browser's own timezone would print the day before for * anyone west of Greenwich. */ const formatDay = ( day: string, format: SiteAdminFormat, month: "long" | "short" = "long", ): string => format.date(new Date(`${day}T00:00:00Z`), { year: "numeric", month, day: "numeric", timeZone: "UTC", }); /** * The temporary accessibility statement on the Statement page (ACC-504): * "partially compliant" while the site is being renewed, with the date the * compliant version is promised by. Business and Enterprise only — the page * does not mount this card on any other plan, except to keep showing a * statement still active after the plan changed (`not_in_plan`). * * One per plan per licence year (ACC-514): on a plan with subdomains the card * says so, and when the year's statement was published for another of the * plan's sites it says which, instead of offering a form the server refuses. * * The card decides nothing. What can be published, and within which dates, * comes from the server (`TemporaryStatementView`), and the server checks it * all again on publication; this only shows it and collects the date. It uses * the page's own form for everything else the document prints. */ export function TemporaryStatementCard({ view, domainName, locale, onLocaleChange, busy, publishing, canEdit = true, previewFor, onPublish, onGenerateRegular, }: { view: TemporaryStatementView; /** The site, as the preview names whose visitors see the document. */ domainName: string; /** The page's document language: shared with its tabs and its previews. */ locale: StatementLocale; onLocaleChange: (locale: StatementLocale) => void; /** * The page is loading, saving, generating or publishing: the card's * controls are locked. */ busy: boolean; /** * This card's own publication is the thing under way. Kept apart from * `busy` so the button does not claim to be publishing while the page loads * or generates the regular statement. */ publishing: boolean; /** * False for a reader: the server refuses the publication, so the date is * shown and nothing can be sent. The preview stays. */ canEdit?: boolean; /** The temporary document for one language and one release date. */ previewFor: (locale: StatementLocale, releaseDate: string) => string; onPublish: (releaseDate: string) => void; /** * Saves the form and generates the regular statement, which is what ends * the temporary one: the page asks first when one is active. */ onGenerateRegular: () => void; }) { const { messages, locale: lang, format } = useSiteAdmin(); const t = messages.statement.temporary; const { current, bounds, publication } = view; const active = current?.state === "active"; const correcting = publication === "correction"; // An active statement opens on its own date; a new one on the latest day // allowed, since the customer is buying time. const initialDate = active ? current.releaseDate : (bounds?.max ?? ""); const [releaseDate, setReleaseDate] = React.useState(initialDate); // The view is recomputed by the server after a publication. React.useEffect(() => setReleaseDate(initialDate), [initialDate]); const [previewOpen, setPreviewOpen] = React.useState(false); const previewButton = React.useRef(null); const dateAllowed = bounds !== null && isIsoDay(releaseDate) && releaseDate >= bounds.min && releaseDate <= bounds.max; const daysLeft = active ? isoDaysBetween(view.today, current.releaseDate) : null; const endingSoon = daysLeft !== null && daysLeft <= TEMPORARY_STATEMENT_NOTICE_DAYS; // The day the page speaks of: the one typed while it is a valid one, // otherwise the one online, otherwise the latest allowed. const spokenDate = dateAllowed ? releaseDate : active ? current.releaseDate : (bounds?.max ?? ""); const badge = publication === "open" ? ( {t.availableBadge} ) : current?.state === "active" ? ( // "1 of 1 used" only where it is this year's one: a statement of the // year before, online past the renewal, has used nothing of this one. correcting && view.yearSpent ? ( {t.usedBadge} ) : ( {t.activeBadge} ) ) : current?.state === "replaced" ? ( {t.replacedBadge} ) : current?.state === "expired" ? ( {t.expiredBadge} ) : null; return (

{active && correcting ? t.titleActive : t.title}

{/* What it is and what follows it, said while one can be published or corrected. What comes back on its date depends on whether a regular statement is published. */} {bounds && spokenDate && (

{t.intro}{" "} {fillFor( lang, view.regularPublished ? t.afterRegular : t.afterNothing, { date: formatDay(spokenDate, format, "short") }, )}

)} {/* One per plan (ACC-514): said wherever the plan has more than one site, since the description reads as this site's own. */} {view.planShared && (

{t.planShared}

)}
{badge}
{active && endingSoon && ( {fillFor(lang, t.endingSoon, { date: formatDay(current.releaseDate, format), })} )} {/* Active on a plan that still includes it but with nothing to correct it with (its audits are gone): the card has no form, so it says what the link shows and until when. */} {active && !correcting && publication !== "not_in_plan" && (

{fillFor(lang, t.activeLine, { date: formatDay(current.releaseDate, format), })}{" "} {daysLeft === 1 ? t.daysLeftOne : fill(t.daysLeft, { count: daysLeft ?? 0 })}

)} {/* The embed entry point is what serves the temporary statement. A site that links the regular statement's own file on the CDN never goes through it, and keeps showing that file. The direct link was that file's address until ACC-523; the one shown now is the entry point. */} {active && view.regularPublished && (

{t.directLinkNote}

)} {publication === "not_in_plan" && (

{active ? fillFor(lang, t.notInPlanUntil, { date: formatDay(current.releaseDate, format), }) : t.notInPlan}

)} {current?.state === "expired" && ( <>

{fillFor( lang, view.regularPublished ? t.expiredRegular : t.expiredEmpty, { date: formatDay(current.releaseDate, format) }, )}

{/* Nothing is online: the way out is the regular statement. */} {!view.regularPublished && canEdit && (
)} )} {current?.state === "replaced" && current.replacedAt && (

{/* An instant, read as the day it was in Italy, like every other date of this statement. */} {fillFor(lang, t.replaced, { date: formatDay( romeDay(new Date(current.replacedAt)), format, "short", ), })}{" "} {t.replacedNow}

)} {/* The licence's days of grace (ACC-516): nothing new is published until the renewal is paid, like a rescan (ACC-513); on a hand-run plan, until it is renewed (ACC-632). */} {(publication === "licence_expired" || publication === "licence_ended" || publication === "licence_renewing") && (

{/* A hand-run plan's licence has no day to say, and is not said to have expired (ACC-632). */} {publication === "licence_renewing" || view.licenceExpiredOn === null ? t.licenceRenewing : fillFor( lang, publication === "licence_ended" ? t.licenceEnded : t.licenceExpired, { date: formatDay(view.licenceExpiredOn, format) }, )}

)} {publication === "no_audit" && (

{t.noAudit}

)} {/* The year's statement was another site's of the plan: named when the reader may see that site (ACC-392). */} {publication === "already_used" && view.usedBy && (

{view.usedBy.url ? fill(t.usedBySite, { domain: view.usedBy.url }) : t.usedByOtherSite}

)} {/* When the next one can be published. After a replacement the notice under the page says it (`TemporaryStatementPlanNotice`). */} {publication === "already_used" && current?.state !== "replaced" && (

{view.availableAgainOn ? fillFor(lang, t.usedUntil, { date: formatDay(view.availableAgainOn, format, "short"), }) : t.usedUntilRenewal}

)} {bounds && (
) => setReleaseDate(e.target.value) } aria-required="true" disabled={busy || !canEdit} />
{canEdit && ( )} {/* The one way to end it before its date: the regular statement replaces it. The page asks before it does. */} {canEdit && active && ( )}
)} previewFor(locale, releaseDate)} locale={locale} onLocaleChange={onLocaleChange} returnFocusTo={previewButton} />
); } /** * What a plan gives of the temporary statement, said under the page once the * year's one is spent (ACC-540): while it is online, and after the regular * statement replaced it. The other ways it is spent (expired, used by another * site of the plan) are said in the card itself. */ export function TemporaryStatementPlanNotice({ view, planName, }: { view: TemporaryStatementView; /** "Business": the plan as the customer bought it. Null on a site whose * plan no longer includes the statement. */ planName: string | null; }) { const { messages, locale: lang, format } = useSiteAdmin(); const t = messages.statement.temporary; const spent = view.yearSpent && ((view.current?.state === "active" && view.publication === "correction") || (view.current?.state === "replaced" && view.publication === "already_used")); if (!spent || !planName) return null; return ( {view.planShared && `${t.planNoticeShared} `} {view.availableAgainOn ? fillFor(lang, t.planNoticeNext, { date: formatDay(view.availableAgainOn, format, "short"), }) : t.planNoticeNextRenewal} ); } /** * A temporary statement still online on a site that no longer holds the * statement licence (ACC-514): a subdomain taken out of its plan, a licence * that lapsed. audit-api serves it until its date whatever the licence says, * so the page behind the licence gate says so too — until when, and that * nothing about it can be changed — instead of reading as a site that shows * no statement. The notice e-mails link to that page. * * Renders nothing unless the statement is active. */ export function TemporaryStatementStillActive({ view, }: { view: TemporaryStatementView; }) { const { messages, locale: lang, format } = useSiteAdmin(); const t = messages.statement.temporary; const { current } = view; if (current?.state !== "active") return null; return ( {fillFor(lang, t.stillOnlineBody, { date: formatDay(current.releaseDate, format), })} ); }