From a152e581322745037f64df7648467ceb1955e613 Mon Sep 17 00:00:00 2001 From: asifrahaman13 Date: Tue, 25 Jun 2024 15:43:36 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20wip:=20implemented=20error=20sta?= =?UTF-8?q?te=20for=20backend=20socket=20connection=20and=20othe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/components/ChatWindow.tsx | 67 +++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/ui/components/ChatWindow.tsx b/ui/components/ChatWindow.tsx index 33bc3fb..728f419 100644 --- a/ui/components/ChatWindow.tsx +++ b/ui/components/ChatWindow.tsx @@ -18,7 +18,11 @@ export type Message = { sources?: Document[]; }; -const useSocket = (url: string, setIsReady: (ready: boolean) => void) => { +const useSocket = ( + url: string, + setIsReady: (ready: boolean) => void, + setError: (error: boolean) => void, +) => { const [ws, setWs] = useState(null); useEffect(() => { @@ -102,28 +106,36 @@ const useSocket = (url: string, setIsReady: (ready: boolean) => void) => { const ws = new WebSocket(wsURL.toString()); + const timeoutId = setTimeout(() => { + if (ws.readyState !== 1) { + ws.close(); + setError(true); + toast.error( + 'Failed to connect to the server. Please try again later.', + ); + } + }, 10000); // 10 seconds timeout + ws.onopen = () => { console.log('[DEBUG] open'); + clearTimeout(timeoutId); + setError(false); + setIsReady(true); }; - const stateCheckInterval = setInterval(() => { - if (ws.readyState === 1) { - setIsReady(true); - clearInterval(stateCheckInterval); - } - }, 100); + ws.onerror = () => { + clearTimeout(timeoutId); + setError(true); + toast.error('WebSocket connection error.'); + }; + + ws.onclose = () => { + clearTimeout(timeoutId); + setError(true); + console.log('[DEBUG] closed'); + }; setWs(ws); - - ws.onmessage = (e) => { - const parsedData = JSON.parse(e.data); - if (parsedData.type === 'error') { - toast.error(parsedData.data); - if (parsedData.key === 'INVALID_MODEL_SELECTED') { - localStorage.clear(); - } - } - }; }; connectWs(); @@ -133,7 +145,7 @@ const useSocket = (url: string, setIsReady: (ready: boolean) => void) => { ws?.close(); console.log('[DEBUG] closed'); }; - }, [ws, url, setIsReady]); + }, [ws, url, setIsReady, setError]); return ws; }; @@ -143,7 +155,12 @@ const ChatWindow = () => { const initialMessage = searchParams.get('q'); const [isReady, setIsReady] = useState(false); - const ws = useSocket(process.env.NEXT_PUBLIC_WS_URL!, setIsReady); + const [hasError, setHasError] = useState(false); + const ws = useSocket( + process.env.NEXT_PUBLIC_WS_URL!, + setIsReady, + setHasError, + ); const [chatHistory, setChatHistory] = useState<[string, string][]>([]); const [messages, setMessages] = useState([]); @@ -298,6 +315,14 @@ const ChatWindow = () => { // eslint-disable-next-line react-hooks/exhaustive-deps }, [isReady, initialMessage]); + if (hasError) { + return ( +
+

Failed to connect to the server. Please try again later.

+
+ ); + } + return isReady ? (
{messages.length > 0 ? ( @@ -329,11 +354,11 @@ const ChatWindow = () => { xmlns="http://www.w3.org/2000/svg" >