Files
PaperClipAI/ui/src/components/ChatComposer.tsx
T
c07e650cd7 feat(ui): single-source design tokens, visual regression suite, and theme retune (#9134)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Its UI is the operator's daily surface: task lists, boards, budgets,
agent status — all built on shadcn components and Tailwind
> - Visual values (colors, spacing, type sizes, radii) were hardcoded at
~1,600 call sites: the same "small gray label" was 9/10/11px depending
on the file, charts disagreed with chips about status colors, two
toggle-switch implementations coexisted in two greens, and there was no
visual regression coverage
> - This made the UI drift-prone and made any restyle a
hundreds-of-files project, which discourages design iteration
> - This pull request extracts visual values into a single token layer
in `ui/src/index.css`, adds a Storybook visual regression suite backed
by external immutable baseline archives, and then applies a deliberate
retune reviewed change-by-change on screenshot diffs
> - The benefit is that Paperclip's look becomes a config surface:
retheming is a token edit reviewed as a snapshot diff, drift is blocked
by a token gate, and future UI PRs can prove exactly what changed
visually without committing hundreds of PNGs

## Linked Issues or Issue Description

No existing public issue covers this work (searched "design tokens",
"visual regression", "design system" across issues and PRs). Related in
spirit: Refs #8982 (theming a hardcoded panel — a one-off instance of
the same problem class this PR addresses systematically).

**Problem (feature-request form):** UI visual values are hardcoded per
call site with no source of truth and no regression coverage;
consistency depends on reviewer memory, and restyling requires mass file
edits.
**Proposed solution (this PR):** a single token layer + enforcement gate
+ externally stored visual snapshot suite, then an intentional restyle
on top of that foundation.

## What Changed

- **Token extraction (zero visual change, machine-verified during
development):** committed codemods (`scripts/codemod-*.mjs`) moved
~1,600 hardcoded color/type/spacing/radius/shadow/misc values into named
tokens in a non-inline `:root` block of `ui/src/index.css`.
- **Visual regression suite:** `pnpm test:storybook-visual` covers 255
stories × light/dark = 510 Playwright screenshots at `maxDiffPixels: 0`,
plus new primitive-coverage stories and deterministic-render fixes.
- **External visual baselines:** committed PNG snapshots were removed.
`tests/storybook-visual/baseline-manifest.json` pins an immutable
archive URL/hash/size/count, and `scripts/storybook-visual-baseline.mjs`
handles `download`, `verify`, `pack`, and trusted maintainer `upload`
flows.
- **Opt-in visual CI artifacts:** added a `Storybook Visual` workflow
that runs on manual dispatch or PRs labeled `storybook-visual`,
downloads/verifies the baseline, runs Playwright, and uploads Playwright
report/test-result artifacts for review. Normal PR runs do not mutate
baseline objects.
- **Token gate:** `pnpm check:token-gates` — zero hex literals, zero
arbitrary bracket values, zero raw font-sizes in `ui/src/components/**`
and `ui/src/pages/**`, with a documented inline allowlist for legitimate
opt-outs.
- **Theme retune (intentional, snapshot-reviewed):** new base theme
values; radius ladder derived from a single `--radius` knob; micro-type
cluster collapsed to a named ladder (`--text-nano/micro/compact` +
Tailwind `text-xs`/`text-sm`); letter-spacing collapsed to named steps.
- **One status-color vocabulary:** charts, quota/budget bar fills,
RUNNING/live chips, and liveness indicators all use the canonical
`--status-*` hues. Light-mode legibility fixes for red alert surfaces
that used dark-tuned text classes.
- **One switch:** `ToggleSwitch` restyled to the registry capsule form,
second hand-rolled implementation removed, and all call sites unified.
- **Docs:** `DESIGN.md` is the design contract; `doc/design/` holds
audit reports, decision logs, and updated guidance for external baseline
review/update workflows.
- Dead code removed (`agentStatusBadge` duplicate map), byte-identical
contrast constants consolidated, semantic renames
(`--project-seed`/`--project-none`, `--liveness-blue`).

