Skip to content

Commit

Permalink
remove type from connection
Browse files Browse the repository at this point in the history
  • Loading branch information
heyitsarpit committed Mar 12, 2024
1 parent 532c712 commit 8b4a55e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 50 deletions.
4 changes: 1 addition & 3 deletions packages/core/useNetwork/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
36 changes: 16 additions & 20 deletions packages/core/useNetwork/docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 3 additions & 27 deletions packages/core/useNetwork/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<number | undefined>(undefined)

Expand All @@ -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
Expand All @@ -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
}
}

0 comments on commit 8b4a55e

Please sign in to comment.