()
| 1 | import { useEffect, useRef, useState } from 'react' |
| 2 | |
| 3 | export function useClipboard() { |
| 4 | const [copied, setCopied] = useState(false) |
| 5 | const timeoutRef = useRef<number | null>(null) |
| 6 | |
| 7 | const copy = async (value: string) => { |
| 8 | try { |
| 9 | await navigator.clipboard.writeText(value) |
| 10 | setCopied(true) |
| 11 | if (timeoutRef.current) window.clearTimeout(timeoutRef.current) |
| 12 | timeoutRef.current = window.setTimeout(() => setCopied(false), 2000) |
| 13 | return true |
| 14 | } catch { |
| 15 | if (timeoutRef.current) window.clearTimeout(timeoutRef.current) |
| 16 | timeoutRef.current = null |
| 17 | setCopied(false) |
| 18 | return false |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | useEffect(() => { |
| 23 | return () => { |
| 24 | if (timeoutRef.current) window.clearTimeout(timeoutRef.current) |
| 25 | } |
| 26 | }, []) |
| 27 | |
| 28 | return { copy, copied } |
| 29 | } |
no outgoing calls
no test coverage detected