## Verification

- `pnpm check:token-gates` — 3/3 gates CLEAN during the design-system
run
- `pnpm typecheck` && `pnpm --filter @paperclipai/ui build` — green
during the design-system run
- `node --test scripts/__tests__/storybook-visual-baseline.test.mjs` —
pass after external-baseline rework
- `pnpm exec tsc --noEmit --pretty false --module NodeNext
--moduleResolution NodeNext --target ES2022 --types
node,@playwright/test tests/storybook-visual/playwright.config.ts
tests/storybook-visual/storybook-visual.spec.ts` — pass after
external-baseline rework
- `git diff --check origin/pr/9134..HEAD` — pass after external-baseline
rework
- `find tests/storybook-visual -type f -name '*.png' -print | wc -l` —
`0`
- `node scripts/storybook-visual-baseline.mjs verify` — intentionally
fails closed until the first trusted maintainer publishes the baseline
archive and updates `baseline-manifest.json`

## Risks

- **Large but shallow:** the PR still touches many UI files due to
mechanical token extraction and retune work, but committed PNG snapshot
churn has been removed from the branch.
- **Baseline publication required before the visual suite can pass in
clean clones:** the manifest currently has placeholder archive metadata.
A trusted maintainer must publish the first immutable archive, then
update `baseline-manifest.json`.
- **Rendering platform variance:** the external baseline should be
captured in the documented Linux/Chromium environment. Future CI runs
verify against the pinned archive and fail closed on checksum/count
mismatch.
- **Visual CI is opt-in while stabilizing:** add the `storybook-visual`
label or dispatch the workflow manually to produce downloadable
Playwright report/test-result artifacts.
- **Scheduled follow-ups, deliberately out of scope:** Tailwind palette
classes map to semantic tokens in a dedicated pass; card/pill component
consolidation; ESLint ratchet. Tracked in
`doc/design/DECISION-SHEET.md`.

## Model Used

Claude Fable 5 (Anthropic, `claude-fable-5`, Mythos-class tier) with
extended thinking, running in Claude Code with tool use; mechanical
phases delegated to Claude Sonnet subagents. Follow-up external-baseline
rework assisted by OpenAI Codex (`gpt-5` coding agent with repository,
terminal, and GitHub tool use). All bulk rewrites executed via
deterministic, idempotent scripts committed in `scripts/`; intentional
visual changes were human-reviewed on screenshot contact sheets.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run targeted local verification and documented the
intentional baseline-publication failure above
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green *(pending new CI run after this
rework)*
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
*(pending review)*
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code) and
OpenAI Codex

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Dotta <bippadotta@protonmail.com>
Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-07-07 16:22:16 -05:00

381 lines
13 KiB
TypeScript

