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 ChatModal from "../../../modals/chatModal";
export default function ChatTrigger({open, setOpen,flow}){
export default function ChatTrigger({open, setOpen}){
const {openPopUp} = useContext(PopUpContext)
return(<Transition
show={!open}

View file

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