Skip to content

Commit f377527

Browse files
perf(web): useCopyToClipboard reads its options through useStableCallback
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4338645 commit f377527

2 files changed

Lines changed: 10 additions & 19 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ export const REACT_COMPILED_FILES = [
3535
"apps/web/src/components/settings/ProjectFaviconPickerDialog.tsx",
3636
"apps/web/src/components/settings/ThemeEditorPanel.tsx",
3737
"apps/web/src/panelAnimations.ts",
38+
"apps/web/src/hooks/useCopyToClipboard.ts",
3839
] as const;

‎apps/web/src/hooks/useCopyToClipboard.ts‎

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
COMPOSER_CONTEXT_CLIPBOARD_MIME,
55
encodeComposerContextClipboardHtml,
66
} from "@t3tools/shared/composerContextClipboard";
7+
import { useStableCallback } from "./useStableCallback";
78

89
export class ClipboardApiUnavailableError extends Schema.TaggedError<ClipboardApiUnavailableError>()(
910
"ClipboardApiUnavailableError",
@@ -206,42 +207,31 @@ export function useCopyToClipboard<TContext = void>({
206207
} = {}): { copyToClipboard: (value: string, ctx: TContext) => void; isCopied: boolean } {
207208
const [isCopied, setIsCopied] = React.useState(false);
208209
const timeoutIdRef = React.useRef<NodeJS.Timeout | null>(null);
209-
const onCopyRef = React.useRef(onCopy);
210-
const onErrorRef = React.useRef(onError);
211-
const targetRef = React.useRef(target);
212-
const timeoutRef = React.useRef(timeout);
213-
214-
onCopyRef.current = onCopy;
215-
onErrorRef.current = onError;
216-
const extraFlavorsRef = React.useRef(extraFlavors);
217-
targetRef.current = target;
218-
timeoutRef.current = timeout;
219-
extraFlavorsRef.current = extraFlavors;
220-
221-
const copyToClipboard = React.useCallback((value: string, ctx: TContext): void => {
222-
void writeTextToClipboard(value, targetRef.current, extraFlavorsRef.current).then(
210+
// Callers pass inline onCopy/onError but key callbacks and effects on this function.
211+
const copyToClipboard = useStableCallback((value: string, ctx: TContext): void => {
212+
void writeTextToClipboard(value, target, extraFlavors).then(
223213
(didCopy) => {
224214
if (!didCopy) return;
225215
if (timeoutIdRef.current) {
226216
clearTimeout(timeoutIdRef.current);
227217
}
228218
setIsCopied(true);
229219

230-
onCopyRef.current?.(ctx);
220+
onCopy?.(ctx);
231221

232-
if (timeoutRef.current !== 0) {
222+
if (timeout !== 0) {
233223
timeoutIdRef.current = setTimeout(() => {
234224
setIsCopied(false);
235225
timeoutIdRef.current = null;
236-
}, timeoutRef.current);
226+
}, timeout);
237227
}
238228
},
239229
(error) => {
240230
console.error(error);
241-
onErrorRef.current?.(error, ctx);
231+
onError?.(error, ctx);
242232
},
243233
);
244-
}, []);
234+
});
245235

246236
React.useEffect(() => {
247237
return (): void => {

0 commit comments

Comments
 (0)