diff --git a/components/MangoProvider.tsx b/components/MangoProvider.tsx index 5b68fdd6..9f52ce3c 100644 --- a/components/MangoProvider.tsx +++ b/components/MangoProvider.tsx @@ -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') { diff --git a/hooks/useNetworkSpeed.tsx b/hooks/useNetworkSpeed.tsx new file mode 100644 index 00000000..58e740f0 --- /dev/null +++ b/hooks/useNetworkSpeed.tsx @@ -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 +} diff --git a/utils/index.ts b/utils/index.ts index 32c20886..de0b7e5a 100644 --- a/utils/index.ts +++ b/utils/index.ts @@ -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 -} diff --git a/utils/network.ts b/utils/network.ts new file mode 100644 index 00000000..749e9af8 --- /dev/null +++ b/utils/network.ts @@ -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 +}