1
1
import { useEffect , useRef , useState } from "react" ;
2
- import { displayError as dispatchError } from "components/GlobalSnackbar/utils" ;
2
+ import { displayError } from "components/GlobalSnackbar/utils" ;
3
3
4
4
const CLIPBOARD_TIMEOUT_MS = 1_000 ;
5
5
const COPY_FAILED_MESSAGE = "Failed to copy text to clipboard" ;
@@ -8,10 +8,10 @@ export type UseClipboardInput = Readonly<{
8
8
textToCopy : string ;
9
9
10
10
/**
11
- * Indicates whether the user should be notified of an error if the copy
12
- * operation fails for whatever reason. Defaults to true .
11
+ * Callback to call when an error happens. By default, this will use the
12
+ * codebase's global displayError function .
13
13
*/
14
- displayErrors ?: boolean ;
14
+ errorCallback ?: ( errorMessage : string ) => void ;
15
15
} > ;
16
16
17
17
export type UseClipboardResult = Readonly < {
@@ -39,7 +39,7 @@ export type UseClipboardResult = Readonly<{
39
39
} > ;
40
40
41
41
export const useClipboard = ( input : UseClipboardInput ) : UseClipboardResult => {
42
- const { textToCopy, displayErrors = true } = input ;
42
+ const { textToCopy, errorCallback } = input ;
43
43
const [ showCopiedSuccess , setShowCopiedSuccess ] = useState ( false ) ;
44
44
const timeoutIdRef = useRef < number | undefined > ( ) ;
45
45
@@ -73,9 +73,9 @@ export const useClipboard = (input: UseClipboardInput): UseClipboardResult => {
73
73
}
74
74
75
75
console . error ( wrappedErr ) ;
76
- if ( displayErrors ) {
77
- dispatchError ( COPY_FAILED_MESSAGE ) ;
78
- }
76
+
77
+ const dispatchError = errorCallback ?? displayError ;
78
+ dispatchError ( COPY_FAILED_MESSAGE ) ;
79
79
}
80
80
} ;
81
81
0 commit comments