export function useClipboard(content: string): [NodeJS.Timeout | undefined, () => void] {
const [clicked, setClicked] = useState<NodeJS.Timeout | undefined>(undefined)
const write = () => {
if (clicked) {
clearTimeout(clicked)
}
const id = setTimeout(() => {
clearTimeout(clicked)
setClicked(undefined)
}, 1000)
setClicked(id)
}
useEffect(() => {
if (!clicked) {
return
}
const copy = async () => {
try {
await navigator.clipboard.writeText(content)
} catch (error) {
console.error(error)
}
}
copy()
}, [clicked, content])
return [clicked, write]
}