Skip to content

fix(start): handle clipboard copy failures#401

Open
murongg wants to merge 1 commit into
Openpanel-dev:mainfrom
murongg:fix/clipboard-fallback
Open

fix(start): handle clipboard copy failures#401
murongg wants to merge 1 commit into
Openpanel-dev:mainfrom
murongg:fix/clipboard-fallback

Conversation

@murongg

@murongg murongg commented Jun 24, 2026

Copy link
Copy Markdown

Summary

  • Add a fallback clipboard copy path for browsers/environments where navigator.clipboard.writeText is unavailable or rejects.
  • Show an error toast instead of a success toast when all copy methods fail.

Why

The shared clipboard helper previously called navigator.clipboard.writeText(...) directly and showed a success toast immediately. In insecure contexts, permission-restricted browser shells, or other unsupported environments, that can fail silently and leave actions like Copy invite link without actually updating the clipboard.

Closes #400.

Validation

  • pnpm exec biome check apps/start/src/utils/clipboard.ts passed.

Note

I intentionally left the existing Vitest workspace setup unchanged because apps/start is currently excluded from the root Vitest workspace.

Summary by CodeRabbit

  • New Features
    • Improved copy-to-clipboard behavior with broader browser/environment support.
  • Bug Fixes
    • Clipboard writes are now attempted asynchronously and safely.
    • Added a fallback copy method when the standard clipboard API isn’t available.
    • Copy failures now show an explicit error toast with manual-copy guidance.
    • Success feedback remains available, with improved toast message handling.

@CLAassistant

CLAassistant commented Jun 24, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 364de5ce-f24c-408a-9cf2-86372a70d4ed

📥 Commits

Reviewing files that changed from the base of the PR and between dc6293f and f0d8fba.

📒 Files selected for processing (1)
  • apps/start/src/utils/clipboard.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/start/src/utils/clipboard.ts

📝 Walkthrough

Walkthrough

The clipboard utility in apps/start/src/utils/clipboard.ts is now async. It tries navigator.clipboard.writeText, falls back to a hidden textarea copy path, shows an error toast if both fail, and shows success only after a confirmed copy.

Changes

Clipboard Resilience

Layer / File(s) Summary
Async clipboard utility with textarea fallback
apps/start/src/utils/clipboard.ts
clipboard is now async and converts values to text. A new copyWithTextarea helper uses a hidden readonly textarea and document.execCommand('copy') as a fallback. The function shows an error toast when both paths fail and keeps the success toast for confirmed copies.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 The clipboard hop is neat and bright,
With fallback path and toast in sight.
If one route fails, another tries,
No silent copy under skies.
A happy hop, the text gets through!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: handling clipboard copy failures in start.
Linked Issues check ✅ Passed The clipboard helper now falls back to execCommand copy and shows failure only after both methods fail.
Out of Scope Changes check ✅ Passed The changes stay focused on clipboard copy failure handling in apps/start/src/utils/clipboard.ts.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@murongg murongg marked this pull request as ready for review June 24, 2026 08:29
@murongg murongg force-pushed the fix/clipboard-fallback branch from e7619e9 to dc6293f Compare June 24, 2026 08:33

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
apps/start/src/utils/clipboard.test.ts (1)

57-66: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert that success toast is not emitted on full failure.

Add a negative assertion so this test also guards against accidental dual-toast behavior in the failure path.

Suggested patch
   it('shows an error when copying is not available', async () => {
     execCommand.mockReturnValueOnce(false);
     setClipboardApi(undefined);

     await clipboard('invite-url');

     expect(toast.error).toHaveBeenCalledWith('Failed to copy to clipboard', {
       description: 'Please copy it manually.',
     });
+    expect(toast).not.toHaveBeenCalled();
   });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/start/src/utils/clipboard.test.ts` around lines 57 - 66, The
failure-path test in clipboard.test.ts should also verify that no success toast
is shown when copying is unavailable. Update the existing clipboard test that
exercises the missing Clipboard API/execCommand failure path to include a
negative assertion against toast.success, alongside the current toast.error
check, so the clipboard helper cannot accidentally emit both success and error
toasts.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@apps/start/src/utils/clipboard.ts`:
- Around line 19-22: In copyWithTextarea, document.execCommand('copy') can throw
and bypass the existing !copied failure handling; wrap the call so the function
returns false on exception, ensuring fire-and-forget callers still hit the
error-toast path. Keep the cleanup in the finally block intact while updating
the copyWithTextarea flow to treat both a false return and a thrown exception as
copy failure.

---

Nitpick comments:
In `@apps/start/src/utils/clipboard.test.ts`:
- Around line 57-66: The failure-path test in clipboard.test.ts should also
verify that no success toast is shown when copying is unavailable. Update the
existing clipboard test that exercises the missing Clipboard API/execCommand
failure path to include a negative assertion against toast.success, alongside
the current toast.error check, so the clipboard helper cannot accidentally emit
both success and error toasts.
🪄 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: 88a7340d-af12-4702-add4-dee257e86765

📥 Commits

Reviewing files that changed from the base of the PR and between 4ed4d39 and e7619e9.

📒 Files selected for processing (4)
  • apps/start/src/utils/clipboard.test.ts
  • apps/start/src/utils/clipboard.ts
  • apps/start/vitest.config.ts
  • vitest.workspace.ts

Comment thread apps/start/src/utils/clipboard.ts
@murongg murongg force-pushed the fix/clipboard-fallback branch from dc6293f to f0d8fba Compare June 24, 2026 09:09
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.

Clipboard copy can fail silently when Clipboard API is unavailable

2 participants