import { NitroEventType, ReconnectEvent } from '@nitrots/nitro-renderer'; import { FC, useCallback, useState } from 'react'; import { Base, Column, Text } from '../../common'; import { useNitroEvent } from '../../hooks'; export const ReconnectView: FC<{}> = props => { const [ isReconnecting, setIsReconnecting ] = useState(false); const [ attempt, setAttempt ] = useState(0); const [ maxAttempts, setMaxAttempts ] = useState(0); const [ hasFailed, setHasFailed ] = useState(false); const onReconnecting = useCallback((event: ReconnectEvent) => { setIsReconnecting(true); setHasFailed(false); setAttempt(event.attempt); setMaxAttempts(event.maxAttempts); }, []); const onReconnected = useCallback(() => { // Socket is open but not yet re-authenticated. // Update attempt display but keep the overlay visible until // re-authentication completes (SOCKET_REAUTHENTICATED). setHasFailed(false); }, []); const onReauthenticated = useCallback(() => { // Fully re-authenticated — dismiss the overlay so the room view // (which stayed alive behind the overlay) is visible again. setIsReconnecting(false); setHasFailed(false); setAttempt(0); }, []); const onReconnectFailed = useCallback(() => { setIsReconnecting(false); setHasFailed(true); }, []); useNitroEvent(NitroEventType.SOCKET_RECONNECTING, onReconnecting); useNitroEvent(NitroEventType.SOCKET_RECONNECTED, onReconnected); useNitroEvent(NitroEventType.SOCKET_REAUTHENTICATED, onReauthenticated); useNitroEvent(NitroEventType.SOCKET_RECONNECT_FAILED, onReconnectFailed); const handleReload = useCallback(() => { window.location.reload(); }, []); const handleGoHome = useCallback(() => { sessionStorage.removeItem('nitro.session.lastRoomId'); sessionStorage.removeItem('nitro.session.lastRoomPassword'); window.location.reload(); }, []); if(!isReconnecting && !hasFailed) return null; return ( { isReconnecting && ( <> Connection lost Reconnecting to server... (attempt { attempt }/{ maxAttempts }) Please wait, your session will be restored automatically ) } { hasFailed && ( <> Connection failed Unable to reconnect to the server after multiple attempts. Reload Page Go to Home ) } ); };