Commit
·
a81564e
1
Parent(s):
1dec6f5
Add conversation management with WebSocket integration in ChatScreen
Browse files
src/pages/chatArea/components/ChatScreen.tsx
CHANGED
|
@@ -3,36 +3,44 @@ import ArrowUpwardIcon from "@mui/icons-material/ArrowUpward";
|
|
| 3 |
import GraphicEqIcon from "@mui/icons-material/GraphicEq";
|
| 4 |
import PersonIcon from "@mui/icons-material/Person";
|
| 5 |
import { Box, IconButton, InputBase, Menu, MenuItem } from "@mui/material";
|
| 6 |
-
import React, { useState, useContext } from "react";
|
| 7 |
import { useNavigate } from "react-router-dom";
|
| 8 |
|
| 9 |
import { AuthContext } from "../../../context/AuthContext";
|
|
|
|
|
|
|
| 10 |
import { logout } from "../../../services/api/authService";
|
| 11 |
-
import {
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
const [input, setInput] = useState<string>("");
|
| 16 |
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
const authContext = useContext(AuthContext);
|
| 18 |
const navigate = useNavigate();
|
| 19 |
|
| 20 |
-
const generateBotResponse = (userMessage: string) => {
|
| 21 |
-
if (userMessage.trim().toLowerCase() === "hello") {
|
| 22 |
-
return "Hi! How can I assist you today?";
|
| 23 |
-
}
|
| 24 |
-
return "I am not sure how to respond to that.";
|
| 25 |
-
};
|
| 26 |
-
|
| 27 |
const handleSendMessage = () => {
|
| 28 |
if (input.trim()) {
|
| 29 |
-
|
| 30 |
setInput("");
|
| 31 |
-
|
| 32 |
-
setTimeout(() => {
|
| 33 |
-
const botResponse = generateBotResponse(input);
|
| 34 |
-
setMessages((prevMessages) => [...prevMessages, { text: botResponse, sender: "bot" }]);
|
| 35 |
-
}, 1000);
|
| 36 |
}
|
| 37 |
};
|
| 38 |
|
|
@@ -51,8 +59,6 @@ const ChatScreen: React.FC<IChatScreenProps> = ({ isOpen, toggleSidebar }) => {
|
|
| 51 |
const handleLogout = async () => {
|
| 52 |
try {
|
| 53 |
await logout();
|
| 54 |
-
} catch (error) {
|
| 55 |
-
console.error("Logout failed", error);
|
| 56 |
} finally {
|
| 57 |
authContext?.logout();
|
| 58 |
}
|
|
|
|
| 3 |
import GraphicEqIcon from "@mui/icons-material/GraphicEq";
|
| 4 |
import PersonIcon from "@mui/icons-material/Person";
|
| 5 |
import { Box, IconButton, InputBase, Menu, MenuItem } from "@mui/material";
|
| 6 |
+
import React, { useState, useContext, useEffect } from "react";
|
| 7 |
import { useNavigate } from "react-router-dom";
|
| 8 |
|
| 9 |
import { AuthContext } from "../../../context/AuthContext";
|
| 10 |
+
import useWebSocket from "../../../hooks/useTextWebhook";
|
| 11 |
+
import { ICreateConversationResponse } from "../../../interfaces/conversation";
|
| 12 |
import { logout } from "../../../services/api/authService";
|
| 13 |
+
import { createConversation } from "../../../services/api/chatService";
|
| 14 |
|
| 15 |
+
interface ChatScreenProps {
|
| 16 |
+
isOpen: boolean;
|
| 17 |
+
toggleSidebar: () => void;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
const ChatScreen: React.FC<ChatScreenProps> = ({ isOpen, toggleSidebar }) => {
|
| 21 |
const [input, setInput] = useState<string>("");
|
| 22 |
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
|
| 23 |
+
|
| 24 |
+
const [conversation, setConversation] = useState<string>("");
|
| 25 |
+
|
| 26 |
+
useEffect(() => {
|
| 27 |
+
const fetchConversation = async () => {
|
| 28 |
+
const conv = await createConversation({ modality: "text" });
|
| 29 |
+
const sessionData: ICreateConversationResponse = await conv.data;
|
| 30 |
+
const conversationId = sessionData.conversation_id;
|
| 31 |
+
setConversation(conversationId);
|
| 32 |
+
};
|
| 33 |
+
fetchConversation();
|
| 34 |
+
}, []);
|
| 35 |
+
|
| 36 |
+
const { messages, sendMessage } = useWebSocket(conversation);
|
| 37 |
const authContext = useContext(AuthContext);
|
| 38 |
const navigate = useNavigate();
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
const handleSendMessage = () => {
|
| 41 |
if (input.trim()) {
|
| 42 |
+
sendMessage(input.trim());
|
| 43 |
setInput("");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
};
|
| 46 |
|
|
|
|
| 59 |
const handleLogout = async () => {
|
| 60 |
try {
|
| 61 |
await logout();
|
|
|
|
|
|
|
| 62 |
} finally {
|
| 63 |
authContext?.logout();
|
| 64 |
}
|