fix: fall back to execCommand when clipboard API is unavailable#598
Draft
sudocarlos wants to merge 1 commit into
Draft
fix: fall back to execCommand when clipboard API is unavailable#598sudocarlos wants to merge 1 commit into
sudocarlos wants to merge 1 commit into
Conversation
navigator.clipboard only exists in secure contexts (HTTPS or
localhost), which self-hosted Headplane is often not served over.
Calling writeText on it threw an uncaught TypeError and silently
failed to copy, in the code block, copyable attribute, and machine
IP address menu.
Add a copyToClipboard helper that falls back to the legacy
document.execCommand("copy") approach and reports success/failure
so callers can toast accordingly.
closes tale#597
Author
Code ReviewReviewed the diff directly (the automated code-reviewer subagent had no shell/browser access in this session, so I did this pass manually). SummaryClean, targeted fix. BlockersNone. WarningsNone. Suggestions (non-blocking)
Import ordering, |
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.
What
Clicking any copy-to-clipboard action (code block, copyable attribute value, or the IP address menu on the Machines page) threw an uncaught
TypeError: Cannot read properties of undefined (reading 'writeText')in the browser console and silently failed to copy anything.Why
navigator.clipboardonly exists in secure contexts (HTTPS orlocalhost). Headplane is frequently self-hosted and accessed over plain HTTP (LAN, reverse proxy without TLS to the browser, etc.), sonavigator.clipboardisundefinedthere, and calling.writeText(...)on it threw instead of failing gracefully or falling back to a legacy copy method.Fix
copyToClipboard(text)inapp/utils/clipboard.ts: triesnavigator.clipboard.writeTextwhen available, and falls back to the legacydocument.execCommand("copy")approach (via a temporary off-screen<textarea>) when the Clipboard API is unavailable or the write fails. Returnstrue/falseinstead of throwing.code-block.tsx,attribute.tsx,machine-row.tsx) to use the new helper and toast a "Failed to copy ..." message on failure instead of crashing.Testing
pnpm run typecheckandpnpm run lintpass.navigator.clipboardis unset.closes #597