new network hook

This commit is contained in:
tjs 2023-07-20 21:40:37 -04:00
parent 28cb708dcd
commit eab9a0a587
4 changed files with 29 additions and 17 deletions

View File

@ -5,8 +5,8 @@ import { useRouter } from 'next/router'
import { MangoAccount } from '@blockworks-foundation/mango-v4'
import useMangoAccount from 'hooks/useMangoAccount'
import useInterval from './shared/useInterval'
import { isNetworkSlow } from 'utils'
import { SECONDS } from 'utils/constants'
import useNetworkSpeed from 'hooks/useNetworkSpeed'
const set = mangoStore.getState().set
const actions = mangoStore.getState().actions
@ -16,7 +16,7 @@ const HydrateStore = () => {
const { name: marketName } = router.query
const { mangoAccountPk, mangoAccountAddress } = useMangoAccount()
const connection = mangoStore((s) => s.connection)
const slowNetwork = isNetworkSlow()
const slowNetwork = useNetworkSpeed()
useEffect(() => {
if (marketName && typeof marketName === 'string') {

12
hooks/useNetworkSpeed.tsx Normal file
View File

@ -0,0 +1,12 @@
import { useEffect, useState } from 'react'
import isNetworkSlow from 'utils/network'
export default function useNetworkSpeed() {
const [slowNetwork, setSlowNetwork] = useState(true)
useEffect(() => {
setSlowNetwork(isNetworkSlow())
}, [])
return slowNetwork
}

View File

@ -44,18 +44,3 @@ export const copyToClipboard = (copyThis: string) => {
document.execCommand('copy')
document.body.removeChild(el)
}
export function getNetworkInfo() {
const connection =
(navigator as any).connection ||
(navigator as any).mozConnection ||
(navigator as any).webkitConnection
if (connection) {
return connection.effectiveType
}
return 'unknown'
}
export function isNetworkSlow() {
return ['slow-2g', '2g', '3g'].includes(getNetworkInfo()) ? true : false
}

15
utils/network.ts Normal file
View File

@ -0,0 +1,15 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export function getNetworkInfo() {
const connection =
(navigator as any).connection ||
(navigator as any).mozConnection ||
(navigator as any).webkitConnection
if (connection) {
return connection.effectiveType
}
return 'unknown'
}
export default function isNetworkSlow() {
return ['slow-2g', '2g', '3g'].includes(getNetworkInfo()) ? true : false
}