Skip to content

Commit ac1f6ee

Browse files
Improve markdown copy button with robust fallback and error handling
Co-authored-by: rahul.chhabria <[email protected]>
1 parent eee8060 commit ac1f6ee

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/components/copyForLLMButton.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,33 @@ export function CopyForLLMButton({markdownPath}: Props) {
1212
const [copied, setCopied] = useState(false);
1313

1414
async function handleClick() {
15+
let didCopy = false;
16+
17+
// First attempt: fetch markdown file and copy its contents
1518
try {
16-
// Attempt to fetch the markdown content and copy it
1719
const response = await fetch(markdownPath);
18-
const text = await response.text();
19-
await navigator.clipboard.writeText(text);
20+
if (response.ok) {
21+
const text = await response.text();
22+
await navigator.clipboard.writeText(text);
23+
didCopy = true;
24+
}
2025
} catch (err) {
21-
// Fallback: copy the markdown url itself
22-
await navigator.clipboard.writeText(window.location.origin + markdownPath);
23-
} finally {
26+
// network error handled below in fallback
27+
console.error(err);
28+
}
29+
30+
// Fallback: copy the markdown URL if first attempt failed
31+
if (!didCopy) {
32+
try {
33+
await navigator.clipboard.writeText(window.location.origin + markdownPath);
34+
didCopy = true;
35+
} catch (err) {
36+
console.error('Failed to copy markdown URL as fallback', err);
37+
}
38+
}
39+
40+
// Visual feedback only when something was actually copied
41+
if (didCopy) {
2442
setCopied(true);
2543
setTimeout(() => setCopied(false), 1200);
2644
}

0 commit comments

Comments
 (0)