Skip to content

Commit 2f164dd

Browse files
fix url fetch gen logic to take multiple origins into account
1 parent 6dbf009 commit 2f164dd

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/components/copyMarkdownButton.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface CopyMarkdownButtonProps {
1616
export function CopyMarkdownButton({pathname}: CopyMarkdownButtonProps) {
1717
const [isLoading, setIsLoading] = useState(false);
1818
const [copied, setCopied] = useState(false);
19+
const [error, setError] = useState(false);
1920
const [isOpen, setIsOpen] = useState(false);
2021
const [isMounted, setIsMounted] = useState(false);
2122
const buttonRef = useRef<HTMLDivElement>(null);
@@ -25,21 +26,26 @@ export function CopyMarkdownButton({pathname}: CopyMarkdownButtonProps) {
2526
const copyMarkdownToClipboard = async () => {
2627
setIsLoading(true);
2728
setCopied(false);
29+
setError(false);
2830
setIsOpen(false);
2931

3032
emit('Copy Page', {props: {page: pathname, source: 'copy_button'}});
3133

3234
try {
33-
const response = await fetch(`https://docs.sentry.io/${pathname}.md`);
35+
// This doesn't work on local development since we need the generated markdown
36+
// files, and we need to be aware of the origin since we have two different origins.
37+
const response = await fetch(`${window.location.origin}/${pathname}.md`);
3438
if (!response.ok) {
3539
throw new Error(`Failed to fetch markdown content: ${response.status}`);
3640
}
3741

3842
await navigator.clipboard.writeText(await response.text());
3943
setCopied(true);
4044
setTimeout(() => setCopied(false), 2000);
41-
} catch (error) {
42-
console.error('Failed to copy markdown to clipboard:', error);
45+
} catch (err) {
46+
console.error('Failed to copy markdown to clipboard:', err);
47+
setError(true);
48+
setTimeout(() => setError(false), 3000);
4349
} finally {
4450
setIsLoading(false);
4551
}
@@ -96,12 +102,12 @@ export function CopyMarkdownButton({pathname}: CopyMarkdownButtonProps) {
96102
<div className="inline-flex items-center h-9 border border-gray-200 rounded-full overflow-hidden bg-white">
97103
<button
98104
onClick={copyMarkdownToClipboard}
99-
className={`${buttonClass} gap-2 px-3.5 text-sm font-medium disabled:opacity-50`}
105+
className={`${buttonClass} gap-2 px-3.5 text-sm font-medium disabled:opacity-50 ${error ? 'text-red-600' : ''}`}
100106
style={{borderRadius: '9999px 0 0 9999px'}}
101107
disabled={isLoading}
102108
>
103109
<Clipboard size={16} />
104-
<span>{copied ? 'Copied!' : 'Copy page'}</span>
110+
<span>{error ? 'Failed to copy' : copied ? 'Copied!' : 'Copy page'}</span>
105111
</button>
106112

107113
<div className="w-px h-full bg-gray-200" />
@@ -137,8 +143,12 @@ export function CopyMarkdownButton({pathname}: CopyMarkdownButtonProps) {
137143
<Clipboard size={14} />
138144
</div>
139145
<div className="flex-1">
140-
<div className="font-medium text-gray-900 text-sm leading-5">Copy page</div>
141-
<div className="text-xs text-gray-500 leading-4">Copy page as Markdown for LLMs</div>
146+
<div className={`font-medium text-sm leading-5 ${error ? 'text-red-600' : 'text-gray-900'}`}>
147+
{error ? 'Failed to copy' : 'Copy page'}
148+
</div>
149+
<div className="text-xs text-gray-500 leading-4">
150+
{error ? 'Network error - please try again' : 'Copy page as Markdown for LLMs'}
151+
</div>
142152
</div>
143153
</button>
144154

0 commit comments

Comments
 (0)