change chat shortcut to ctrl+k and handled websocket errors

This commit is contained in:
anovazzi1 2023-04-27 23:23:14 -03:00
commit e92cf1110a
2 changed files with 116 additions and 80 deletions

View file

@ -14,7 +14,8 @@ export default function Chat({ flow }: ChatType) {
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
useEffect(() => { useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => { const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "K" && event.shiftKey && (event.metaKey||event.ctrlKey)) { event.preventDefault()
if ((event.key === "K"||event.key==="k") && (event.metaKey||event.ctrlKey)) {
setOpen(oldState=>!oldState); setOpen(oldState=>!oldState);
} }
}; };

View file

@ -28,7 +28,6 @@ export default function ChatModal({
setOpen: Function; setOpen: Function;
flow: FlowType; flow: FlowType;
}) { }) {
const { updateFlow } = useContext(TabsContext);
const [chatValue, setChatValue] = useState(""); const [chatValue, setChatValue] = useState("");
const [chatHistory, setChatHistory] = useState<ChatMessageType[]>([]); const [chatHistory, setChatHistory] = useState<ChatMessageType[]>([]);
const { reactFlowInstance } = useContext(typesContext); const { reactFlowInstance } = useContext(typesContext);
@ -55,6 +54,8 @@ export default function ChatModal({
}; };
function connectWS() { function connectWS() {
console.log("conectou");
try {
const newWs = new WebSocket(`ws://127.0.0.1:5003/chat/${flow.id}`); const newWs = new WebSocket(`ws://127.0.0.1:5003/chat/${flow.id}`);
newWs.onopen = () => { newWs.onopen = () => {
console.log("WebSocket connection established!"); console.log("WebSocket connection established!");
@ -117,7 +118,8 @@ export default function ChatModal({
} catch (error) { } catch (error) {
if (event.data !== "Error: 1005") { if (event.data !== "Error: 1005") {
setErrorData({ title: event.data }); setErrorData({ title: event.data });
newWs.close()
connectWS()
} }
} }
}; };
@ -126,25 +128,55 @@ export default function ChatModal({
setLockChat(false); setLockChat(false);
setTimeout(() => { setTimeout(() => {
connectWS(); connectWS();
}, 100); }, 1000);
} }
}; };
newWs.onerror= (ev)=>{
console.log(ev,"error")
}
setWs(newWs); setWs(newWs);
return newWs; return newWs;
} catch {
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",
],
});
} }
}
useEffect(() => {
if (ws && (ws.readyState === ws.CLOSED|| ws.readyState===ws.CLOSING)) {
let newWs = connectWS();
return () => {
console.log("trigger")
newWs.close();
};
}
}, [lockChat]);
useEffect(() => { useEffect(() => {
let newWs = connectWS(); let newWs = connectWS();
return () => { return () => {
console.log("trigger")
newWs.close(); newWs.close();
}; }
}, []); }, []);
async function sendAll(data: sendAllProps) { async function sendAll(data: sendAllProps) {
try {
if (ws) { if (ws) {
ws.send(JSON.stringify(data)); ws.send(JSON.stringify(data));
} }
} catch (error) {
setErrorData({title:"There was an erro sending the message",list:[error.message]})
setChatValue(data.message)
connectWS();
}
} }
useEffect(() => { useEffect(() => {
@ -294,7 +326,10 @@ export default function ChatModal({
) : ( ) : (
<div className="flex flex-col h-full text-center justify-center w-full items-center align-middle "> <div className="flex flex-col h-full text-center justify-center w-full items-center align-middle ">
<span> <span>
👋 <span className="text-gray-600 text-lg">LangFlow Chat</span> 👋{" "}
<span className="text-gray-600 text-lg">
LangFlow Chat
</span>
</span> </span>
<br /> <br />
<div className="bg-gray-100 rounded-md w-2/4 px-6 py-8 border border-gray-200"> <div className="bg-gray-100 rounded-md w-2/4 px-6 py-8 border border-gray-200">