File tree Expand file tree Collapse file tree 2 files changed +21
-4
lines changed
frontend/app/src/shared/hooks Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Original file line number Diff line number Diff line change 1+ Resolved an issue where the copy to clipboard did not work on insecure (HTTP) URLs.
Original file line number Diff line number Diff line change 11import React from "react" ;
22
3+ function oldSchoolCopy ( text : string ) {
4+ const tempTextArea = document . createElement ( "textarea" ) ;
5+ tempTextArea . value = text ;
6+ document . body . appendChild ( tempTextArea ) ;
7+ tempTextArea . select ( ) ;
8+ document . execCommand ( "copy" ) ;
9+ document . body . removeChild ( tempTextArea ) ;
10+ }
11+
12+ const COPIED_FEEDBACK_DURATION = 2000 ;
13+
314export function useCopyToClipboard ( ) {
415 const [ isCopied , setIsCopied ] = React . useState ( false ) ;
516
617 const copyToClipboard = React . useCallback ( async ( value : string ) => {
18+ function confirmCopied ( ) {
19+ setIsCopied ( true ) ;
20+ setTimeout ( ( ) => setIsCopied ( false ) , COPIED_FEEDBACK_DURATION ) ;
21+ }
22+
723 try {
824 await navigator . clipboard . writeText ( value ) ;
9- setIsCopied ( true ) ;
10- setTimeout ( ( ) => setIsCopied ( false ) , 2000 ) ; // Reset copied state after 2 seconds
11- } catch ( error ) {
12- console . error ( "Failed to copy: " , error ) ;
25+ confirmCopied ( ) ;
26+ } catch {
27+ oldSchoolCopy ( value ) ;
28+ confirmCopied ( ) ;
1329 }
1430 } , [ ] ) ;
1531
You can’t perform that action at this time.
0 commit comments