import {useEffect} from 'react'

import {onActive} from '@/lib/network'

// run refresh on mount, and again whenever the app is likely to have regained
// reach (foreground or reconnect) — so a foregrounded stale screen self-heals
// the moment the network returns. pass a stable function (useCallback) or the
// effect re-subscribes
export const useRefreshOnActive = (refresh: () => void): void => {
  useEffect(() => {
    refresh()
    return onActive(refresh)
  }, [refresh])
}
