Files
chatkit/dist/react/CommentThread.js
T
CatoandClaude 6451d3b9d0 feat(react): DictationField and CommentThread — any text box has the microphone (#73)
The owner's finding, 2026-11-06: every product installs the shared packages
and the text boxes people type in are still made by hand, with no microphone;
two comment systems were written in one week. This gives the two remaining
shapes of "a person types words" a component, so there is nothing to build
locally.

- DictationField: a labelled text area (or one line) with the composer's
  microphone — Web Speech, the recording fallback, the take with its timer,
  kept for a retry — appending to what is there; 16px, 44px targets.
  Optional improve={{ run, suggestions }}: one tap or say/type what to change;
  working, failure and Undo in words, the text untouched on failure. No model
  call or prompt here; run is the app's.
- CommentThread: oldest first, the box is Composer, a transport the app
  implements (post, optional remove), optimistic removal rolled back with a
  message, a refused post keeps the draft, text not markup, a sign-in link
  for a signed-out reader, times in a stated timeZone.
- VoiceBar/VoiceProblem are one implementation shared by Composer and the
  field; Composer behaves as before.
- Styles after the ChatApp boundary: the embedded sheet keeps its budget.
- test/browser-field.mjs + fixture page; render tests; README; 0.23.0.

verify: format, lint, typecheck, build, 142 unit tests, and all five browser
suites (the four existing ones unchanged) pass.


Claude-Session: https://claude.ai/code/session_019MDtmvpWNTLxXNfwUHS7MD

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-06 09:22:19 +01:00

78 lines
4.3 KiB
JavaScript

"use client";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { createElement, useId, useState } from "react";
import { Composer } from "./Composer.js";
export const DEFAULT_COMMENT_THREAD_LABELS = {
count: (n) => (n === 0 ? "No comments yet" : n === 1 ? "1 comment" : `${n} comments`),
placeholder: "Say something about this…",
ariaLabel: "Write a comment",
send: "Comment",
signIn: "Sign in to comment",
remove: "Remove",
postFailed: "The comment could not be posted. Your text is still in the box; try again.",
removeFailed: "The comment could not be removed. Try again.",
postedAs: (name) => `Posted as ${name}.`,
};
function defaultTime(locale, timeZone) {
const fmt = new Intl.DateTimeFormat(locale, {
day: "numeric",
month: "short",
hour: "2-digit",
minute: "2-digit",
timeZone: timeZone ?? "UTC",
});
return (at) => fmt.format(at);
}
/**
* The conversation under anything: an article, a page, a listing. Comments
* read oldest first, the box is chatkit's `Composer` (so it has the
* microphone, 16px text and Enter-to-send like every other box in the fleet),
* and a person can remove what they wrote. Reading needs nothing; writing
* needs `viewer`, and a signed-out reader is shown the way to sign in on the
* same screen, never a dead end.
*/
export function CommentThread({ comments, transport, viewer, signInHref, canRemove, voice, formatTime, locale, timeZone, headingLevel = 2, labels: overrides, className, }) {
const labels = { ...DEFAULT_COMMENT_THREAD_LABELS, ...overrides };
const [list, setList] = useState(() => [...comments]);
const [draft, setDraft] = useState("");
const [sending, setSending] = useState(false);
const [error, setError] = useState(null);
const headingId = useId();
const time = formatTime ?? defaultTime(locale, timeZone);
const post = async (text) => {
setError(null);
setSending(true);
try {
const created = await transport.post(text);
setList((current) => [...current, created]);
return true;
}
catch {
setError(labels.postFailed);
return false;
}
finally {
setSending(false);
}
};
const remove = async (id) => {
if (!transport.remove)
return;
setError(null);
const before = list;
setList((current) => current.filter((c) => c.id !== id));
try {
await transport.remove(id);
}
catch {
setList(before);
setError(labels.removeFailed);
}
};
const heading = createElement(`h${headingLevel}`, { id: headingId, className: "ck-comments-title" }, labels.count(list.length));
return (_jsxs("section", { className: className ? `ck-comments ${className}` : "ck-comments", "aria-labelledby": headingId, children: [heading, list.length > 0 && (_jsx("ol", { className: "ck-comment-list", children: list.map((c) => {
const at = new Date(c.createdAt);
return (_jsxs("li", { className: "ck-comment", children: [_jsxs("p", { className: "ck-comment-meta", children: [_jsx("strong", { children: c.author }), _jsx("span", { "aria-hidden": true, children: " \u00B7 " }), _jsx("time", { dateTime: at.toISOString(), children: time(at) }), transport.remove && canRemove?.(c) && (_jsxs(_Fragment, { children: [_jsx("span", { "aria-hidden": true, children: " \u00B7 " }), _jsx("button", { type: "button", className: "ck-comment-remove", onClick: () => void remove(c.id), children: labels.remove })] }))] }), _jsx("p", { className: "ck-comment-body", children: c.body })] }, c.id));
}) })), error && (_jsx("p", { className: "ck-problem", role: "alert", children: _jsx("span", { children: error }) })), viewer ? (_jsx("div", { className: "ck-comment-box", children: _jsx(Composer, { onSend: post, placeholder: labels.placeholder, ariaLabel: labels.ariaLabel, sending: sending, value: draft, onValueChange: setDraft, attach: false, voice: voice, labels: { send: labels.send }, hint: labels.postedAs(viewer.name) }) })) : (signInHref && (_jsx("p", { className: "ck-comment-signin", children: _jsx("a", { className: "ck-comment-signin-link", href: signInHref, children: labels.signIn }) })))] }));
}
//# sourceMappingURL=CommentThread.js.map