fix(web): add error handling for copy message button#1564
Conversation
Handle navigator.clipboard.writeText() failures gracefully: - Add copyError state to track clipboard API errors - Show X icon with error color when copy fails - Reset error state after 2 seconds - Closes coleam00#1540
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdded clipboard-copy failure handling to the chat message bubble: introduced a ChangesClipboard Copy Error Handling
Estimated Code Review Effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/web/src/components/chat/MessageBubble.tsx (1)
157-171: ⚡ Quick winUntracked
setTimeoutcalls cause stale-timer races on rapid clicks.Each call to
copyMessageenqueues a freshsetTimeoutwithout cancelling any prior pending timer. On rapid repeated failures the first timer fires before the second and prematurely resetscopyError(e.g. two failures 500 ms apart — the first timer fires 1 500 ms into the second's 2 000 ms window and clears the error early). The same issue applies tocopied. The same pattern exists inHeader.tsxbut that doesn't make it correct.♻️ Proposed fix — cancel stale timers before scheduling new ones
+ const copiedTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); + const copyErrorTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); const copyMessage = (): void => { void navigator.clipboard .writeText(message.content) .then(() => { + if (copiedTimerRef.current) clearTimeout(copiedTimerRef.current); + if (copyErrorTimerRef.current) clearTimeout(copyErrorTimerRef.current); setCopied(true); setCopyError(false); - setTimeout(() => { + copiedTimerRef.current = setTimeout(() => { setCopied(false); }, 1500); }) .catch(() => { + setCopied(false); + if (copyErrorTimerRef.current) clearTimeout(copyErrorTimerRef.current); setCopyError(true); - setTimeout(() => { + copyErrorTimerRef.current = setTimeout(() => { setCopyError(false); }, 2000); }); };The
useRefimport is already present on Line 1. The timer refs should be declared alongside the other state variables (around Line 143).🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/web/src/components/chat/MessageBubble.tsx` around lines 157 - 171, The copyMessage handler enqueues setTimeouts without cancelling previous timers causing stale-timer races for copied and copyError; fix it by adding two refs (e.g. copiedTimerRef and copyErrorTimerRef via useRef<number | null>) near the existing state declarations, clear any existing timer (clearTimeout on copiedTimerRef/current or copyErrorTimerRef/current) before creating a new setTimeout, store the new timer id in the corresponding ref, and also clear both timers in a cleanup effect (useEffect cleanup) to avoid leaks; update the promise .then and .catch branches to use these refs instead of raw setTimeout so clicks won’t prematurely reset state.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/web/src/components/chat/MessageBubble.tsx`:
- Around line 166-171: In MessageBubble's copy Promise .catch() handler, ensure
the copied state is reset so the error icon/label can appear; update the
.catch() to call setCopied(false) before (or alongside) setCopyError(true) and
keep the existing timeout to clear copyError (and ensure the timeout doesn't
rely on copied). Locate the .catch() in the copy handler where
setCopyError(true) is called and add a setCopied(false) there so failed copies
won't leave the success state visible.
---
Nitpick comments:
In `@packages/web/src/components/chat/MessageBubble.tsx`:
- Around line 157-171: The copyMessage handler enqueues setTimeouts without
cancelling previous timers causing stale-timer races for copied and copyError;
fix it by adding two refs (e.g. copiedTimerRef and copyErrorTimerRef via
useRef<number | null>) near the existing state declarations, clear any existing
timer (clearTimeout on copiedTimerRef/current or copyErrorTimerRef/current)
before creating a new setTimeout, store the new timer id in the corresponding
ref, and also clear both timers in a cleanup effect (useEffect cleanup) to avoid
leaks; update the promise .then and .catch branches to use these refs instead of
raw setTimeout so clicks won’t prematurely reset state.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 3b707ada-c62e-4fea-b1dd-8d82193bb3bb
📒 Files selected for processing (1)
packages/web/src/components/chat/MessageBubble.tsx
| .catch(() => { | ||
| setCopyError(true); | ||
| setTimeout(() => { | ||
| setCopyError(false); | ||
| }, 2000); | ||
| }); |
There was a problem hiding this comment.
.catch() must reset copied to avoid suppressing the error icon.
When a copy succeeds the copied flag stays true for 1 500 ms. If the user clicks again within that window and the second copy fails, the .catch() handler sets copyError = true but leaves copied = true. Because both the icon ternary and the aria-label evaluate copied first, the UI still shows the checkmark and announces "Copied" — the X icon and "Failed to copy" label never appear. This directly defeats the purpose of this PR.
🐛 Proposed fix
.catch(() => {
+ setCopied(false);
setCopyError(true);
setTimeout(() => {
setCopyError(false);
}, 2000);
});📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| .catch(() => { | |
| setCopyError(true); | |
| setTimeout(() => { | |
| setCopyError(false); | |
| }, 2000); | |
| }); | |
| .catch(() => { | |
| setCopied(false); | |
| setCopyError(true); | |
| setTimeout(() => { | |
| setCopyError(false); | |
| }, 2000); | |
| }); |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/web/src/components/chat/MessageBubble.tsx` around lines 166 - 171,
In MessageBubble's copy Promise .catch() handler, ensure the copied state is
reset so the error icon/label can appear; update the .catch() to call
setCopied(false) before (or alongside) setCopyError(true) and keep the existing
timeout to clear copyError (and ensure the timeout doesn't rely on copied).
Locate the .catch() in the copy handler where setCopyError(true) is called and
add a setCopied(false) there so failed copies won't leave the success state
visible.
Add docstrings to MessageBubbleRaw component and copyMessage function to satisfy 80% docstring coverage requirement. Closes coleam00#1540
Review SummaryVerdict: minor-fixes-needed Your PR adds a useful UX improvement — surfacing clipboard copy failures to users with an X icon — and the implementation is clean. Two JSDoc comment blocks added to Suggested fixes
Minor / nice-to-have
Compliments
Reviewed via maintainer-review-pr workflow (Pi/Minimax). Aspects run: code-review, error-handling, test-coverage, comment-quality. |
…logging Per Wirasm review - remove redundant JSDoc blocks that restate code. The function names and types already convey what the code does. Add console.debug for clipboard errors for debugging support. Closes coleam00#1540
|
Thanks for the review! Applied all fixes:
Thanks for the detailed feedback! 🙏 |
* fix(web): add error handling for copy message button Handle navigator.clipboard.writeText() failures gracefully: - Add copyError state to track clipboard API errors - Show X icon with error color when copy fails - Reset error state after 2 seconds - Closes coleam00#1540 * docs(MessageBubble): add JSDoc comments for CodeRabbit coverage Add docstrings to MessageBubbleRaw component and copyMessage function to satisfy 80% docstring coverage requirement. Closes coleam00#1540 * refactor(MessageBubble): remove JSDoc comments per policy, add debug logging Per Wirasm review - remove redundant JSDoc blocks that restate code. The function names and types already convey what the code does. Add console.debug for clipboard errors for debugging support. Closes coleam00#1540
Summary
Describe this PR in 2-5 bullets:
UX Journey
Before
After
Architecture Diagram
Before
After
Connection inventory (list every module-to-module edge, mark changes):
Label Snapshot
Change Metadata
Linked Issue
Validation Evidence (required)
Commands and result summary:
Security Impact (required)
Compatibility / Migration
Human Verification (required)
What was personally validated beyond CI:
Side Effects / Blast Radius (required)
Rollback Plan (required)
git revert <commit-sha>Risks and Mitigations
None - this is a simple error handling addition that improves UX without changing existing behavior for successful cases.
Summary by CodeRabbit