From a6f15476c9438c18e1a30a04963d0784ff18b87b Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 14 Jun 2023 10:51:54 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20refactor(chatModal):=20remove=20?= =?UTF-8?q?unused=20imports=20and=20variables,=20extract=20getWebSocketUrl?= =?UTF-8?q?=20function=20The=20imports=20for=20XMarkIcon=20and=20FaEraser?= =?UTF-8?q?=20were=20removed=20as=20they=20were=20not=20being=20used.=20Th?= =?UTF-8?q?e=20lodash=20set=20function=20was=20also=20removed=20as=20it=20?= =?UTF-8?q?was=20not=20being=20used.=20The=20getWebSocketUrl=20function=20?= =?UTF-8?q?was=20extracted=20from=20the=20connectWS=20function=20to=20impr?= =?UTF-8?q?ove=20readability=20and=20maintainability.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/modals/chatModal/index.tsx | 27 ++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/frontend/src/modals/chatModal/index.tsx b/src/frontend/src/modals/chatModal/index.tsx index c8a227ae8..bf2de6718 100644 --- a/src/frontend/src/modals/chatModal/index.tsx +++ b/src/frontend/src/modals/chatModal/index.tsx @@ -1,8 +1,5 @@ import { Dialog, Transition } from "@headlessui/react"; -import { - ChatBubbleOvalLeftEllipsisIcon, - XMarkIcon, -} from "@heroicons/react/24/outline"; +import { ChatBubbleOvalLeftEllipsisIcon } from "@heroicons/react/24/outline"; import { Fragment, useContext, useEffect, useRef, useState } from "react"; import { FlowType, NodeType } from "../../types/flow"; import { alertContext } from "../../contexts/alertContext"; @@ -12,10 +9,10 @@ import ChatMessage from "./chatMessage"; import { FaEraser } from "react-icons/fa"; import { HiX } from "react-icons/hi"; import { sendAllProps } from "../../types/api"; -import { ChatMessageType, ChatType } from "../../types/chat"; +import { ChatMessageType } from "../../types/chat"; import ChatInput from "./chatInput"; -import _, { set } from "lodash"; +import _ from "lodash"; export default function ChatModal({ flow, @@ -177,15 +174,23 @@ export default function ChatModal({ updateLastMessage({ str: data.message }); } } + 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!");