import { useState, useLayoutEffect, useCallback, useEffect } from "react"; export function useLockBodyScroll() { useLayoutEffect(() => { // Get original body overflow const originalStyle = window.getComputedStyle(document.body).overflow; // Prevent scrolling on mount document.body.style.overflow = "hidden"; // Re-enable scrolling when component unmounts return () => (document.body.style.overflow = originalStyle); }, []); // Empty array ensures effect is only run on mount and unmount } export function useCopyToClipboard(text, notifyTimeout = 1000) { const [copyStatus, setCopyStatus] = useState("inactive"); const copy = useCallback(() => { navigator.clipboard.writeText(text).then( () => setCopyStatus("copied"), () => setCopyStatus("failed") ); }, [text]); useEffect(() => { if (copyStatus === "inactive") { return; } const timeoutId = setTimeout( () => setCopyStatus("inactive"), notifyTimeout ); return () => clearTimeout(timeoutId); }, [copyStatus]); return [copyStatus, copy]; } function getStorageValue(key, defaultValue) { // getting stored value if (typeof window !== "undefined") { const saved = window.localStorage.getItem(key); const initial = saved !== null ? JSON.parse(saved) : defaultValue; return initial; } } export const useLocalStorage = (key, defaultValue) => { const [value, setValue] = useState(() => { return getStorageValue(key, defaultValue); }); useEffect(() => { // storing input name window.localStorage.setItem(key, JSON.stringify(value)); }, [key, value]); return [value, setValue]; };