{"version":3,"file":"useNetworkStatus.cjs","names":["useState","isServer"],"sources":["../../../src/hooks/useNetworkStatus/useNetworkStatus.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport { isServer } from '../../utils/isServer/index.ts';\n\n/**\n * Effective connection type based on Network Information API\n * @see https://wicg.github.io/netinfo/#effective-connection-types\n */\nexport type EffectiveConnectionType = 'slow-2g' | '2g' | '3g' | '4g';\n\n/**\n * Physical connection type\n * @see https://wicg.github.io/netinfo/#dom-connectiontype\n */\nexport type ConnectionType =\n  | 'bluetooth'\n  | 'cellular'\n  | 'ethernet'\n  | 'mixed'\n  | 'none'\n  | 'other'\n  | 'unknown'\n  | 'wifi'\n  | 'wimax';\n\n/**\n * Network status information from Network Information API\n */\nexport type NetworkStatus = {\n  /** Effective connection type (4g, 3g, 2g, slow-2g) - undefined if API not supported */\n  effectiveType?: EffectiveConnectionType;\n  /** Physical connection type (wifi, cellular, etc.) - undefined if API not supported */\n  type?: ConnectionType;\n  /** Downlink speed in Mbps - undefined if API not supported */\n  downlink?: number;\n  /** Round-trip time in milliseconds - undefined if API not supported */\n  rtt?: number;\n  /** User's data saver preference - undefined if API not supported */\n  saveData?: boolean;\n};\n\ntype NetworkInformation = {\n  effectiveType?: EffectiveConnectionType;\n  type?: ConnectionType;\n  downlink?: number;\n  rtt?: number;\n  saveData?: boolean;\n  addEventListener(type: 'change', listener: () => void): void;\n  removeEventListener(type: 'change', listener: () => void): void;\n} & EventTarget;\n\ntype NavigatorWithConnection = {\n  connection?: NetworkInformation;\n} & Navigator;\n\n/**\n * @description\n * `useNetworkStatus` is a React hook that provides access to the Network Information API.\n * It provides raw network connection data. Returns undefined for all properties\n * if the API is not supported (e.g., Safari, Firefox).\n *\n * **Browser Support**:\n * - Chrome/Edge (Android): Full support\n * - Chrome/Edge (Desktop): Partial support (effectiveType, downlink, rtt, saveData)\n * - Firefox: Not supported\n * - Safari: Not supported\n *\n * @returns {NetworkStatus} Network status information\n * - `effectiveType` - Connection quality: 'slow-2g' | '2g' | '3g' | '4g'\n * - `type` - Physical connection: 'wifi' | 'cellular' | 'ethernet' | etc.\n * - `downlink` - Downlink speed in Mbps\n * - `rtt` - Round-trip time in milliseconds\n * - `saveData` - User's data saver preference\n *\n * @example\n * function AdaptiveImage() {\n *   const { effectiveType, saveData } = useNetworkStatus();\n *\n *   // Determine quality based on your app's needs\n *   const useHighQuality = effectiveType === '4g' && !saveData;\n *\n *   return (\n *     <img\n *       src={useHighQuality ? 'high-res.jpg' : 'low-res.jpg'}\n *       alt=\"Content\"\n *     />\n *   );\n * }\n *\n * @example\n * function VideoPlayer() {\n *   const { type, downlink } = useNetworkStatus();\n *\n *   // Custom logic: only autoplay on wifi with good bandwidth\n *   const shouldAutoplay = type === 'wifi' && (downlink ?? 0) > 5;\n *\n *   return <video src=\"video.mp4\" autoPlay={shouldAutoplay} />;\n * }\n *\n * @see https://wicg.github.io/netinfo/\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Network_Information_API\n */\nexport function useNetworkStatus(): NetworkStatus {\n  const [networkStatus, setNetworkStatus] = useState<NetworkStatus>(getNetworkStatus);\n\n  useEffect(function subscribeToNetworkChanges() {\n    if (isServer()) {\n      return;\n    }\n\n    const connection = getNavigatorConnection();\n\n    if (connection == null) {\n      return;\n    }\n\n    function updateNetworkStatus() {\n      setNetworkStatus(getNetworkStatus());\n    }\n\n    connection.addEventListener('change', updateNetworkStatus);\n\n    return function cleanup() {\n      connection.removeEventListener('change', updateNetworkStatus);\n    };\n  }, []);\n\n  return networkStatus;\n}\n\nfunction getNavigatorConnection(): NetworkInformation | null {\n  if (isServer() || !('connection' in navigator)) {\n    return null;\n  }\n\n  try {\n    return (navigator as NavigatorWithConnection).connection ?? null;\n  } catch {\n    return null;\n  }\n}\n\nfunction getNetworkStatus(): NetworkStatus {\n  const connection = getNavigatorConnection();\n\n  if (connection == null) {\n    return {};\n  }\n\n  return {\n    effectiveType: connection.effectiveType,\n    type: connection.type,\n    downlink: connection.downlink,\n    rtt: connection.rtt,\n    saveData: connection.saveData,\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsGA,SAAgB,mBAAkC;CAChD,MAAM,CAAC,eAAe,qBAAA,GAAoBA,MAAAA,SAAAA,CAAwB,gBAAgB;CAElF,CAAA,GAAA,MAAA,UAAA,CAAU,SAAS,4BAA4B;EAC7C,IAAIC,iBAAAA,SAAS,GACX;EAGF,MAAM,aAAa,uBAAuB;EAE1C,IAAI,cAAc,MAChB;EAGF,SAAS,sBAAsB;GAC7B,iBAAiB,iBAAiB,CAAC;EACrC;EAEA,WAAW,iBAAiB,UAAU,mBAAmB;EAEzD,OAAO,SAAS,UAAU;GACxB,WAAW,oBAAoB,UAAU,mBAAmB;EAC9D;CACF,GAAG,CAAC,CAAC;CAEL,OAAO;AACT;AAEA,SAAS,yBAAoD;CAC3D,IAAIA,iBAAAA,SAAS,KAAK,EAAE,gBAAgB,YAClC,OAAO;CAGT,IAAI;EACF,OAAQ,UAAsC,cAAc;CAC9D,QAAQ;EACN,OAAO;CACT;AACF;AAEA,SAAS,mBAAkC;CACzC,MAAM,aAAa,uBAAuB;CAE1C,IAAI,cAAc,MAChB,OAAO,CAAC;CAGV,OAAO;EACL,eAAe,WAAW;EAC1B,MAAM,WAAW;EACjB,UAAU,WAAW;EACrB,KAAK,WAAW;EAChB,UAAU,WAAW;CACvB;AACF"}