feat(chat): copy the AI answer from the message action bar - #16
Merged
Conversation
Adds a copy button next to the existing export/like/dislike actions. It copies the answer's markdown — the same content the PDF export uses. The i18n keys already existed and were unused: chat.copyAnswer, common.copied, common.copyFailed. No new strings. ## Why this isn't a one-line navigator.clipboard call The async Clipboard API only exists in a **secure context**. Customer deployments are served over plain HTTP on an internal address (http://192.168.80.3:3001), where `navigator.clipboard` is `undefined` and a direct call throws a TypeError. That failure mode is the worst kind: the button reacts, nothing is reported, and the clipboard still holds whatever was there before — so the user pastes stale content believing the copy worked. `lib/clipboard.ts` therefore falls back to `document.execCommand("copy")` (deprecated, but still the only path available outside a secure context) and, crucially, **returns false when it genuinely cannot copy** so the caller can say so. Two details that make the fallback actually work: - The textarea is moved off-screen rather than `display: none` — a hidden element cannot be selected, so the copy silently fails. - Focus is restored afterwards, or the user loses their place in whatever input they were typing in. The secure-context check also guards a subtler case: some browsers keep the `clipboard` object on HTTP but reject every call. Without the check we'd produce a rejected promise and a console error on every single copy before falling back. ## Verification - 268 tests pass (10 new) - 7 mutations, all red: no fallback / no secure-context check / fallback lies about success / textarea left in the DOM / focus not restored / `display:none` instead of off-screen / empty content still copied - One mutation initially survived — "no secure-context check". That test only asserted the end result, which is identical either way. It now asserts `writeText` was **not called**. - tsc: non-TS5097 errors 112, unchanged from baseline. The one TS5097 is the `.ts` import extension every other test file in this repo also has. ## Not verified Rendering was not checked in a browser. The local dev server on :3000 runs the private repo, and the chat view needs an authenticated session. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a copy button next to the existing export / like / dislike actions. It copies the answer's markdown — the same content the PDF export uses. Clicking it swaps the icon to a check for two seconds and raises a toast.
The three i18n keys already existed in
chat.json/common.jsonand had no callers:chat.copyAnswer,common.copied,common.copyFailed. This change just puts them to use — no new strings.Why this isn't a one-line
navigator.clipboardcallThe async Clipboard API only exists in a secure context. Customer deployments are served over plain HTTP on an internal address (
http://192.168.80.3:3001), wherenavigator.clipboardisundefinedand calling it throws aTypeError.That is the worst kind of failure: the button reacts, nothing is reported, and the clipboard still holds whatever was there before — so the user pastes stale content believing the copy worked.
lib/clipboard.tstherefore falls back todocument.execCommand("copy")— deprecated, but still the only path available outside a secure context — and, crucially, returnsfalsewhen it genuinely cannot copy, so the caller surfaces an error instead of pretending.Two details that decide whether the fallback works at all:
display: none. A hidden element cannot be selected, so the copy fails silently.The secure-context check also guards a subtler case: some browsers keep the
clipboardobject on HTTP but reject every call. Without the check, each copy would produce a rejected promise and a console error before falling back.Verification
display:noneinstead of off-screen / empty content still copiedwriteTextwas not called.tsc: non-TS5097 errors 112, unchanged from baseline. The single TS5097 is the.tsimport extension that every other test file in this repo also carries.Not verified
Rendering was not checked in a browser: the local dev server on :3000 runs the private repo, and the chat view requires an authenticated session.
🤖 Generated with Claude Code