"use client"; import * as React from "react"; import { ClockIcon, SquarePenIcon } from "lucide-react"; import { isoDaysBetween, type StatementAuditSnapshot, type TemporaryStatementView, } from "@accessibit/types"; import { useSiteAdmin, type SiteAdminFormat } from "../context"; import { fill, fillFor } from "../lib/fill"; import { Badge } from "../ui/badge"; import { Card } from "../ui/card"; import { Skeleton } from "../ui/skeleton"; import type { StatementState } from "./statementState"; const DAY: Intl.DateTimeFormatOptions = { year: "numeric", month: "short", day: "numeric", }; /** A calendar day (`YYYY-MM-DD`): formatted in UTC, so nobody west of * Greenwich reads the day before. */ const formatDay = (day: string, format: SiteAdminFormat): string => format.date(new Date(`${day}T00:00:00Z`), { ...DAY, timeZone: "UTC" }); /** A moment, as the day and the time it was in Italy. */ const formatMoment = (moment: string, format: SiteAdminFormat): string => format.customerDate(moment, { ...DAY, hour: "2-digit", minute: "2-digit", }); /** * The facts on top of the statement page (ACC-540): what state the statement * is in, its outcome, the audit it rests on and when it was last updated. * While a temporary statement is what the link shows, the facts are that * one's: until when it is online and when it was published. * * The outcome is the one the reference audit gives today, which is what the * preview prints and what the next generation publishes. It is not read off * the published page: since ACC-571 the statement records the outcome it was * generated with (`Statement.complianceBucket`), and when a newer run exists * the page says so in its own notice (`StatementNewerResultNotice`), which * names the published run; the run here is then labelled as the next * publication's (`basisIsNext`), so the two lines never name two runs with * the same words. * * While the statement is being read (`loading`) the facts that come from it * hold their place and say nothing: a published statement must not read * "Bozza, mai pubblicata" for the moment the answer takes. What the server * already sent (the audit, the temporary statement) is said at once. */ export function StatementFacts({ state, auditSnapshot, generatedAt, savedAt, temporary, loading = false, basisIsNext = false, }: { state: StatementState; auditSnapshot: StatementAuditSnapshot | null; /** When the published pages were last written; null if never. */ generatedAt: string | null; /** When the form was last saved; null on a statement never saved. */ savedAt: string | null; temporary: TemporaryStatementView | null; /** The statement has not been read yet. */ loading?: boolean; /** * The page shows the newer-result notice: the run named here is the next * publication's, not the one the published document declares. */ basisIsNext?: boolean; }) { const { messages, locale: lang, format } = useSiteAdmin(); const formDict = messages.statement.form; const t = messages.statement.facts; // The outcome in the dashboard's own words, the ones the overview and the // reports use: the document's wording is the document's. const outcomes = messages.auditCompliance; const badge: Record = { generating: {formDict.status.processing}, failed: {formDict.status.failed}, temporary: ( {t.temporary} ), published: {t.published}, nothingOnline: {t.nothingOnline}, draft: ( {t.draft} ), }; const current = temporary?.current ?? null; const showsTemporary = current?.state === "active"; const outcome = showsTemporary ? // The temporary statement declares one outcome, by definition. outcomes.parzialmente_conforme : generatedAt === null ? t.outcomePending : auditSnapshot ? outcomes[auditSnapshot.bucket] : t.outcomeUnavailable; // As tall as a line of the values, so nothing moves when they land. const pending = ; const facts: Array<{ label: string; value: React.ReactNode }> = [ { label: formDict.status.label, value: loading ? pending : badge[state] }, { label: t.outcome, // The temporary statement's outcome is known without the row. value: loading && !showsTemporary ? pending : outcome, }, ]; if (showsTemporary && current) { const daysLeft = isoDaysBetween(temporary!.today, current.releaseDate); const left = daysLeft === 1 ? t.daysLeftOne : daysLeft > 1 ? fill(t.daysLeft, { count: String(daysLeft) }) : null; facts.push( { label: t.onlineUntil, value: [formatDay(current.releaseDate, format), left] .filter(Boolean) .join(" · "), }, { label: t.publishedOn, value: formatDay(current.draftedOn, format) }, ); } else if (state === "nothingOnline" && current) { facts.push({ label: t.temporaryExpiredOn, value: formatDay(current.releaseDate, format), }); } else { const basis = auditSnapshot?.basedOn; facts.push({ label: basisIsNext ? t.nextBasedOn : t.basedOn, value: basis ? [ fillFor(lang, t.basis[basis.source ?? basis.kind], { date: format.customerDate(basis.completedAt, DAY), }), basis.pagesScanned === 1 ? t.pagesOne : fill(t.pages, { count: String(basis.pagesScanned) }), ].join(" · ") : t.noAudit, }); if (loading) { facts.push({ label: t.updatedOn, value: pending }); } else if (generatedAt) { facts.push({ label: t.updatedOn, value: formatMoment(generatedAt, format), }); } else if (savedAt) { facts.push({ label: t.lastSaved, value: formatMoment(savedAt, format) }); } } return (
{facts.map((fact) => (
{fact.label}
{fact.value}
))}
); }