import { useState } from 'react'

import { useEventListener } from 'hooks'

export const useNetwork = () => {
  const [isOnline, setNetwork] = useState(window.navigator.onLine)

  const updateNetwork = () => {
    setNetwork(window.navigator.onLine)
  }

  useEventListener({
    callback: updateNetwork,
    event: 'offline',
  })

  useEventListener({
    callback: updateNetwork,
    event: 'online',
  })

  return { isOnline }
}
