Created
January 11, 2024 21:33
-
-
Save GalindoSVQ/4e330b5ab84170d2c765354cd75d7b61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackblitz.com/edit/stackblitz-starters-uyyln2