All files / src/hooks useSmoothPending.ts

100% Statements 20/20
90.9% Branches 10/11
100% Functions 6/6
100% Lines 16/16

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                                259x 259x   259x 79x 9x 5x 4x 4x   5x     70x 3x 79x 3x 3x   79x     259x    
import { useEffect, useRef, useState } from 'react'
 
interface SmoothPendingOptions {
  /** How long a request may run before it is worth telling the user about. */
  delay?: number
  /** Once shown, the shortest time the indicator stays up. */
  minDuration?: number
}
 
/**
 * Smooths a pending flag so a spinner never flashes.
 *
 * Two problems, and each needs one of the two timers. A request that answers in
 * 80ms would otherwise blink the indicator on and straight back off, which
 * reads as a glitch rather than as speed — so nothing is shown for the first
 * `delay`. And a request that answers at `delay + 20ms` would show it for
 * 20ms, which is the same blink one step later — so once it is up it stays for
 * `minDuration`.
 *
 * Return this for what the user sees. Keep `disabled` and `aria-busy` on the
 * real flag: the control has to stop accepting presses the moment the request
 * starts, whatever is on screen.
 */
export function useSmoothPending(isPending: boolean, { delay = 400, minDuration = 400 }: SmoothPendingOptions = {}) {
  const [isVisible, setIsVisible] = useState(false)
  const shownAt = useRef<number | null>(null)
 
  useEffect(() => {
    if (isPending) {
      if (isVisible) return
      const timer = setTimeout(() => {
        shownAt.current = Date.now()
        setIsVisible(true)
      }, delay)
      return () => clearTimeout(timer)
    }
 
    if (!isVisible) return
    const elapsed = Date.now() - (shownAt.current ?? Date.now())
    const timer = setTimeout(() => {
      shownAt.current = null
      setIsVisible(false)
    }, Math.max(0, minDuration - elapsed))
    return () => clearTimeout(timer)
  }, [isPending, isVisible, delay, minDuration])
 
  return isVisible
}