All files / src/shared/lib/hooks useDelayedSkeleton.ts

100% Statements 14/14
85.71% Branches 6/7
100% Functions 4/4
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26      11x   11x 8x 8x   8x 4x   4x 2x       8x 8x 8x       11x    
import { useEffect, useState } from 'react';
 
export function useDelayedSkeleton(isLoading: boolean, delay = 300) {
	const [show, setShow] = useState(isLoading);
 
	useEffect(() => {
		let timer: ReturnType<typeof setTimeout> | null = null;
		let isMounted = true;
 
		if (isLoading) {
			setShow(true);
		} else {
			timer = setTimeout(() => {
				Eif (isMounted) setShow(false);
			}, delay);
		}
 
		return () => {
			isMounted = false;
			if (timer) clearTimeout(timer);
		};
	}, [isLoading, delay]);
 
	return show;
}