Skip to content

Add useNetworkInformation#79

Open
wojtekmaj wants to merge 2 commits into
mainfrom
use-network-information
Open

Add useNetworkInformation#79
wojtekmaj wants to merge 2 commits into
mainfrom
use-network-information

Conversation

@wojtekmaj

Copy link
Copy Markdown
Owner

No description provided.

@wojtekmaj

Copy link
Copy Markdown
Owner Author

See microsoft/TypeScript#27186

Copilot AI review requested due to automatic review settings February 13, 2026 13:20
@wojtekmaj wojtekmaj force-pushed the use-network-information branch from 0f17fd9 to d59f1f3 Compare February 13, 2026 13:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new React hook (useNetworkInformation) to expose the browser’s Network Information API (navigator.connection) in an SSR-safe way, and wires it into the library’s public exports.

Changes:

  • Add useNetworkInformation hook implementation and a corresponding test file.
  • Export useNetworkInformation from src/index.ts.
  • Add network-information-types and configure TypeScript to see its global types.

Reviewed changes

Copilot reviewed 4 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
yarn.lock Locks the newly added network-information-types package.
tsconfig.json Adds a types entry intended to include Network Information ambient typings.
src/useNetworkInformation.ts Implements the new useNetworkInformation hook.
src/useNetworkInformation.spec.ts Adds tests for SSR behavior and change subscriptions.
src/index.ts Exposes the new hook as part of the public API.
package.json Adds network-information-types to dependencies list (currently as devDependency).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/index.ts
Comment on lines 38 to 43
useIntersectionObserver,
useLocalStorage,
useMatchMedia,
useMutationObserver,
useNetworkInformation,
useOnLine,

Copilot AI Feb 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an src/index.spec.ts test that asserts every exported hook is present. This PR adds useNetworkInformation to src/index.ts, but without updating that export-coverage test the new hook won’t be checked. Please add a matching assertion for useNetworkInformation in src/index.spec.ts.

Copilot uses AI. Check for mistakes.
Comment thread package.json
Comment on lines 38 to 41
"@vitest/browser-playwright": "^4.0.1",
"husky": "^9.0.0",
"network-information-types": "^0.1.1",
"playwright": "^1.55.1",

Copilot AI Feb 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useNetworkInformation’s public return type uses NetworkInformation. If that type is provided only via network-information-types and not by lib.dom, consumers may get "Cannot find name 'NetworkInformation'" when using this package’s generated .d.ts. Consider shipping the ambient declaration as part of this package (or adding a /// <reference types="network-information-types" /> in emitted declarations) and ensure the type package is available to consumers (often meaning moving it from devDependencies to dependencies).

Copilot uses AI. Check for mistakes.
Comment on lines +31 to +35

const connection = navigator.connection;

setConnection(connection || null);
}

Copilot AI Feb 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hook stores the navigator.connection object itself in state. In browsers, this object is typically stable and its properties mutate on change events, so calling setConnection(navigator.connection) often sets the same reference and React will skip re-rendering. To ensure consumers see updates, store a snapshot of relevant fields (or maintain a separate version counter) rather than the raw object reference.

Copilot uses AI. Check for mistakes.
Comment on lines +27 to +29
function onConnectionChange() {
if (!isBrowser || !('connection' in navigator)) {
return null;

Copilot AI Feb 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onConnectionChange returns null in one branch, but the return value is ignored by the event listener. This should be a void handler for clarity/consistency.

Suggested change
function onConnectionChange() {
if (!isBrowser || !('connection' in navigator)) {
return null;
function onConnectionChange(): void {
if (!isBrowser || !('connection' in navigator)) {
return;

Copilot uses AI. Check for mistakes.
import { useEffect, useState } from 'react';

const isBrowser = typeof window !== 'undefined';

Copilot AI Feb 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new hook is missing the JSDoc block used by the other exported hooks in src/ (description + @returns etc.). Adding it keeps generated typings/docs consistent across the package.

Suggested change
/**
* React hook that provides the current {@link NetworkInformation} from the browser's
* `navigator.connection` API and subscribes to updates when the connection changes.
*
* When the Network Information API is not supported or the code is not running in a
* browser environment, this hook returns {@code null}.
*
* @returns The current {@link NetworkInformation} object, or {@code null} if the
* Network Information API is unavailable.
*/

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,133 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { act, renderHook } from '@testing-library/react';

Copilot AI Feb 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test imports renderHook/act from @testing-library/react, but the repo’s other hook tests consistently use vitest-browser-react (which is also mocked in vitest.setup.ts). Additionally, @testing-library/react is not declared in package.json, which will fail under Yarn PnP. Switch to vitest-browser-react (and react-dom/test-utils for act, as in other tests) or add the missing dependency and align with the repo’s test harness.

Suggested change
import { act, renderHook } from '@testing-library/react';
import { renderHook } from 'vitest-browser-react';
import { act } from 'react-dom/test-utils';

Copilot uses AI. Check for mistakes.
Comment thread src/index.ts
Comment on lines 8 to 12
import useMatchMedia from './useMatchMedia.js';
import useMutationObserver from './useMutationObserver.js';
import useNetworkInformation from './useNetworkInformation.js';
import useOnLine from './useOnLine.js';
import usePermissionState from './usePermissionState.js';

Copilot AI Feb 13, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useNetworkInformation is a new public export; the README appears to document every hook (table of contents + section per hook). Please add the corresponding README entry so consumers can discover and use it consistently with the rest of the package.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants