@@ -11,8 +11,9 @@ import {
1111 DropdownItem ,
1212} from './Dropdown'
1313import { getPackageManager } from '~/utils/markdown/installCommand'
14+ import * as Sentry from '@sentry/tanstackstart-react'
1415import {
15- copyTextToClipboard ,
16+ copyTextPromiseToClipboard ,
1617 openPopupWindow ,
1718 useTemporaryFlag ,
1819} from '~/utils/browser-effects'
@@ -150,72 +151,87 @@ export function CopyPageDropdown({
150151 ? `https://raw.githubusercontent.com/${ repo } /${ branch } /${ filePath } `
151152 : pageMarkdownUrl
152153
153- const handleCopyPage = async ( ) => {
154+ // Resolve the text to copy (raw content, cache, fetched markdown, or fallbacks).
155+ const resolveCopyContent = async ( ) : Promise < {
156+ content : string
157+ source ?: string
158+ } > => {
154159 if ( rawContent ) {
155- await copyTextToClipboard ( rawContent )
156- copied . trigger ( )
157- notify (
158- < div >
159- < div className = "font-medium" > Copied to clipboard</ div >
160- </ div > ,
161- { id : 'page-copied' } ,
162- )
163- return
160+ return { content : rawContent }
164161 }
165162
166163 const urlToFetch = sourceMarkdownUrl
167164 const cached = markdownCache . get ( urlToFetch )
168165
169- const copyContent = async ( content : string , source : string ) => {
170- await copyTextToClipboard ( content )
171- copied . trigger ( )
172- notify (
173- < div >
174- < div className = "font-medium" > Copied to clipboard</ div >
175- < div className = "text-xs text-gray-500 dark:text-gray-400" >
176- { source }
177- </ div >
178- </ div > ,
179- { id : 'page-copied' } ,
180- )
181- }
182-
183166 if ( cached ) {
184- await copyContent ( cached , 'Markdown content copied from cache' )
185- return
167+ return { content : cached , source : 'Markdown content copied from cache' }
186168 }
187169
188170 try {
189171 const response = await fetch ( urlToFetch )
190172 if ( ! response . ok ) throw new Error ( 'Failed to fetch' )
191173 const content = await response . text ( )
192174 markdownCache . set ( urlToFetch , content )
193- await copyContent (
175+ return {
194176 content,
195- repo === 'tanstack/tanstack.com' && hasPageMarkdownEndpoint
196- ? 'Markdown content copied from markdown endpoint'
197- : 'Markdown content copied from GitHub' ,
198- )
177+ source :
178+ repo === 'tanstack/tanstack.com' && hasPageMarkdownEndpoint
179+ ? 'Markdown content copied from markdown endpoint'
180+ : 'Markdown content copied from GitHub' ,
181+ }
199182 } catch {
200183 // Fallback: try to copy current page content if available
201184 const pageContent =
202185 document . querySelector ( '.styled-markdown-content' ) ?. textContent || ''
203186 if ( pageContent ) {
204- await copyContent ( pageContent , 'Copied rendered page content' )
205- } else {
206- // Last resort: copy the URL
207- await copyTextToClipboard ( window . location . href )
208- copied . trigger ( )
209- notify (
210- < div >
211- < div className = "font-medium" > Copied to clipboard</ div >
187+ return { content : pageContent , source : 'Copied rendered page content' }
188+ }
189+ // Last resort: copy the URL
190+ return { content : window . location . href , source : 'Page URL copied' }
191+ }
192+ }
193+
194+ const handleCopyPage = async ( ) => {
195+ // Start the clipboard write synchronously inside the click handler and let
196+ // the content resolve later. Awaiting the fetch first consumes the user
197+ // activation, and WebKit (iOS Safari) then rejects every clipboard write.
198+ const contentPromise = resolveCopyContent ( )
199+ const copyPromise = copyTextPromiseToClipboard (
200+ contentPromise . then ( ( { content } ) => content ) ,
201+ )
202+
203+ try {
204+ await copyPromise
205+ const { source } = await contentPromise
206+ copied . trigger ( )
207+ notify (
208+ < div >
209+ < div className = "font-medium" > Copied to clipboard</ div >
210+ { source ? (
212211 < div className = "text-xs text-gray-500 dark:text-gray-400" >
213- Page URL copied
212+ { source }
214213 </ div >
215- </ div > ,
216- { id : 'page-copied' } ,
217- )
218- }
214+ ) : null }
215+ </ div > ,
216+ { id : 'page-copied' } ,
217+ )
218+ } catch ( error ) {
219+ Sentry . captureException ( error , {
220+ tags : { feature : 'copy-page' } ,
221+ extra : {
222+ sourceMarkdownUrl,
223+ hasClipboardItem : typeof ClipboardItem !== 'undefined' ,
224+ } ,
225+ } )
226+ notify (
227+ < div >
228+ < div className = "font-medium" > Copy failed</ div >
229+ < div className = "text-xs text-gray-500 dark:text-gray-400" >
230+ Your browser blocked clipboard access. Try View as Markdown instead.
231+ </ div >
232+ </ div > ,
233+ { id : 'page-copy-failed' } ,
234+ )
219235 }
220236 }
221237
0 commit comments