refactor: Add useCopy()

The `useCopy()` hook is pretty simple, as it handle a state variable
(`copied`) and put it back to false after a `timeout`
This commit is contained in:
Alejandro Borbolla 2025-07-31 00:31:34 +02:00
commit 59504ac336
No known key found for this signature in database

View file

@ -0,0 +1,29 @@
"use client";
import { useEffect, useState } from "react";
type UseCopyProps = {
timeout?: number;
};
function useCopy({ timeout = 500 }: UseCopyProps = {}) {
const [copied, setCopied] = useState(false);
useEffect(() => {
let timeoutId: NodeJS.Timeout;
if (copied) {
timeoutId = setTimeout(() => {
setCopied(false);
}, timeout);
}
return () => {
if (timeoutId) {
clearTimeout(timeoutId);
}
};
}, [copied]);
return { copied, setCopied };
}
export default useCopy;