import { useCallback, useLayoutEffect, useRef } from "react"; // Reported during the commit, so a close request right after a keystroke already sees it. export const useDirtyReport = (dirty: boolean, onDirtyChange?: (dirty: boolean) => void) => { useLayoutEffect(() => { onDirtyChange?.(dirty); }, [dirty, onDirtyChange]); }; // The overlay's side: a ref, read at the moment of closing rather than from the last render. export const useDirtyGuard = () => { const dirty = useRef(false); const report = useCallback((value: boolean) => { dirty.current = value; }, []); const reset = useCallback(() => { dirty.current = false; }, []); return { report, reset, isDirty: () => dirty.current }; };