import React from 'react'
import type { ConnectionStatus } from './types'
import { useSocketManager } from './use-socket-manager'
import {
ActionButton,
ConnectionBadge,
DiagnosticItem,
ErrorAlert,
} from './diagnostic-component'
import { Container } from 'react-bootstrap-5'
import MaterialIcon from '@/shared/components/material-icon'
type NetworkInformation = {
downlink: number
effectiveType: string
rtt: number
saveData: boolean
type: string
}
const NavigatorInfo = () => {
if (!('connection' in navigator)) {
return
Network Information API not supported
}
const connection = navigator.connection as NetworkInformation
return (
<>
Downlink: {connection.downlink} Mbps
Effective Type: {connection.effectiveType}
Round Trip Time: {connection.rtt} ms
Save Data: {connection.saveData ? 'Enabled' : 'Disabled'}
Platform: {navigator.platform}
{/* @ts-ignore */}
Device Memory: {navigator.deviceMemory}
Hardware Concurrency: {navigator.hardwareConcurrency}
>
)
}
export const SocketDiagnostics = () => {
const { socketState, debugInfo, disconnectSocket, forceReconnect, socket } =
useSocketManager()
const getConnectionState = (): ConnectionStatus => {
if (socketState.connected) return 'connected'
if (socketState.connecting) return 'connecting'
return 'disconnected'
}
const lastReceivedS = debugInfo.lastReceived
? Math.round((Date.now() - debugInfo.lastReceived) / 1000)
: null
return (
Socket Diagnostics
{socketState.lastError && }
Connection Stats
{debugInfo.received} / {debugInfo.sent}
{lastReceivedS !== null && (
<>
Last received {lastReceivedS}s ago
>
)}
>
}
type={
lastReceivedS !== null
? lastReceivedS < 4
? 'success'
: 'danger'
: undefined
}
/>
{debugInfo.latency} ms
Max: {debugInfo.maxLatency} ms
>
) : (
'-'
)
}
type={
debugInfo.latency
? debugInfo.latency < 150
? 'success'
: 'danger'
: undefined
}
/>
{new Date(debugInfo.client.connectedAt).toUTCString()} (
{Math.round(
(Date.now() - debugInfo.client.connectedAt) / 1000
)}
s)
>
) : (
'-'
)
}
/>
}
/>
)
}