import { useEffect } from 'react'; import { useLocation, useNavigate } from 'react-router'; import { useTranslation } from 'react-i18next'; import { QRCodeSVG } from 'qrcode.react'; import { useAuthStore } from '../store/auth'; import { useBranding } from '../hooks/useBranding'; interface ConnectionQRState { url: string; hideLink: boolean; } function isValidState(state: unknown): state is ConnectionQRState { if (!state || typeof state !== 'object') return false; const s = state as Record; return typeof s.url === 'string' && s.url.length > 0 && typeof s.hideLink === 'boolean'; } export default function ConnectionQR() { const { t } = useTranslation(); const navigate = useNavigate(); const location = useLocation(); const isAuthenticated = useAuthStore((state) => state.isAuthenticated); const isLoading = useAuthStore((state) => state.isLoading); const { appName } = useBranding(); const state = location.state as unknown; const validState = isValidState(state) ? state : null; useEffect(() => { if (!isLoading && !isAuthenticated) { navigate('/login', { replace: true }); } }, [isAuthenticated, isLoading, navigate]); useEffect(() => { if (!isLoading && isAuthenticated && !validState) { navigate('/connection', { replace: true }); } }, [isLoading, isAuthenticated, validState, navigate]); useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') { e.preventDefault(); navigate(-1); } }; document.addEventListener('keydown', handleKeyDown); return () => document.removeEventListener('keydown', handleKeyDown); }, [navigate]); if (isLoading || !validState) { return null; } return (
{/* Close button */} {/* Content */}
{/* Branding name */} {appName && (

{appName}

)} {/* Title */}

{t('subscription.connection.qrTitle')}

{/* Hint */}

{t('subscription.connection.qrScanHint')}

{/* QR code container */}
{/* URL display */} {!validState.hideLink && (

{validState.url}

)}
); }