fix(clipboard): Clear setTimeout in useCopyToClipboard to resolve memory leak (#4228)

fix: auto patch for clipboard_44
This commit is contained in:
kiwina 2025-06-05 04:45:51 +08:00 committed by GitHub
parent 72493ad40d
commit 15b37134c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,4 +1,4 @@
import { useState, useCallback } from "react"
import { useState, useCallback, useEffect, useRef } from "react"
/**
* Options for copying text to clipboard
@ -33,15 +33,24 @@ export const copyToClipboard = async (text: string, options?: CopyOptions): Prom
*/
export const useCopyToClipboard = (feedbackDuration = 2000) => {
const [showCopyFeedback, setShowCopyFeedback] = useState(false)
const timeoutRef = useRef<NodeJS.Timeout | null>(null)
const copyWithFeedback = useCallback(
async (text: string, e?: React.MouseEvent) => {
e?.stopPropagation()
// Clear any existing timeout
if (timeoutRef.current) {
clearTimeout(timeoutRef.current)
}
const success = await copyToClipboard(text, {
onSuccess: () => {
setShowCopyFeedback(true)
setTimeout(() => setShowCopyFeedback(false), feedbackDuration)
timeoutRef.current = setTimeout(() => {
setShowCopyFeedback(false)
timeoutRef.current = null
}, feedbackDuration)
},
})
@ -50,6 +59,15 @@ export const useCopyToClipboard = (feedbackDuration = 2000) => {
[feedbackDuration],
)
// Cleanup timeout on unmount
useEffect(() => {
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current)
}
}
}, [])
return {
showCopyFeedback,
copyWithFeedback,