import {
forwardRef,
useImperativeHandle,
useLayoutEffect,
useRef,
useState,
type ChangeEvent,
type DragEvent as ReactDragEvent,
type KeyboardEvent as ReactKeyboardEvent,
type ReactNode,
} from "react";
import { AlertTriangle, Check, Loader2, Paperclip, Send } from "lucide-react";
import { cn } from "../lib/utils";
/**
* Shared chat composer (PAP-95a / PAP-96).
*
* One reusable input shell used by BOTH the conference room (BoardChat) and
* task comments (IssueChatThread). It is intentionally a *plain textarea* —
* **no formatting toolbar** — with attach + send. The focus state is a neutral
* border darkening (no blue focus ring) so the box reads as calm chrome in both
* surfaces.
*
* Task-only chrome — the yellow planning chip and the assignee footer — is NOT
* baked in here. It layers on through the `tone`, `leadingTools`, and
* `trailingTools` props (wired by PAP-95b). The @project/@task picker layers on
* through PAP-95f. The conference room adopts this bare: textarea + send (+ attach
* when a handler is supplied), no mode chip, since the room has no task lifecycle.
*/
export interface ChatComposerAttachment {
id: string;
name: string;
size?: number;
status: "uploading" | "attached" | "error";
error?: string;
/** True when the file was inserted inline (e.g. an image) rather than only attached. */
inline?: boolean;
}
export interface ChatComposerHandle {
focus: () => void;
}
export interface ChatComposerProps {
value: string;
onChange: (value: string) => void;
onSubmit: () => void;
placeholder?: string;
disabled?: boolean;
/** Shows the send button in a busy state and blocks resubmission. */
submitting?: boolean;
/**
* Send-key behavior.
* - `"enter"`: Enter submits, Shift+Enter inserts a newline (conference room default today).
* - `"mod-enter"`: Cmd/Ctrl+Enter submits, Enter inserts a newline (recommended unified default).
*/
submitKey?: "enter" | "mod-enter";
/** Collapse to a single visual line — strips newlines and disables wrapping (conference room). */
singleLine?: boolean;
/** Visual tone. Task issue modes tint the box for planning and ask flows. */
tone?: "standard" | "ask" | "planning";
/**
* Surface treatment (PAP-128 A / PAP-131).
* - `"card"`: opaque `bg-card` box (default).
* - `"translucent"`: the task-comments glass recipe — translucent background,
* backdrop blur, and a soft upward shadow — so chat text reads through the
* box as it scrolls behind (mirrors `IssueChatThread.tsx` composer shell).
*/
surface?: "card" | "translucent";
autoFocus?: boolean;
/** Accessible label for the send button. */
sendLabel?: string;
/**
* When provided, an attach button, drag-and-drop, and the attachment chip list
* render. The parent owns the actual upload and reflects progress back through
* the `attachments` prop. Omit entirely to render bare (no attach affordance).
*/
onAttachFiles?: (files: File[]) => void | Promise<void>;
attachments?: ChatComposerAttachment[];
attaching?: boolean;
/** Restrict the file picker (e.g. "image/*"). */
acceptFileTypes?: string;
/** Slot rendered just right of the attach button (e.g. the planning mode chip). */
leadingTools?: ReactNode;
/** Slot rendered just left of the send button (e.g. the assignee picker). */
trailingTools?: ReactNode;
/** Optional hint chip rendered under the textarea. */
hint?: ReactNode;
className?: string;
textareaClassName?: string;
/** test id for the textarea (defaults to "chat-composer-input"). */
inputTestId?: string;
}
const MAX_TEXTAREA_HEIGHT_PX = 200;
function formatAttachmentSize(bytes: number | undefined): string {
if (!bytes || bytes <= 0) return "";
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
function dragHasFiles(evt: ReactDragEvent<HTMLDivElement>): boolean {
const types = evt.dataTransfer?.types;
if (!types) return false;
return Array.from(types).includes("Files");
}
export const ChatComposer = forwardRef<ChatComposerHandle, ChatComposerProps>(function ChatComposer(
{
value,
onChange,
onSubmit,
placeholder = "Message…",
disabled = false,
submitting = false,
submitKey = "mod-enter",
singleLine = false,
tone = "standard",
surface = "card",
autoFocus = false,
sendLabel = "Send message",
onAttachFiles,
attachments = [],
attaching = false,
acceptFileTypes,
leadingTools,
trailingTools,
hint,
className,
textareaClassName,
inputTestId = "chat-composer-input",
},
forwardedRef,
) {
const textareaRef = useRef<HTMLTextAreaElement>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
const dragDepthRef = useRef(0);
const [isDragOver, setIsDragOver] = useState(false);
const canAttach = typeof onAttachFiles === "function";
const isAsk = tone === "ask";
const isPlanning = tone === "planning";
const canSend = !disabled && !submitting && value.trim().length > 0;
useImperativeHandle(forwardedRef, () => ({
focus: () => textareaRef.current?.focus(),
}), []);
// Auto-grow the textarea up to a cap (multiline only). Single-line stays at
// one row and scrolls horizontally, matching the conference room today.
useLayoutEffect(() => {
const el = textareaRef.current;
if (!el || singleLine) return;
el.style.height = "auto";
el.style.height = `${Math.min(el.scrollHeight, MAX_TEXTAREA_HEIGHT_PX)}px`;
}, [value, singleLine]);
function handleChange(evt: ChangeEvent<HTMLTextAreaElement>) {
const next = singleLine ? evt.target.value.replace(/\r?\n/g, " ") : evt.target.value;
onChange(next);
}
function handleKeyDown(evt: ReactKeyboardEvent<HTMLTextAreaElement>) {
if (evt.key !== "Enter") return;
const wantsSubmit =
submitKey === "mod-enter"
? evt.metaKey || evt.ctrlKey
: !evt.shiftKey && !evt.metaKey && !evt.ctrlKey;
if (!wantsSubmit) return;
evt.preventDefault();
if (canSend) onSubmit();
}
function triggerFilePicker() {
fileInputRef.current?.click();
}
async function emitFiles(files: File[]) {
if (!onAttachFiles || files.length === 0) return;
await onAttachFiles(files);
}
async function handleFileInputChange(evt: ChangeEvent<HTMLInputElement>) {
const files = evt.target.files ? Array.from(evt.target.files) : [];
await emitFiles(files);
if (fileInputRef.current) fileInputRef.current.value = "";
}
function resetDrag() {
dragDepthRef.current = 0;
setIsDragOver(false);
}
function handleDragEnter(evt: ReactDragEvent<HTMLDivElement>) {
if (!canAttach || !dragHasFiles(evt)) return;
evt.preventDefault();
dragDepthRef.current += 1;
setIsDragOver(true);
}
function handleDragOver(evt: ReactDragEvent<HTMLDivElement>) {
if (!canAttach || !dragHasFiles(evt)) return;
evt.preventDefault();
evt.dataTransfer.dropEffect = "copy";
}
function handleDragLeave(evt: ReactDragEvent<HTMLDivElement>) {
if (!canAttach || !dragHasFiles(evt)) return;
evt.preventDefault();
dragDepthRef.current = Math.max(0, dragDepthRef.current - 1);
if (dragDepthRef.current === 0) setIsDragOver(false);
}
function handleDrop(evt: ReactDragEvent<HTMLDivElement>) {
if (!canAttach || !dragHasFiles(evt)) return;
evt.preventDefault();
resetDrag();
const files = evt.dataTransfer?.files ? Array.from(evt.dataTransfer.files) : [];
void emitFiles(files);
}
return (
<div
data-testid="chat-composer"
data-tone={tone}
data-surface={surface}
className={cn(
"relative rounded-xl border px-3 pt-2.5 pb-2 transition-colors duration-150 focus-within:border-muted-foreground/40",
// Surface: opaque card vs the task glass recipe (IssueChatThread.tsx shell).
surface === "translucent"
? "border-border/70 bg-background/95 shadow-(--shadow-extract-4) backdrop-blur supports-[backdrop-filter]:bg-background/85 dark:shadow-(--shadow-extract-5)"
: "border-border bg-card",
// No blue focus ring — neutral border darkening only.
isAsk &&
"border-sky-500/55 bg-sky-50/50 focus-within:border-sky-500/70 dark:border-sky-500/50 dark:bg-sky-500/[0.07]",
isPlanning &&
"border-amber-500/55 bg-amber-50/50 focus-within:border-amber-500/70 dark:border-amber-500/50 dark:bg-amber-500/[0.07]",
isDragOver && canAttach && "border-muted-foreground/50 bg-accent/20",
disabled && "opacity-60",
className,
)}
onDragEnter={handleDragEnter}
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
>
{isDragOver && canAttach ? (
<div
data-testid="chat-composer-drop-overlay"
className="pointer-events-none absolute inset-1.5 z-20 flex items-center justify-center rounded-lg border border-dashed border-muted-foreground/50 bg-background/80 text-xs text-muted-foreground backdrop-blur-(--blur-1px)"
>
<span className="inline-flex items-center gap-2">
<Paperclip className="h-3.5 w-3.5" />
Drop to attach
</span>
</div>
) : null}
<textarea
ref={textareaRef}
data-testid={inputTestId}
value={value}
onChange={handleChange}
onKeyDown={handleKeyDown}
placeholder={placeholder}
disabled={disabled}
autoFocus={autoFocus}
rows={1}
wrap={singleLine ? "off" : "soft"}
className={cn(
"block min-h-(--sz-22px) w-full resize-none border-0 bg-transparent p-0 text-sm leading-6 text-foreground outline-none placeholder:text-muted-foreground focus:outline-none focus:ring-0",
singleLine
? "max-h-(--sz-22px) overflow-x-auto whitespace-nowrap"
: "max-h-(--sz-200px) overflow-y-auto",
textareaClassName,
)}
/>
{attachments.length > 0 ? (
<div
data-testid="chat-composer-attachments"
className="mt-2 space-y-1 rounded-md border border-dashed border-border/80 bg-muted/20 p-1.5"
>
{attachments.map((attachment) => {
const sizeLabel = formatAttachmentSize(attachment.size);
const statusLabel =
attachment.status === "uploading"
? "Uploading…"
: attachment.status === "error"
? attachment.error ?? "Upload failed"
: attachment.inline
? "Inserted inline"
: "Attached";
return (
<div
key={attachment.id}
className={cn(
"flex min-w-0 items-center gap-2 rounded-sm px-2 py-1 text-xs",
attachment.status === "error"
? "bg-destructive/10 text-destructive"
: "bg-background/70 text-muted-foreground",
)}
>
{attachment.status === "uploading" ? (
<Loader2 className="h-3.5 w-3.5 shrink-0 animate-spin" />
) : attachment.status === "attached" ? (
<Check className="h-3.5 w-3.5 shrink-0 text-green-600 dark:text-green-400" />
) : (
<AlertTriangle className="h-3.5 w-3.5 shrink-0" />
)}
<span className="min-w-0 flex-1 truncate font-medium text-foreground">
{attachment.name}
</span>
{sizeLabel ? <span className="shrink-0">{sizeLabel}</span> : null}
<span className="shrink-0">{statusLabel}</span>
</div>
);
})}
</div>
) : null}
{hint ? (
<div className="mt-2 inline-flex items-center rounded-full border border-border/70 bg-muted/30 px-2 py-0.5 text-(length:--text-micro) text-muted-foreground">
{hint}
</div>
) : null}
<div className="mt-2 flex items-center gap-2">
{canAttach ? (
<>
<input
ref={fileInputRef}
type="file"
accept={acceptFileTypes}
className="hidden"
onChange={handleFileInputChange}
/>
<button
type="button"
onClick={triggerFilePicker}
disabled={disabled || attaching}
aria-label="Attach files"
title="Attach files"
className="grid h-7 w-7 shrink-0 place-items-center rounded-md text-muted-foreground transition-colors duration-150 hover:bg-accent hover:text-foreground disabled:cursor-not-allowed disabled:opacity-50"
>
{attaching ? <Loader2 className="h-4 w-4 animate-spin" /> : <Paperclip className="h-4 w-4" />}
</button>
</>
) : null}
{leadingTools}
<span className="flex-1" />
{trailingTools}
<button
type="button"
onClick={() => {
if (canSend) onSubmit();
}}
disabled={!canSend}
aria-label={sendLabel}
title={sendLabel}
className={cn(
"grid h-7 w-7 shrink-0 place-items-center rounded-md transition-colors duration-150 disabled:cursor-not-allowed",
canSend
? "bg-foreground text-background hover:opacity-90"
: "bg-accent text-muted-foreground",
)}
>
{submitting ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <Send className="h-3.5 w-3.5" />}
</button>
</div>
</div>
);
});