({ markdownUrl }: CopyPageProps)
| 84 | |
| 85 | /** a split button: copy the page as Markdown, or pick another way to read it */ |
| 86 | export const CopyPage = ({ markdownUrl }: CopyPageProps) => { |
| 87 | const { result, copy } = useCopyMarkdown(markdownUrl) |
| 88 | const [open, setOpen] = useState(false) |
| 89 | const root = useRef<HTMLDivElement>(null) |
| 90 | const toggle = useRef<HTMLButtonElement>(null) |
| 91 | const menu = useRef<HTMLDivElement>(null) |
| 92 | const menuId = useId() |
| 93 | |
| 94 | const items = () => [...(menu.current?.querySelectorAll("a") ?? [])] |
| 95 | |
| 96 | const close = (refocus: boolean) => { |
| 97 | setOpen(false) |
| 98 | if (refocus) toggle.current?.focus() |
| 99 | } |
| 100 | |
| 101 | useEffect(() => { |
| 102 | if (!open) return |
| 103 | items()[0]?.focus({ preventScroll: true }) |
| 104 | const onPointerDown = (e: PointerEvent) => { |
| 105 | if (!root.current?.contains(e.target as Node)) setOpen(false) |
| 106 | } |
| 107 | document.addEventListener("pointerdown", onPointerDown) |
| 108 | return () => document.removeEventListener("pointerdown", onPointerDown) |
| 109 | }, [open]) |
| 110 | |
| 111 | const prompt = `Read https://arktype.io${markdownUrl}, I want to ask questions about it.` |
| 112 | |
| 113 | const links = [ |
| 114 | { name: "View as Markdown", hint: markdownUrl, href: markdownUrl }, |
| 115 | { |
| 116 | name: "Open in Claude", |
| 117 | hint: "ask about this page", |
| 118 | href: `https://claude.ai/new?${new URLSearchParams({ q: prompt })}` |
| 119 | }, |
| 120 | { |
| 121 | name: "Open in ChatGPT", |
| 122 | hint: "ask about this page", |
| 123 | href: `https://chatgpt.com/?${new URLSearchParams({ prompt, hints: "search" })}` |
| 124 | }, |
| 125 | { |
| 126 | name: "Open in Cursor", |
| 127 | hint: "add as context", |
| 128 | href: `https://cursor.com/link/prompt?${new URLSearchParams({ text: prompt })}` |
| 129 | } |
| 130 | ] |
| 131 | |
| 132 | return ( |
| 133 | <div className="copy-page" ref={root}> |
| 134 | <button |
| 135 | type="button" |
| 136 | className="cp-main" |
| 137 | aria-label="Copy page as Markdown" |
| 138 | onClick={copy} |
| 139 | > |
| 140 | <CopyFace result={result} /> |
| 141 | </button> |
| 142 | <button |
| 143 | ref={toggle} |
nothing calls this directly
no test coverage detected