From 8b4a55e994a0bf927aee13f647d2bfd9efe7813e Mon Sep 17 00:00:00 2001 From: Arpit Bharti Date: Wed, 13 Mar 2024 00:02:57 +0530 Subject: [PATCH] remove type from connection --- packages/core/useNetwork/demo.tsx | 4 +--- packages/core/useNetwork/docs.mdx | 36 ++++++++++++++----------------- packages/core/useNetwork/index.ts | 30 +++----------------------- 3 files changed, 20 insertions(+), 50 deletions(-) diff --git a/packages/core/useNetwork/demo.tsx b/packages/core/useNetwork/demo.tsx index 91b88d8..498507e 100644 --- a/packages/core/useNetwork/demo.tsx +++ b/packages/core/useNetwork/demo.tsx @@ -5,9 +5,7 @@ import { NetworkEffectiveType, useNetwork } from '.' export function Demo() { const network = useNetwork() - const getClass = ( - value: number | boolean | NetworkEffectiveType | ConnectionType - ) => + const getClass = (value: number | boolean | NetworkEffectiveType) => `pill ${ value === undefined ? 'opacity-60' diff --git a/packages/core/useNetwork/docs.mdx b/packages/core/useNetwork/docs.mdx index f7dabc0..319d66d 100644 --- a/packages/core/useNetwork/docs.mdx +++ b/packages/core/useNetwork/docs.mdx @@ -40,34 +40,30 @@ export function Demo() { ## Type Declarations ```typescript -declare type NetworkEffectiveType = 'slow-2g' | '2g' | '3g' | '4g' | undefined - +declare type NetworkEffectiveType = 'slow-2g' | '2g' | '3g' | '4g' | undefined; interface NetworkInformation extends EventTarget { - readonly type?: ConnectionType - readonly effectiveType?: NetworkEffectiveType - readonly downlinkMax?: number - readonly downlink?: number - readonly rtt?: number - readonly saveData?: boolean - onchange?: EventListener + readonly effectiveType?: NetworkEffectiveType; + readonly downlinkMax?: number; + readonly downlink?: number; + readonly rtt?: number; + readonly saveData?: boolean; + onchange?: EventListener; } - /** * Reactive Network status. * * @see https://react-hooks-library.vercel.app/core/useNetwork */ declare function useNetwork(): { - isSupported: boolean - isOnline: boolean - offlineAt: number | undefined - saveData: boolean | undefined - rtt: number | undefined - downlink: number | undefined - downlinkMax: number | undefined - effectiveType: NetworkEffectiveType - type: ConnectionType -} + isSupported: boolean; + isOnline: boolean; + offlineAt: number | undefined; + saveData: boolean | undefined; + rtt: number | undefined; + downlink: number | undefined; + downlinkMax: number | undefined; + effectiveType: NetworkEffectiveType; +}; ``` ## Source diff --git a/packages/core/useNetwork/index.ts b/packages/core/useNetwork/index.ts index c97d26f..677714b 100644 --- a/packages/core/useNetwork/index.ts +++ b/packages/core/useNetwork/index.ts @@ -7,21 +7,7 @@ import { useMount } from '../useMount' export type NetworkEffectiveType = 'slow-2g' | '2g' | '3g' | '4g' | undefined -type ConnectionType = - | 'cellular2g' - | 'cellular3g' - | 'cellular4g' - | 'bluetooth' - | 'ethernet' - | 'none' - | 'wifi' - | 'wimax' - | 'other' - | 'unknown' - | undefined - export interface NetworkInformation extends EventTarget { - readonly type?: ConnectionType readonly effectiveType?: NetworkEffectiveType readonly downlinkMax?: number readonly downlink?: number @@ -30,21 +16,13 @@ export interface NetworkInformation extends EventTarget { onchange?: EventListener } -declare global { - interface TNavigator extends Navigator { - readonly connection: NetworkInformation - } -} - /** * Reactive Network status. * * @see https://react-hooks-library.vercel.app/core/useNetwork */ export function useNetwork() { - const isSupported = useIsSupported( - () => !!(_navigator as TNavigator)?.connection - ) + const isSupported = useIsSupported(() => !!_navigator?.connection) const [isOnline, setIsOnline] = useState(true) const [offlineAt, setOfflineAt] = useState(undefined) @@ -57,8 +35,7 @@ export function useNetwork() { setIsOnline(_navigator.onLine) setOfflineAt(isOnline ? undefined : Date.now()) - const _connection = (_navigator as TNavigator) - ?.connection as NetworkInformation + const _connection = _navigator?.connection as NetworkInformation if (!_connection) return connection.current = _connection @@ -82,7 +59,6 @@ export function useNetwork() { rtt: connection.current?.rtt, downlink: connection.current?.downlink, downlinkMax: connection.current?.downlinkMax, - effectiveType: connection.current?.effectiveType, - type: connection.current?.type + effectiveType: connection.current?.effectiveType } }