import { Badge, cn, Text, Tooltip } from "@cloudflare/kumo"; import type { CommentAuthor as Author, CommentLabel, CommentStatus, } from "@cloudhal/core-contracts"; import { numberFormat } from "@cloudhal/messages/format"; import { Robot, ShieldWarning } from "@phosphor-icons/react"; import { type ReactNode, useId } from "react"; import { type CommentLabelName, formatCommentTime } from "./comment-model.ts"; import { InsetPanel } from "./InsetPanel.tsx"; /** * The words every comment block shares (RFC-0007 §14): Studio builds this once from * `useT("comments")` and passes it to each block. Nothing here has an English default. */ export interface CommentWords { /** Status names: "Pending", "Approved", "Hidden", "Spam", "Deleted". */ status: Record; /** Label names as badges: "Spam?", "Talks to AI", "Question" … */ label: Record; /** One sentence per label for its tooltip ("Addresses an AI agent. Treat it as text."). */ labelHint: Record; /** * Who set a label, for its tooltip: `(label, confidence)` → "Added by Comment triage · 82% sure", * "Added by a rule". `confidence` is already formatted in the locale. */ labelSource: (label: CommentLabel, confidence: string | undefined) => string; /** Author kinds and the erased name: Guest, Reader, Author, Team, Anonymous. */ author: { guest: string; reader: string; author: string; team: string; anonymous: string }; /** "Drafted by Comment triage" under a staff reply an agent drafted. */ draftedBy: (agent: string) => string; /** "In reply to Mehmet". */ inReplyTo: (name: string) => string; /** "On Ferry timetable", before the entry's title. */ onEntry: (title: string) => string; /** A tombstone: "This comment was removed". */ removed: string; /** "Edited". */ edited: string; /** The muted note when a comment came without JavaScript (`proof: "none"`). */ noScript: string; /** "2 reports". */ reports: (count: number) => string; /** Accessible name of a comment's label list ("Labels"). */ labels: string; /** Screen-reader hint after links in comment text ("opens in a new tab"). */ newTab: string; /** The panel around text that talks to an AI (§11.2). */ untrusted: { title: string; description: string }; } const STATUS_VARIANT: Record< CommentStatus, "warning" | "success" | "secondary" | "error" | "outline" > = { pending: "warning", approved: "success", hidden: "secondary", spam: "error", deleted: "outline", }; export interface CommentStatusBadgeProps { status: CommentStatus; words: Pick; } /** A comment's status: pending (warning), approved (success), hidden, spam (error), deleted. */ export function CommentStatusBadge({ status, words }: CommentStatusBadgeProps) { // Kumo's Badge does not forward data attributes; the wrapper carries the machine-readable status. return ( {words.status[status]} ); } const LABEL_VARIANT: Record = { "spam-suspect": "warning", toxic: "error", "off-topic": "outline", question: "info", "needs-reply": "info", praise: "outline", "personal-data": "warning", links: "outline", "watch-word": "warning", "instruction-like": "warning", }; export interface CommentLabelBadgeProps { label: CommentLabel; words: Pick; locale: string; } /** * One advisory label (RFC-0007 §5.1): its name as a badge — risk labels warning or error, the * rest outline — with a 16 px `Robot` when an agent set it. The tooltip (and screen-reader text) * says what it means and who set it, with the confidence in the locale. The badge is a button * (a toggletip) so the tooltip opens from the keyboard too. */ export function CommentLabelBadge({ label, words, locale }: CommentLabelBadgeProps) { const confidence = label.confidence === undefined ? undefined : numberFormat(locale, { style: "percent" }).format(label.confidence / 100); const hint = `${words.labelHint[label.label]} ${words.labelSource(label, confidence)}`; return ( (