From 68e17384c835721c494815ec5358f99a2864e777 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 12 Jun 2023 17:03:36 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20chore(chatModal):=20extract=20ge?= =?UTF-8?q?tWebSocketUrl=20function=20to=20improve=20readability=20and=20m?= =?UTF-8?q?aintainability=20The=20getWebSocketUrl=20function=20was=20extra?= =?UTF-8?q?cted=20to=20improve=20the=20readability=20and=20maintainability?= =?UTF-8?q?=20of=20the=20code.=20It=20takes=20the=20chatId=20and=20isDevel?= =?UTF-8?q?opment=20flag=20as=20parameters=20and=20returns=20the=20WebSock?= =?UTF-8?q?et=20URL=20based=20on=20the=20current=20environment.=20The=20fu?= =?UTF-8?q?nction=20uses=20the=20window.location=20object=20to=20determine?= =?UTF-8?q?=20the=20protocol=20and=20host,=20and=20the=20chatEndpoint=20to?= =?UTF-8?q?=20construct=20the=20URL.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/modals/chatModal/index.tsx | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/frontend/src/modals/chatModal/index.tsx b/src/frontend/src/modals/chatModal/index.tsx index c8a227ae8..59f117319 100644 --- a/src/frontend/src/modals/chatModal/index.tsx +++ b/src/frontend/src/modals/chatModal/index.tsx @@ -178,14 +178,23 @@ export default function ChatModal({ } } + function getWebSocketUrl(chatId, isDevelopment = false) { + const isSecureProtocol = window.location.protocol === "https:"; + const webSocketProtocol = isSecureProtocol ? "wss" : "ws"; + const host = isDevelopment ? "localhost:7860" : window.location.host; + const chatEndpoint = `/api/v1/chat/${chatId}`; + + return `${ + isDevelopment ? "ws" : webSocketProtocol + }://${host}${chatEndpoint}`; + } + function connectWS() { try { - const urlWs = + const urlWs = getWebSocketUrl( + id.current, process.env.NODE_ENV === "development" - ? `ws://localhost:7860/api/v1/chat/${id.current}` - : `${window.location.protocol === "https:" ? "wss" : "ws"}://${ - window.location.host - }api/v1/chat/${id.current}`; + ); const newWs = new WebSocket(urlWs); newWs.onopen = () => { console.log("WebSocket connection established!");