import { useState, useEffect } from 'react';

const useTriggerStatus = () => {
  // initialized with the current online status of the browser (navigator.onLine).
  const [isOnline, setIsOnline] = useState<boolean>(navigator.onLine);
  
  const updateOnlineStatus = () => {
    setIsOnline(navigator.onLine);
  };

  useEffect(() => {
    // Adds an event listener that calls updateOnlineStatus when the browser goes online or offline
    window.addEventListener('online', updateOnlineStatus);
    window.addEventListener('offline', updateOnlineStatus);

  }, []);

  return isOnline;
};

export default useTriggerStatus;
