-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: fall back when sidebar clipboard writes fail #1216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,6 +135,46 @@ const Sidebar = ({ | |
| [toast], | ||
| ); | ||
|
|
||
| const copyTextToClipboard = useCallback(async (text: string) => { | ||
| const fallbackCopy = () => { | ||
| const textarea = document.createElement("textarea"); | ||
| textarea.value = text; | ||
| textarea.setAttribute("readonly", ""); | ||
| textarea.style.position = "fixed"; | ||
| textarea.style.opacity = "0"; | ||
| textarea.style.pointerEvents = "none"; | ||
|
|
||
| document.body.appendChild(textarea); | ||
| let copied = false; | ||
| try { | ||
| textarea.focus(); | ||
| textarea.select(); | ||
| copied = document.execCommand("copy"); | ||
| } finally { | ||
| document.body.removeChild(textarea); | ||
| } | ||
|
|
||
| if (!copied) { | ||
| throw new Error("Clipboard copy failed"); | ||
| } | ||
| }; | ||
|
|
||
| if (!navigator.clipboard?.writeText) { | ||
| fallbackCopy(); | ||
| return; | ||
| } | ||
|
Comment on lines
+162
to
+165
|
||
|
|
||
| try { | ||
| await navigator.clipboard.writeText(text); | ||
| } catch (error) { | ||
| try { | ||
| fallbackCopy(); | ||
| } catch (fallbackError) { | ||
| throw fallbackError instanceof Error ? fallbackError : error; | ||
| } | ||
| } | ||
| }, []); | ||
|
|
||
| // Shared utility function to generate server config | ||
| const generateServerConfig = useCallback(() => { | ||
| if (transportType === "stdio") { | ||
|
|
@@ -183,8 +223,7 @@ const Sidebar = ({ | |
| const handleCopyServerEntry = useCallback(() => { | ||
| try { | ||
| const configJson = generateMCPServerEntry(); | ||
| navigator.clipboard | ||
| .writeText(configJson) | ||
| void copyTextToClipboard(configJson) | ||
| .then(() => { | ||
| setCopiedServerEntry(true); | ||
|
|
||
|
|
@@ -202,19 +241,22 @@ const Sidebar = ({ | |
| setCopiedServerEntry(false); | ||
| }, 2000); | ||
| }) | ||
| .catch((error) => { | ||
| reportError(error); | ||
| }); | ||
| .catch(reportError); | ||
| } catch (error) { | ||
| reportError(error); | ||
| } | ||
| }, [generateMCPServerEntry, transportType, toast, reportError]); | ||
| }, [ | ||
| copyTextToClipboard, | ||
| generateMCPServerEntry, | ||
| transportType, | ||
| toast, | ||
| reportError, | ||
| ]); | ||
|
|
||
| const handleCopyServerFile = useCallback(() => { | ||
| try { | ||
| const configJson = generateMCPServerFile(); | ||
| navigator.clipboard | ||
| .writeText(configJson) | ||
| void copyTextToClipboard(configJson) | ||
| .then(() => { | ||
| setCopiedServerFile(true); | ||
|
|
||
|
|
@@ -228,13 +270,11 @@ const Sidebar = ({ | |
| setCopiedServerFile(false); | ||
| }, 2000); | ||
| }) | ||
| .catch((error) => { | ||
| reportError(error); | ||
| }); | ||
| .catch(reportError); | ||
| } catch (error) { | ||
| reportError(error); | ||
| } | ||
| }, [generateMCPServerFile, toast, reportError]); | ||
| }, [copyTextToClipboard, generateMCPServerFile, toast, reportError]); | ||
|
|
||
| return ( | ||
| <div className="bg-card border-r border-border flex flex-col h-full"> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fallbackCopycallsdocument.execCommand("copy")without checking thatexecCommandexists/is callable. In browsers/environments whereexecCommandis missing, this will throw a TypeError and surface a confusing error message (and bypass the intended "Clipboard copy failed" UX). Consider guarding withtypeof document.execCommand === "function"and throwing a consistentError("Clipboard copy failed")(or similar) when unavailable.