Skip to content

Instantly share code, notes, and snippets.

@GalindoSVQ
Created January 11, 2024 21:33
Show Gist options
  • Save GalindoSVQ/4e330b5ab84170d2c765354cd75d7b61 to your computer and use it in GitHub Desktop.
Save GalindoSVQ/4e330b5ab84170d2c765354cd75d7b61 to your computer and use it in GitHub Desktop.
import * as React from "react";
const isShallowEqual = (object1, object2) => {
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);
if (keys1.length !== keys2.length) {
return false;
}
for (let key of keys1) {
if (object1[key] !== object2[key]) {
return false;
}
}
return true;
};
const getConnection = () => {
return (
navigator?.connection ||
navigator?.mozConnection ||
navigator?.webkitConnection
);
};
const subscribe = (callback) => {
window.addEventListener("online", callback, { passive: true });
window.addEventListener("offline", callback, { passive: true });
const connection = getConnection();
if (connection) {
connection.addEventListener("change", callback, { passive: true });
}
return () => {
window.removeEventListener("online", callback);
window.removeEventListener("offline", callback);
if (connection) {
connection.removeEventListener("change", callback);
}
};
};
const getServerSnapshot = () => {
throw Error("useNetworkState is a client-only hook");
};
export default function useNetworkState() {
const cache = React.useRef({});
const getSnapshot = () => {
const online = navigator.onLine;
const connection = getConnection();
const nextState = {
online,
downlink: connection?.downlink,
downlinkMax: connection?.downlinkMax,
effectiveType: connection?.effectiveType,
rtt: connection?.rtt,
saveData: connection?.saveData,
type: connection?.type
};
if (isShallowEqual(cache.current, nextState)) {
return cache.current;
} else {
cache.current = nextState;
return nextState;
}
};
return React.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
}
@GalindoSVQ
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment