File tree Expand file tree Collapse file tree 2 files changed +10
-26
lines changed
node-detail/primitive/components Expand file tree Collapse file tree 2 files changed +10
-26
lines changed Original file line number Diff line number Diff line change 11import { Chip } from '@nextui-org/chip' ;
2- import { memo , useCallback , useEffect } from 'react' ;
2+ import { memo , useCallback } from 'react' ;
33import { Text } from '../../../ui/components/Text' ;
44import { isNull } from '../../../utils/json.util' ;
5- import useCopyToClipboard from '../../../utils/react-hooks/useCopyToClipboard' ;
5+ import { useCopyToClipboard } from '../../../utils/react-hooks/useCopyToClipboard' ;
66import { useHover } from '../../../utils/react-hooks/useHover' ;
77
88type Props = {
@@ -11,18 +11,12 @@ type Props = {
1111
1212const _TextCopyBox = ( { text } : Props ) => {
1313 const [ hostRef , isHostHovered ] = useHover < HTMLDivElement > ( ) ;
14- const { copiedText, copyToClipboard, clearClipboard } = useCopyToClipboard ( ) ;
14+ const { copiedText, copyToClipboard } = useCopyToClipboard ( ) ;
1515
1616 const copyText = useCallback ( ( ) => {
1717 copyToClipboard ( text ) ;
1818 } , [ copyToClipboard , text ] ) ;
1919
20- useEffect ( ( ) => {
21- if ( ! isHostHovered ) {
22- clearClipboard ( ) ;
23- }
24- } , [ clearClipboard , isHostHovered ] ) ;
25-
2620 return (
2721 < div
2822 ref = { hostRef }
Original file line number Diff line number Diff line change @@ -4,12 +4,16 @@ import { useState } from 'react';
44
55type CopiedText = string | null ;
66type CopyFn = ( text : string ) => Promise < boolean > ; // Return success
7- type ClearFn = ( ) => Promise < boolean > ; // Return success
87
9- const useCopyToClipboard = ( ) => {
8+ export const useCopyToClipboard = ( ) => {
109 const [ copiedText , setCopiedText ] = useState < CopiedText > ( null ) ;
1110
1211 const copyToClipboard : CopyFn = async ( text ) => {
12+ if ( ! navigator ?. clipboard ) {
13+ console . warn ( 'Clipboard not supported' ) ;
14+ return false ;
15+ }
16+
1317 // Try to save to clipboard then save it in the state if worked
1418 try {
1519 await navigator . clipboard . writeText ( text ) ;
@@ -22,19 +26,5 @@ const useCopyToClipboard = () => {
2226 }
2327 } ;
2428
25- const clearClipboard : ClearFn = async ( ) => {
26- try {
27- await navigator . clipboard . writeText ( '' ) ;
28- setCopiedText ( null ) ;
29- return true ;
30- } catch ( error ) {
31- console . warn ( 'Clear failed' , error ) ;
32- setCopiedText ( null ) ;
33- return false ;
34- }
35- } ;
36-
37- return { copiedText, copyToClipboard, clearClipboard } ;
29+ return { copiedText, copyToClipboard } ;
3830} ;
39-
40- export default useCopyToClipboard ;
You can’t perform that action at this time.
0 commit comments