@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
53 lines (52 loc) • 1.68 kB
JavaScript
"use client";
const require_use_window_event = require("../use-window-event/use-window-event.cjs");
let react = require("react");
//#region packages/@mantine/hooks/src/use-network/use-network.ts
function getConnection() {
if (typeof navigator === "undefined") return {};
const _navigator = navigator;
const connection = _navigator.connection || _navigator.mozConnection || _navigator.webkitConnection;
if (!connection) return {};
return {
downlink: connection?.downlink,
downlinkMax: connection?.downlinkMax,
effectiveType: connection?.effectiveType,
rtt: connection?.rtt,
saveData: connection?.saveData,
type: connection?.type
};
}
function useNetwork() {
const [status, setStatus] = (0, react.useState)({ online: true });
const handleConnectionChange = (0, react.useCallback)(() => setStatus((current) => ({
...current,
...getConnection()
})), []);
require_use_window_event.useWindowEvent("online", () => setStatus({
online: true,
...getConnection()
}));
require_use_window_event.useWindowEvent("offline", () => setStatus({
online: false,
...getConnection()
}));
(0, react.useEffect)(() => {
const _navigator = navigator;
if (_navigator.connection) {
setStatus({
online: _navigator.onLine,
...getConnection()
});
_navigator.connection.addEventListener("change", handleConnectionChange);
return () => _navigator.connection.removeEventListener("change", handleConnectionChange);
}
if (typeof _navigator.onLine === "boolean") setStatus((current) => ({
...current,
online: _navigator.onLine
}));
}, []);
return status;
}
//#endregion
exports.useNetwork = useNetwork;
//# sourceMappingURL=use-network.cjs.map