diff --git a/src/components/Footer.tsx b/src/components/Footer.tsx index 9faeeb2..81700e7 100644 --- a/src/components/Footer.tsx +++ b/src/components/Footer.tsx @@ -1,9 +1,27 @@ import React from 'react'; import { NavLink } from 'react-router-dom'; -import { Flex, useColorModeValue, Link } from '@chakra-ui/react'; +import { Box, Flex, useColorModeValue, Link } from '@chakra-ui/react'; + +import { useNetworkState } from '@hooks/useNetworkState'; export function Footer() { const bgFooter = useColorModeValue('gray.100', 'gray.900'); + const networkState = useNetworkState(); + let connectionState; + + if (networkState === 'online') { + connectionState = ( + + ); + } + + if (networkState === 'offline') { + connectionState = ; + } + + if (networkState === 'slow') { + connectionState = ; + } return ( <> @@ -26,6 +44,18 @@ export function Footer() { > Políticas de Privacidad + + Estado de conexión + {connectionState} + ); diff --git a/src/hooks/useNetworkState.tsx b/src/hooks/useNetworkState.tsx new file mode 100644 index 0000000..cae207f --- /dev/null +++ b/src/hooks/useNetworkState.tsx @@ -0,0 +1,48 @@ +import { useEffect, useState } from 'react'; + +type NetworkState = 'online' | 'offline' | 'slow'; + +export function useNetworkState(): NetworkState { + const [networkState, setNetworkState] = useState('online'); + + useEffect(() => { + function handleNetworkChange() { + if (navigator.onLine) { + // Comprobar si la velocidad de conexión es lenta o no + const connection = + (navigator as any).connection || + (navigator as any).mozConnection || + (navigator as any).webkitConnection; + + if (connection) { + const effectiveType = connection.effectiveType; + + if ( + effectiveType === 'slow-2g' || + effectiveType === '2g' || + effectiveType === '3g' + ) { + setNetworkState('slow'); + } else { + setNetworkState('online'); + } + } else { + setNetworkState('online'); + } + } else { + setNetworkState('offline'); + } + } + + window.addEventListener('online', handleNetworkChange); + window.addEventListener('offline', handleNetworkChange); + handleNetworkChange(); + + return () => { + window.removeEventListener('online', handleNetworkChange); + window.removeEventListener('offline', handleNetworkChange); + }; + }, []); + + return networkState; +}