Skip to content

Commit 5cfbc8e

Browse files
committed
refactor: redesign useClipboard to be easier to test
1 parent 734b4f1 commit 5cfbc8e

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

site/src/hooks/useClipboard.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useRef, useState } from "react";
2-
import { displayError as dispatchError } from "components/GlobalSnackbar/utils";
2+
import { displayError } from "components/GlobalSnackbar/utils";
33

44
const CLIPBOARD_TIMEOUT_MS = 1_000;
55
const COPY_FAILED_MESSAGE = "Failed to copy text to clipboard";
@@ -8,10 +8,10 @@ export type UseClipboardInput = Readonly<{
88
textToCopy: string;
99

1010
/**
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.
1313
*/
14-
displayErrors?: boolean;
14+
errorCallback?: (errorMessage: string) => void;
1515
}>;
1616

1717
export type UseClipboardResult = Readonly<{
@@ -39,7 +39,7 @@ export type UseClipboardResult = Readonly<{
3939
}>;
4040

4141
export const useClipboard = (input: UseClipboardInput): UseClipboardResult => {
42-
const { textToCopy, displayErrors = true } = input;
42+
const { textToCopy, errorCallback } = input;
4343
const [showCopiedSuccess, setShowCopiedSuccess] = useState(false);
4444
const timeoutIdRef = useRef<number | undefined>();
4545

@@ -73,9 +73,9 @@ export const useClipboard = (input: UseClipboardInput): UseClipboardResult => {
7373
}
7474

7575
console.error(wrappedErr);
76-
if (displayErrors) {
77-
dispatchError(COPY_FAILED_MESSAGE);
78-
}
76+
77+
const dispatchError = errorCallback ?? displayError;
78+
dispatchError(COPY_FAILED_MESSAGE);
7979
}
8080
};
8181

0 commit comments

Comments
 (0)