sending message when hit enter, not saving chat for each tab yet
This commit is contained in:
parent
84921b419f
commit
4e1f1b58bd
3 changed files with 41 additions and 27 deletions
|
|
@ -9,13 +9,15 @@ import { useContext, useEffect, useRef, useState } from "react";
|
||||||
import { sendAll } from "../../controllers/NodesServices";
|
import { sendAll } from "../../controllers/NodesServices";
|
||||||
import { alertContext } from "../../contexts/alertContext";
|
import { alertContext } from "../../contexts/alertContext";
|
||||||
import { nodeColors } from "../../utils";
|
import { nodeColors } from "../../utils";
|
||||||
|
import { TabsContext } from "../../contexts/tabsContext";
|
||||||
|
|
||||||
const _ = require("lodash");
|
const _ = require("lodash");
|
||||||
|
|
||||||
export default function Chat({ reactFlowInstance }) {
|
export default function Chat({flow, reactFlowInstance }) {
|
||||||
|
const {updateFlow} = useContext(TabsContext)
|
||||||
const [open, setOpen] = useState(true);
|
const [open, setOpen] = useState(true);
|
||||||
const [chatValue, setChatValue] = useState("");
|
const [chatValue, setChatValue] = useState("");
|
||||||
const [chatHistory, setChatHistory] = useState([]);
|
const [chatHistory, setChatHistory] = useState(flow.chat);
|
||||||
const {setErrorData} = useContext(alertContext);
|
const {setErrorData} = useContext(alertContext);
|
||||||
const addChatHistory = (message, isSend) => {
|
const addChatHistory = (message, isSend) => {
|
||||||
setChatHistory((old) => {
|
setChatHistory((old) => {
|
||||||
|
|
@ -23,7 +25,13 @@ export default function Chat({ reactFlowInstance }) {
|
||||||
newChat.push({ message, isSend });
|
newChat.push({ message, isSend });
|
||||||
return newChat;
|
return newChat;
|
||||||
});
|
});
|
||||||
|
updateFlow({..._.cloneDeep(flow),chat:chatHistory})
|
||||||
|
|
||||||
};
|
};
|
||||||
|
useEffect(()=>{
|
||||||
|
// setChatHistory(flow.chat)
|
||||||
|
console.log(flow.chat)
|
||||||
|
},[flow])
|
||||||
useEffect(()=>{
|
useEffect(()=>{
|
||||||
ref.current.scrollIntoView({behavior: 'smooth'});
|
ref.current.scrollIntoView({behavior: 'smooth'});
|
||||||
}, [chatHistory])
|
}, [chatHistory])
|
||||||
|
|
@ -40,6 +48,28 @@ export default function Chat({ reactFlowInstance }) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
const ref = useRef(null);
|
const ref = useRef(null);
|
||||||
|
|
||||||
|
function sendMessage(){
|
||||||
|
console.log(reactFlowInstance.toObject())
|
||||||
|
if(chatValue !== ""){
|
||||||
|
if(validateNodes()){
|
||||||
|
if(validateChatNodes()){
|
||||||
|
let message = chatValue;
|
||||||
|
setChatValue("");
|
||||||
|
addChatHistory(message, true);
|
||||||
|
sendAll({...reactFlowInstance.toObject(),message}).then((r) => {addChatHistory(r.data.result, false);});
|
||||||
|
} else {
|
||||||
|
setErrorData({title: 'Error sending message', list:['Chat nodes are missing.']})
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
setErrorData({title: 'Error sending message', list:['There are required fields not filled yet.']})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setErrorData({title: 'Error sending message', list:['The message cannot be empty.']})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Transition
|
<Transition
|
||||||
|
|
@ -90,6 +120,11 @@ export default function Chat({ reactFlowInstance }) {
|
||||||
<div className="w-full bg-white border-t flex items-center justify-between p-3">
|
<div className="w-full bg-white border-t flex items-center justify-between p-3">
|
||||||
<div className="relative w-full mt-1 rounded-md shadow-sm">
|
<div className="relative w-full mt-1 rounded-md shadow-sm">
|
||||||
<input
|
<input
|
||||||
|
onKeyDown={(event)=>{
|
||||||
|
if(event.key==='Enter'){
|
||||||
|
sendMessage()
|
||||||
|
}
|
||||||
|
}}
|
||||||
type="text"
|
type="text"
|
||||||
value={chatValue}
|
value={chatValue}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
|
|
@ -100,28 +135,7 @@ export default function Chat({ reactFlowInstance }) {
|
||||||
/>
|
/>
|
||||||
<div className="absolute inset-y-0 right-0 flex items-center pr-3">
|
<div className="absolute inset-y-0 right-0 flex items-center pr-3">
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => sendMessage()}
|
||||||
console.log(reactFlowInstance.toObject())
|
|
||||||
if(chatValue !== ""){
|
|
||||||
if(validateNodes()){
|
|
||||||
if(validateChatNodes()){
|
|
||||||
let message = chatValue;
|
|
||||||
setChatValue("");
|
|
||||||
addChatHistory(message, true);
|
|
||||||
sendAll({...reactFlowInstance.toObject(),message}).then((r) => {addChatHistory(r.data.result, false);});
|
|
||||||
} else {
|
|
||||||
setErrorData({title: 'Error sending message', list:['Chat nodes are missing.']})
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
setErrorData({title: 'Error sending message', list:['There are required fields not filled yet.']})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
setErrorData({title: 'Error sending message', list:['The message cannot be empty.']})
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
<PaperAirplaneIcon
|
<PaperAirplaneIcon
|
||||||
className="h-5 w-5 text-gray-400 hover:text-gray-600"
|
className="h-5 w-5 text-gray-400 hover:text-gray-600"
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { createContext, useEffect, useState, useRef } from "react";
|
import { createContext, useEffect, useState, useRef } from "react";
|
||||||
|
|
||||||
type flow={name:string,id:string,data:any}
|
type flow={name:string,id:string,data:any,chat:Array<{message:string,isSend:boolean}>}
|
||||||
|
|
||||||
type TabsContextType={
|
type TabsContextType={
|
||||||
tabIndex:number;
|
tabIndex:number;
|
||||||
|
|
@ -74,7 +74,7 @@ export function TabsProvider({children}){
|
||||||
}
|
}
|
||||||
function addFlow(flowData?:flow) {
|
function addFlow(flowData?:flow) {
|
||||||
const data = flowData?flowData:null
|
const data = flowData?flowData:null
|
||||||
let newFlow: flow = {name: "flow"+id, id: id.toString(), data}
|
let newFlow: flow = {name: "flow"+id, id: id.toString(), data,chat:[]}
|
||||||
setId((old) => old+1);
|
setId((old) => old+1);
|
||||||
setFlows(prevState => {
|
setFlows(prevState => {
|
||||||
const newFlows = [...prevState, newFlow];
|
const newFlows = [...prevState, newFlow];
|
||||||
|
|
|
||||||
|
|
@ -173,7 +173,7 @@ export default function FlowPage({ flow }) {
|
||||||
<Background />
|
<Background />
|
||||||
<Controls></Controls>
|
<Controls></Controls>
|
||||||
</ReactFlow>
|
</ReactFlow>
|
||||||
<Chat reactFlowInstance={reactFlowInstance} />
|
<Chat flow={flow} reactFlowInstance={reactFlowInstance} />
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<></>
|
<></>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue