/**
 * useOnlineStatus Hook
 * 
 * Tracks the browser's online/offline status.
 * Useful for showing offline indicators and handling offline scenarios.
 * 
 * @returns {boolean} True if online, false if offline
 * 
 * @example
 * const isOnline = useOnlineStatus();
 * if (!isOnline) {
 *   return <OfflineIndicator />;
 * }
 */
import { useState, useEffect } from 'react';

export function useOnlineStatus(): boolean {
  const [isOnline, setIsOnline] = useState(() => {
    // Initialize with current status
    if (typeof navigator !== 'undefined') {
      return navigator.onLine;
    }
    return true; // Default to online if navigator is not available
  });

  useEffect(() => {
    const handleOnline = () => setIsOnline(true);
    const handleOffline = () => setIsOnline(false);

    window.addEventListener('online', handleOnline);
    window.addEventListener('offline', handleOffline);

    return () => {
      window.removeEventListener('online', handleOnline);
      window.removeEventListener('offline', handleOffline);
    };
  }, []);

  return isOnline;
}
