|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | + |
| 3 | +import styles from './styles.module.scss'; |
| 4 | + |
| 5 | +const CHATGPT_URL = 'https://chatgpt.com/?prompt='; |
| 6 | +const CLAUDE_URL = 'https://claude.ai/new?q='; |
| 7 | + |
| 8 | +const buildPrompt = (absoluteMdUrl, pageTitle) => |
| 9 | + `Read ${absoluteMdUrl} and help me with questions about "${pageTitle}".`; |
| 10 | + |
| 11 | +const toAbsolute = (mdUrl) => { |
| 12 | + if (typeof window === 'undefined') return mdUrl; |
| 13 | + return new URL(mdUrl, window.location.origin).toString(); |
| 14 | +}; |
| 15 | + |
| 16 | +const CopyIcon = () => ( |
| 17 | + <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"> |
| 18 | + <rect x="9" y="9" width="13" height="13" rx="2" ry="2" /> |
| 19 | + <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" /> |
| 20 | + </svg> |
| 21 | +); |
| 22 | + |
| 23 | +const CheckIcon = () => ( |
| 24 | + <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"> |
| 25 | + <polyline points="20 6 9 17 4 12" /> |
| 26 | + </svg> |
| 27 | +); |
| 28 | + |
| 29 | +const ChevronIcon = () => ( |
| 30 | + <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"> |
| 31 | + <polyline points="6 9 12 15 18 9" /> |
| 32 | + </svg> |
| 33 | +); |
| 34 | + |
| 35 | +const ExternalIcon = () => ( |
| 36 | + <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"> |
| 37 | + <path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" /> |
| 38 | + <polyline points="15 3 21 3 21 9" /> |
| 39 | + <line x1="10" y1="14" x2="21" y2="3" /> |
| 40 | + </svg> |
| 41 | +); |
| 42 | + |
| 43 | +export default function CopyPageButton({ mdUrl, pageTitle }) { |
| 44 | + const [copied, setCopied] = useState(false); |
| 45 | + const [open, setOpen] = useState(false); |
| 46 | + const wrapperRef = useRef(null); |
| 47 | + const copiedTimer = useRef(null); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (!open) return undefined; |
| 51 | + const onMouseDown = (e) => { |
| 52 | + if (wrapperRef.current && !wrapperRef.current.contains(e.target)) { |
| 53 | + setOpen(false); |
| 54 | + } |
| 55 | + }; |
| 56 | + const onKey = (e) => { |
| 57 | + if (e.key === 'Escape') setOpen(false); |
| 58 | + }; |
| 59 | + document.addEventListener('mousedown', onMouseDown); |
| 60 | + document.addEventListener('keydown', onKey); |
| 61 | + return () => { |
| 62 | + document.removeEventListener('mousedown', onMouseDown); |
| 63 | + document.removeEventListener('keydown', onKey); |
| 64 | + }; |
| 65 | + }, [open]); |
| 66 | + |
| 67 | + useEffect(() => () => { |
| 68 | + if (copiedTimer.current) clearTimeout(copiedTimer.current); |
| 69 | + }, []); |
| 70 | + |
| 71 | + const copyMarkdown = async () => { |
| 72 | + try { |
| 73 | + const res = await fetch(mdUrl); |
| 74 | + if (!res.ok) throw new Error(`Fetch failed: ${res.status}`); |
| 75 | + const text = await res.text(); |
| 76 | + await navigator.clipboard.writeText(text); |
| 77 | + setCopied(true); |
| 78 | + if (copiedTimer.current) clearTimeout(copiedTimer.current); |
| 79 | + copiedTimer.current = setTimeout(() => setCopied(false), 2000); |
| 80 | + } catch (err) { |
| 81 | + console.error('CopyPageButton: failed to copy markdown', err); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + const viewAsMarkdown = () => { |
| 86 | + setOpen(false); |
| 87 | + window.open(mdUrl, '_blank', 'noopener,noreferrer'); |
| 88 | + }; |
| 89 | + |
| 90 | + const openInLLM = (baseUrl) => { |
| 91 | + setOpen(false); |
| 92 | + const prompt = buildPrompt(toAbsolute(mdUrl), pageTitle); |
| 93 | + window.open(baseUrl + encodeURIComponent(prompt), '_blank', 'noopener,noreferrer'); |
| 94 | + }; |
| 95 | + |
| 96 | + return ( |
| 97 | + <div className={styles.wrapper} ref={wrapperRef}> |
| 98 | + <button |
| 99 | + type="button" |
| 100 | + className={styles.mainButton} |
| 101 | + onClick={copyMarkdown} |
| 102 | + aria-label={copied ? 'Page markdown copied' : 'Copy page as markdown'} |
| 103 | + > |
| 104 | + <span className={styles.icon}>{copied ? <CheckIcon /> : <CopyIcon />}</span> |
| 105 | + <span className={styles.label}>{copied ? 'Copied!' : 'Copy page'}</span> |
| 106 | + </button> |
| 107 | + <button |
| 108 | + type="button" |
| 109 | + className={styles.chevronButton} |
| 110 | + onClick={() => setOpen((v) => !v)} |
| 111 | + aria-haspopup="menu" |
| 112 | + aria-expanded={open} |
| 113 | + aria-label="Open page actions menu" |
| 114 | + > |
| 115 | + <ChevronIcon /> |
| 116 | + </button> |
| 117 | + {open && ( |
| 118 | + <div className={styles.menu} role="menu"> |
| 119 | + <button type="button" className={styles.menuItem} onClick={viewAsMarkdown} role="menuitem"> |
| 120 | + <span className={styles.menuIcon}><ExternalIcon /></span> |
| 121 | + <span className={styles.menuText}> |
| 122 | + <span className={styles.menuTitle}>View as Markdown</span> |
| 123 | + <span className={styles.menuDesc}>Open the raw .md in a new tab</span> |
| 124 | + </span> |
| 125 | + </button> |
| 126 | + <button type="button" className={styles.menuItem} onClick={() => openInLLM(CHATGPT_URL)} role="menuitem"> |
| 127 | + <span className={styles.menuIcon}><ExternalIcon /></span> |
| 128 | + <span className={styles.menuText}> |
| 129 | + <span className={styles.menuTitle}>Open in ChatGPT</span> |
| 130 | + <span className={styles.menuDesc}>Ask ChatGPT about this page</span> |
| 131 | + </span> |
| 132 | + </button> |
| 133 | + <button type="button" className={styles.menuItem} onClick={() => openInLLM(CLAUDE_URL)} role="menuitem"> |
| 134 | + <span className={styles.menuIcon}><ExternalIcon /></span> |
| 135 | + <span className={styles.menuText}> |
| 136 | + <span className={styles.menuTitle}>Open in Claude</span> |
| 137 | + <span className={styles.menuDesc}>Ask Claude about this page</span> |
| 138 | + </span> |
| 139 | + </button> |
| 140 | + </div> |
| 141 | + )} |
| 142 | + </div> |
| 143 | + ); |
| 144 | +} |
0 commit comments