Skip to content

fix: fall back when sidebar clipboard writes fail#1216

Open
lawrence3699 wants to merge 1 commit intomodelcontextprotocol:mainfrom
lawrence3699:fix/sidebar-copy-fallback
Open

fix: fall back when sidebar clipboard writes fail#1216
lawrence3699 wants to merge 1 commit intomodelcontextprotocol:mainfrom
lawrence3699:fix/sidebar-copy-fallback

Conversation

@lawrence3699
Copy link
Copy Markdown

Fixes #913.

Summary

  • add a Sidebar clipboard helper that falls back to document.execCommand("copy") when navigator.clipboard.writeText is rejected or unavailable
  • keep the existing success UX for both copy buttons and preserve the destructive error toast when every copy path fails
  • add regression coverage for both the fallback-success path and the full-failure path

Testing

  • npm test -- --runTestsByPath src/components/tests/Sidebar.test.tsx
  • npm run lint
  • npm run build
  • npx prettier --check client/src/components/Sidebar.tsx client/src/components/tests/Sidebar.test.tsx

Copilot AI review requested due to automatic review settings April 19, 2026 14:04
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a resilient clipboard-copy implementation in the Sidebar to fix copy failures (Issue #913) by falling back to document.execCommand("copy"), while preserving existing success/error toasts and adding regression tests.

Changes:

  • Introduce a copyTextToClipboard helper with an execCommand("copy") fallback when navigator.clipboard.writeText is unavailable or rejected.
  • Update both Sidebar copy handlers to use the helper while keeping existing UX/toast behavior.
  • Add Sidebar tests covering fallback-success and full-failure scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
client/src/components/Sidebar.tsx Adds clipboard helper + routes copy actions through it with fallback logic.
client/src/components/tests/Sidebar.test.tsx Adds regression tests for fallback success and full failure of clipboard copying.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

try {
textarea.focus();
textarea.select();
copied = document.execCommand("copy");
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fallbackCopy calls document.execCommand("copy") without checking that execCommand exists/is callable. In browsers/environments where execCommand is missing, this will throw a TypeError and surface a confusing error message (and bypass the intended "Clipboard copy failed" UX). Consider guarding with typeof document.execCommand === "function" and throwing a consistent Error("Clipboard copy failed") (or similar) when unavailable.

Suggested change
copied = document.execCommand("copy");
if (typeof document.execCommand !== "function") {
throw new Error("Clipboard copy failed");
}
try {
copied = document.execCommand("copy");
} catch {
throw new Error("Clipboard copy failed");
}

Copilot uses AI. Check for mistakes.
Comment on lines +162 to +165
if (!navigator.clipboard?.writeText) {
fallbackCopy();
return;
}
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new branch that handles navigator.clipboard.writeText being unavailable (if (!navigator.clipboard?.writeText)) isn't covered by a regression test. Adding a test case where navigator.clipboard (or writeText) is missing would help ensure the fallback path works in insecure contexts/older browsers.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Servers File Export is Broken

2 participants