added check to onCloseWebsocket event

This commit is contained in:
anovazzi1 2023-08-30 18:49:41 -03:00
commit 60945ce883
2 changed files with 93 additions and 84 deletions

View file

@ -312,7 +312,7 @@ export async function getHealth() {
*/ */
export async function getBuildStatus( export async function getBuildStatus(
flowId: string flowId: string
): Promise<BuildStatusTypeAPI> { ): Promise<AxiosResponse<BuildStatusTypeAPI>> {
return await api.get(`${BASE_URL_API}build/${flowId}/status`); return await api.get(`${BASE_URL_API}build/${flowId}/status`);
} }

View file

@ -8,7 +8,7 @@ import { classNames } from "../../utils/utils";
import ChatInput from "./chatInput"; import ChatInput from "./chatInput";
import ChatMessage from "./chatMessage"; import ChatMessage from "./chatMessage";
import _ from "lodash"; import _, { set } from "lodash";
import AccordionComponent from "../../components/AccordionComponent"; import AccordionComponent from "../../components/AccordionComponent";
import IconComponent from "../../components/genericIconComponent"; import IconComponent from "../../components/genericIconComponent";
import ToggleShadComponent from "../../components/toggleShadComponent"; import ToggleShadComponent from "../../components/toggleShadComponent";
@ -27,6 +27,7 @@ import { AuthContext } from "../../contexts/authContext";
import { TabsContext } from "../../contexts/tabsContext"; import { TabsContext } from "../../contexts/tabsContext";
import { TabsState } from "../../types/tabs"; import { TabsState } from "../../types/tabs";
import { validateNodes } from "../../utils/reactflowUtils"; import { validateNodes } from "../../utils/reactflowUtils";
import { getBuildStatus } from "../../controllers/API";
export default function FormModal({ export default function FormModal({
flow, flow,
@ -155,9 +156,21 @@ export default function FormModal({
function handleOnClose(event: CloseEvent): void { function handleOnClose(event: CloseEvent): void {
if (isOpen.current) { if (isOpen.current) {
getBuildStatus(flow.id).then((response) => {
if (response.data.built) {
connectWS();
}
else {
setErrorData({
title: "Please build the flow again before using the chat."
})
}
}).catch((error) => {
setErrorData({title:error.data?.detail?error.data.detail:error.message})
});
setErrorData({ title: event.reason }); setErrorData({ title: event.reason });
setTimeout(() => { setTimeout(() => {
connectWS();
setLockChat(false); setLockChat(false);
}, 1000); }, 1000);
} }
@ -173,8 +186,7 @@ export default function FormModal({
const host = isDevelopment ? "localhost:7860" : window.location.host; const host = isDevelopment ? "localhost:7860" : window.location.host;
const chatEndpoint = `/api/v1/chat/${chatId}`; const chatEndpoint = `/api/v1/chat/${chatId}`;
return `${ return `${isDevelopment ? "ws" : webSocketProtocol
isDevelopment ? "ws" : webSocketProtocol
}://${host}${chatEndpoint}?token=${encodeURIComponent(accessToken!)}`; }://${host}${chatEndpoint}?token=${encodeURIComponent(accessToken!)}`;
} }
@ -260,7 +272,6 @@ export default function FormModal({
}; };
newWs.onmessage = (event) => { newWs.onmessage = (event) => {
const data = JSON.parse(event.data); const data = JSON.parse(event.data);
console.log("Received data:", data);
handleWsMessage(data); handleWsMessage(data);
//get chat history //get chat history
}; };
@ -268,7 +279,6 @@ export default function FormModal({
handleOnClose(event); handleOnClose(event);
}; };
newWs.onerror = (ev) => { newWs.onerror = (ev) => {
console.log(ev, "error");
if (flow.id === "") { if (flow.id === "") {
connectWS(); connectWS();
} else { } else {
@ -294,7 +304,6 @@ export default function FormModal({
useEffect(() => { useEffect(() => {
connectWS(); connectWS();
return () => { return () => {
console.log("unmount");
console.log(ws); console.log(ws);
if (ws.current) { if (ws.current) {
ws.current.close(); ws.current.close();