From 60b4d2e446bb40fffe6b3ec7797792baaef5fee6 Mon Sep 17 00:00:00 2001 From: Steven Enamakel Date: Thu, 29 Jan 2026 08:12:37 +0530 Subject: [PATCH] Enhance error handling and connection management in Telegram components - Added console error logging in TelegramConnectionModal for better debugging during authentication failures. - Updated connection status check in TelegramProvider to only verify once when connected, improving efficiency. - Simplified socket connection logic in socketService by removing unnecessary checks and reducing timeout for initial connection. This update improves error visibility and optimizes connection handling, enhancing the overall user experience during Telegram interactions. --- src/components/TelegramConnectionModal.tsx | 2 ++ src/providers/TelegramProvider.tsx | 5 +---- src/services/socketService.ts | 11 ++++------- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/components/TelegramConnectionModal.tsx b/src/components/TelegramConnectionModal.tsx index 5d55c541c..f3e0d1cec 100644 --- a/src/components/TelegramConnectionModal.tsx +++ b/src/components/TelegramConnectionModal.tsx @@ -155,6 +155,7 @@ const TelegramConnectionModal = ({ }, async (err) => { // Handle errors + console.error("[mtproto] signInWithQrCode error:", err); const errorMessage = err.message || "Authentication error"; // Check if it's a 2FA password needed error @@ -208,6 +209,7 @@ const TelegramConnectionModal = ({ onComplete(); onClose(); } catch (err) { + console.error("[mtproto] Telegram login flow failed:", err); setIsAuthenticating(false); const errorMessage = err instanceof Error ? err.message : "Authentication failed"; diff --git a/src/providers/TelegramProvider.tsx b/src/providers/TelegramProvider.tsx index dda70d489..bbd3ca31c 100644 --- a/src/providers/TelegramProvider.tsx +++ b/src/providers/TelegramProvider.tsx @@ -131,7 +131,7 @@ const TelegramProvider = ({ children }: TelegramProviderProps) => { userId, ]); - // Check connection status periodically to keep user online + // Check connection status once when Telegram reports as connected useEffect(() => { if ( !shouldSetupTelegram || @@ -151,9 +151,6 @@ const TelegramProvider = ({ children }: TelegramProviderProps) => { }; checkConnection(); - const interval = setInterval(checkConnection, 20000); - - return () => clearInterval(interval); }, [shouldSetupTelegram, isInitialized, connectionStatus, userId]); const checkConnection = async (): Promise => { diff --git a/src/services/socketService.ts b/src/services/socketService.ts index 22b861ffc..cb86deb6e 100644 --- a/src/services/socketService.ts +++ b/src/services/socketService.ts @@ -19,14 +19,10 @@ class SocketService { * Connect to the socket server with authentication */ connect(token: string): void { - if (!token) { - return; - } + if (!token) return; // Don't connect if already connected with the same token - if (this.socket?.connected && this.token === token) { - return; - } + if (this.socket?.connected && this.token === token) return; // Disconnect existing connection if token changed or socket exists if (this.socket) { @@ -66,7 +62,7 @@ class SocketService { reconnectionDelay: 1000, reconnectionAttempts: 5, forceNew: true, // Force new connection to ensure auth is sent - timeout: 20000, // Increase timeout for initial connection + timeout: 2000, // Increase timeout for initial connection upgrade: true, // Allow upgrade from polling to websocket query: {}, // Explicitly prevent token from being added to query string }; @@ -77,6 +73,7 @@ class SocketService { this.socket.on("connect", () => { const socketId = this.socket?.id || null; const uid = getSocketUserId(); + console.log("connected", socketId, uid); store.dispatch(setStatusForUser({ userId: uid, status: "connected" })); store.dispatch(setSocketIdForUser({ userId: uid, socketId })); });