-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
103 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use client'; | ||
|
||
import {useEffect, useState} from 'react'; | ||
import {isServer} from '@/utils/isServer'; | ||
|
||
const fastNetworkTypes = ['4g']; | ||
|
||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
function isSupported() { | ||
if (isServer()) { | ||
return false; | ||
} | ||
|
||
return navigator && navigator.connection && navigator.connection.effectiveType; | ||
} | ||
|
||
/** | ||
* @returns Network speed. | ||
*/ | ||
function getNetworkType() { | ||
if (!isSupported()) { | ||
return '4g'; | ||
} | ||
|
||
return navigator.connection!.effectiveType!; | ||
} | ||
|
||
/** | ||
* Hook to get network speed. | ||
* @returns Network speed. | ||
*/ | ||
export default function useNetworkSpeed() { | ||
const [networkType, setNetworkType] = useState(getNetworkType()); | ||
const isNetworkFast = fastNetworkTypes.includes(networkType); | ||
|
||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
const handleNetworkChange = () => { | ||
setNetworkType(getNetworkType()); | ||
}; | ||
|
||
useEffect(() => { | ||
isSupported() && navigator.connection!.addEventListener('change', handleNetworkChange); | ||
|
||
return () => { | ||
isSupported() && navigator.connection!.removeEventListener('change', handleNetworkChange); | ||
}; | ||
}, [setNetworkType]); | ||
|
||
return [networkType, isNetworkFast]; | ||
} |
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* eslint-disable jsdoc/require-jsdoc */ | ||
declare interface Navigator extends NavigatorNetworkInformation {} | ||
|
||
type EffectiveConnectionType = '2g' | '3g' | '4g' | 'slow-2g'; | ||
|
||
interface NetworkInformation extends EventTarget { | ||
readonly effectiveType?: EffectiveConnectionType; | ||
onchange?: EventListener; | ||
} | ||
|
||
declare interface NavigatorNetworkInformation { | ||
readonly connection?: NetworkInformation; | ||
} |