update ws error handle

This commit is contained in:
anovazzi1 2023-05-04 19:25:24 -03:00
commit 4c2f3b80dc
3 changed files with 318 additions and 341 deletions

View file

@ -5,7 +5,7 @@ import { PopUpContext } from "../../../contexts/popUpContext";
import { useContext } from "react"; import { useContext } from "react";
import ChatModal from "../../../modals/chatModal"; import ChatModal from "../../../modals/chatModal";
export default function ChatTrigger({open, setOpen,flow}){ export default function ChatTrigger({open, setOpen}){
const {openPopUp} = useContext(PopUpContext) const {openPopUp} = useContext(PopUpContext)
return(<Transition return(<Transition
show={!open} show={!open}

View file

@ -26,7 +26,7 @@ export default function Chat({ flow }: ChatType) {
return ( return (
<> <>
<ChatModal key={flow.id} flow={flow} open={open} setOpen={setOpen} /> <ChatModal key={flow.id} flow={flow} open={open} setOpen={setOpen} />
<ChatTrigger open={open} setOpen={setOpen} flow={flow} /> <ChatTrigger open={open} setOpen={setOpen} />
</> </>
); );
} }

View file

@ -1,13 +1,7 @@
import { Dialog, Transition } from "@headlessui/react"; import { Dialog, Transition } from "@headlessui/react";
import { import { ChatBubbleOvalLeftEllipsisIcon } from "@heroicons/react/24/outline";
ChatBubbleOvalLeftEllipsisIcon,
LockClosedIcon,
PaperAirplaneIcon,
} from "@heroicons/react/24/outline";
import { Fragment, useContext, useEffect, useRef, useState } from "react"; import { Fragment, useContext, useEffect, useRef, useState } from "react";
import { PopUpContext } from "../../contexts/popUpContext";
import { FlowType, NodeType } from "../../types/flow"; import { FlowType, NodeType } from "../../types/flow";
import { TabsContext } from "../../contexts/tabsContext";
import { alertContext } from "../../contexts/alertContext"; import { alertContext } from "../../contexts/alertContext";
import { toNormalCase } from "../../utils"; import { toNormalCase } from "../../utils";
import { typesContext } from "../../contexts/typesContext"; import { typesContext } from "../../contexts/typesContext";
@ -54,7 +48,6 @@ export default function ChatModal({
}; };
function connectWS() { function connectWS() {
console.log("conectou");
try { try {
const urlWs = const urlWs =
process.env.NODE_ENV === "development" process.env.NODE_ENV === "development"
@ -68,7 +61,6 @@ export default function ChatModal({
console.log("WebSocket connection established!"); console.log("WebSocket connection established!");
}; };
newWs.onmessage = (event) => { newWs.onmessage = (event) => {
try {
setLockChat(false); setLockChat(false);
const data = JSON.parse(event.data); const data = JSON.parse(event.data);
console.log("Received data:", data); console.log("Received data:", data);
@ -119,24 +111,15 @@ export default function ChatModal({
addChatHistory(data.message, false, data.intermediate_steps); addChatHistory(data.message, false, data.intermediate_steps);
} }
} }
if (data.type == "file") { if (data.type === "file") {
console.log(data); console.log(data);
} }
} catch (error) {
if (event.data !== "Error: 1005") {
setErrorData({ title: event.data });
newWs.close();
connectWS();
}
}
}; };
newWs.onclose = (_) => { newWs.onclose = (_) => {
if (open) {
setLockChat(false); setLockChat(false);
setTimeout(() => { setTimeout(() => {
connectWS(); connectWS();
}, 1000); }, 1000);
}
}; };
newWs.onerror = (ev) => { newWs.onerror = (ev) => {
console.log(ev, "error"); console.log(ev, "error");
@ -148,14 +131,22 @@ export default function ChatModal({
setErrorData({ setErrorData({
title: "There was an error on web connection, please: ", title: "There was an error on web connection, please: ",
list: [ list: [
"refresh the page", "Refresh the page",
"use a new flow tab", "Use a new flow tab",
"check if the backend is up", "Check if the backend is up",
], ],
}); });
} }
} }
useEffect(() => {
let newWs = connectWS();
return () => {
console.log("trigger");
newWs.close();
};
}, []);
useEffect(() => { useEffect(() => {
if (ws && (ws.readyState === ws.CLOSED || ws.readyState === ws.CLOSING)) { if (ws && (ws.readyState === ws.CLOSED || ws.readyState === ws.CLOSING)) {
let newWs = connectWS(); let newWs = connectWS();
@ -166,14 +157,6 @@ export default function ChatModal({
} }
}, [lockChat]); }, [lockChat]);
useEffect(() => {
let newWs = connectWS();
return () => {
console.log("trigger");
newWs.close();
};
}, []);
async function sendAll(data: sendAllProps) { async function sendAll(data: sendAllProps) {
try { try {
if (ws) { if (ws) {
@ -281,17 +264,11 @@ export default function ChatModal({
ws.send(JSON.stringify({ clear_history: true })); ws.send(JSON.stringify({ clear_history: true }));
} }
const { closePopUp } = useContext(PopUpContext);
function setModalOpen(x: boolean) { function setModalOpen(x: boolean) {
setOpen(x); setOpen(x);
if (x === false) {
setTimeout(() => {
closePopUp();
}, 300);
}
} }
return ( return (
<Transition.Root show={open} appear={true} as={Fragment}> <Transition.Root show={open} appear={open} as={Fragment}>
<Dialog <Dialog
as="div" as="div"
className="relative z-10" className="relative z-10"