Skip to content

Commit

Permalink
feat: Added use-network.
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankeos committed Oct 5, 2024
1 parent 6975c9d commit 06a1d71
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Based on the [@mantine/hooks](https://github.com/mantinedev/mantine/tree/master/
- [ ] use-mouse
- [ ] use-move
- [ ] use-mutation-observer
- [ ] use-network
- [x] use-network
- [ ] use-orientation
- [x] use-os
- [ ] use-page-leave
Expand Down
7 changes: 6 additions & 1 deletion dev/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Component } from 'solid-js';
import './app.css';

// Hooks
import { useClickOutside, useHotkeys, useHover, useIdle, useOs } from '../src';
import { useClickOutside, useHotkeys, useHover, useIdle, useNetwork, useOs } from '../src';

const App: Component = () => {
// let ref = useClickOutside(() => {
Expand All @@ -17,6 +17,9 @@ const App: Component = () => {
events: ['mousemove', 'touchmove'],
initialState: false,
});

const networkStatus = useNetwork();

return (
<div class="">
{/* <p class="bg-green-500" ref={ref}>
Expand All @@ -28,6 +31,8 @@ const App: Component = () => {
<p ref={ref}>hovered: {JSON.stringify(hovered())}</p>

<p>idle: {JSON.stringify(idle())}</p>

<p>networkStatus: {JSON.stringify(networkStatus())}</p>
</div>
);
};
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from './use-hotkeys/use-hotkeys';
export * from './use-hover/use-hover';
export * from './use-idle/use-idle';
export * from './use-mounted/use-mounted';
export * from './use-network/use-network';
export * from './use-os/use-os';
75 changes: 75 additions & 0 deletions src/use-network/use-network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { createEffect, createSignal, onCleanup, onMount } from 'solid-js';

interface NetworkStatus {
downlink?: number;
downlinkMax?: number;
effectiveType?: 'slow-2g' | '2g' | '3g' | '4g';
rtt?: number;
saveData?: boolean;
type?: 'bluetooth' | 'cellular' | 'ethernet' | 'wifi' | 'wimax' | 'none' | 'other' | 'unknown';
}

function getConnection(): NetworkStatus {
if (typeof navigator === 'undefined') {
return {};
}

const _navigator = navigator as any;
const connection: any =
_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,
};
}

/**
* A hook that returns the current network status.
*/
export function useNetwork() {
const [status, setStatus] = createSignal<{ online: boolean } & NetworkStatus>({
online: true,
});

function handleConnectionChange() {
setStatus(current => ({ ...current, ...getConnection() }));
}

onMount(() => {
const onlineListener = () => setStatus({ online: true, ...getConnection() });
const offlineListener = () => setStatus({ online: false, ...getConnection() });
window.addEventListener('offline', offlineListener);

onCleanup(() => {
window.removeEventListener('online', onlineListener);
window.removeEventListener('offline', offlineListener);
});
});

createEffect(() => {
const _navigator = navigator as any;

if (_navigator.connection) {
setStatus({ online: _navigator.onLine, ...getConnection() });
_navigator.connection.addEventListener('change', handleConnectionChange);

onCleanup(() => _navigator.connection.removeEventListener('change', handleConnectionChange));
}

if (typeof _navigator.onLine === 'boolean') {
// Required for Firefox and other browsers that don't support navigator.connection
setStatus(current => ({ ...current, online: _navigator.onLine }));
}
});

return status;
}

0 comments on commit 06a1d71

Please sign in to comment.