import { Badge, Button, cn, Text } from "@cloudflare/kumo"; import type { BlockChange, BodyDiff as BodyDiffData } from "@cloudhal/core-ts/body-blocks"; import { CaretDown } from "@phosphor-icons/react"; import { Fragment, useState } from "react"; export interface BodyDiffLabels { /** Read before an added block or words, e.g. "Added". */ added: string; /** Read before a removed block or words, e.g. "Removed". */ removed: string; /** Read before an edited block, e.g. "Edited". */ changed: string; /** The button over a run of unchanged blocks, e.g. "3 unchanged blocks". */ unchanged: (count: number) => string; /** Summary badges, e.g. "+12 words", "−3 words". */ wordsAdded: (count: number) => string; wordsRemoved: (count: number) => string; /** Nothing differs. */ same: string; } export interface BodyDiffProps { diff: BodyDiffData; labels: BodyDiffLabels; className?: string; } type Run = { kind: "same"; blocks: string[] } | { kind: "change"; block: BlockChange }; /** Consecutive unchanged blocks fold into one run; every change stands alone. */ function runs(blocks: readonly BlockChange[]): Run[] { const out: Run[] = []; for (const block of blocks) { const last = out.at(-1); if (block.kind === "same") { if (last?.kind === "same") last.blocks.push(block.markdown); else out.push({ kind: "same", blocks: [block.markdown] }); } else out.push({ kind: "change", block }); } return out; } const SOURCE = "whitespace-pre-wrap break-words font-mono text-[0.9em] leading-relaxed"; /** * What changed in a markdown body (RFC-0014 §5), on its source: added blocks on a success tint, * removed ones struck through on a danger tint, edited ones with the words that changed marked * inside. Unchanged blocks fold away. ``/`` and a hidden word before each change carry * the meaning without colour (WCAG 1.4.1). */ export function BodyDiff({ diff, labels, className }: BodyDiffProps) { const changes = diff.counts.added + diff.counts.removed + diff.counts.changed; return (
{changes === 0 ? ( {labels.same} ) : ( <> {labels.wordsAdded(diff.words.added)} {labels.wordsRemoved(diff.words.removed)} )}
{changes > 0 && (
{runs(diff.blocks).map((run, i) => run.kind === "same" ? ( // biome-ignore lint/suspicious/noArrayIndexKey: runs have no identity beyond their order. ) : ( // biome-ignore lint/suspicious/noArrayIndexKey: runs have no identity beyond their order. ), )}
)}
); } function UnchangedRun({ blocks, labels }: { blocks: string[]; labels: BodyDiffLabels }) { const [open, setOpen] = useState(false); if (open) return (
{blocks.join("\n\n")}
); return (
); } function ChangedBlock({ block, labels }: { block: BlockChange; labels: BodyDiffLabels }) { if (block.kind === "added") return ( {labels.added}: {block.markdown} ); if (block.kind === "removed") return ( {labels.removed}: {block.markdown} ); if (block.kind !== "changed") return null; return (
{labels.changed}: {block.words.map((w, i) => ( // biome-ignore lint/suspicious/noArrayIndexKey: word runs have no identity beyond their order. {w.kind === "same" ? ( w.text ) : w.kind === "added" ? ( {labels.added}: {w.text} ) : ( {labels.removed}: {w.text} )} ))}
); }