diff --git a/apps/webapp/app/hooks/useCopy.ts b/apps/webapp/app/hooks/useCopy.ts index 00d63c51bc5..843ff2b3298 100644 --- a/apps/webapp/app/hooks/useCopy.ts +++ b/apps/webapp/app/hooks/useCopy.ts @@ -1,7 +1,8 @@ -import { useCallback, useState } from "react"; +import { useCallback, useState, useRef, useEffect } from "react"; export function useCopy(value: string, duration = 1500) { const [copied, setCopied] = useState(false); + const timeoutRef = useRef(null); const copy = useCallback( (e?: React.MouseEvent) => { @@ -10,13 +11,23 @@ export function useCopy(value: string, duration = 1500) { e.stopPropagation(); } navigator.clipboard.writeText(value); + + if (timeoutRef.current) clearTimeout(timeoutRef.current); + setCopied(true); - setTimeout(() => { + timeoutRef.current = setTimeout(() => { setCopied(false); }, duration); }, [value, duration] ); + useEffect(() => { + setCopied(false); + return () => { + if (timeoutRef.current) clearTimeout(timeoutRef.current); + }; + }, [value]); + return { copy, copied }; }