import React, { useState, useEffect } from "react"; import { Box, Typography, CircularProgress, Paper, LinearProgress } from "@mui/material"; import { ReadyState } from "react-use-websocket"; interface ConnectionStatusProps { readyState: ReadyState; reconnectAttempt?: number; } const ConnectionStatus: React.FC = ({ readyState, reconnectAttempt = 0 }) => { const [countdown, setCountdown] = useState(0); // Start countdown when connection is closed and we're attempting to reconnect useEffect(() => { if (readyState === ReadyState.CLOSED && reconnectAttempt > 0) { setCountdown(5); const interval = setInterval(() => { setCountdown((prev) => { if (prev <= 1) { clearInterval(interval); return 0; } return prev - 1; }); }, 1000); return () => clearInterval(interval); } }, [readyState, reconnectAttempt]); const getConnectionStatusText = () => { switch (readyState) { case ReadyState.CONNECTING: return reconnectAttempt > 0 ? `Reconnecting... (attempt ${reconnectAttempt})` : "Connecting to server..."; case ReadyState.OPEN: return "Connected"; case ReadyState.CLOSING: return "Disconnecting..."; case ReadyState.CLOSED: if (reconnectAttempt > 0 && countdown > 0) { return `Connection lost. Retrying in ${countdown}s... (attempt ${reconnectAttempt})`; } return reconnectAttempt > 0 ? "Reconnecting..." : "Disconnected"; case ReadyState.UNINSTANTIATED: return "Initializing..."; default: return "Unknown connection state"; } }; const getConnectionColor = () => { switch (readyState) { case ReadyState.OPEN: return "success.main"; case ReadyState.CONNECTING: return "info.main"; case ReadyState.CLOSED: return "error.main"; case ReadyState.CLOSING: return "warning.main"; default: return "text.secondary"; } }; const shouldShowProgress = readyState === ReadyState.CONNECTING || (readyState === ReadyState.CLOSED && reconnectAttempt > 0); return ( {shouldShowProgress && } {getConnectionStatusText()} {readyState === ReadyState.CLOSED && reconnectAttempt > 0 && countdown > 0 && ( )} ); }; export { ConnectionStatus };