import React, { useEffect, useState, useRef } from "react"; import { useNavigate } from "react-router-dom"; import "./css/Chat.css"; import { FaPaperPlane, FaSmile } from "react-icons/fa"; import { get, post } from "../backend/api"; import { displayMessage } from "../backend/notifications/notifications"; import { MessageType } from "../backend/notifications/message"; import io from "socket.io-client"; const emojis = [ "๐", "๐", "๐", "๐คฃ", "๐", "๐", "๐ ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ฅฐ", "๐", "๐", "๐", "๐", "๐ค", "๐คฉ", "๐ค", "๐", "๐", "๐ถ", "๐", "๐", "๐ฃ", "๐ฅ", "๐ฎ", "๐ค", "๐ฏ", "๐ช", "๐ซ", "๐ด", "๐", "๐", "๐", "๐คช", "๐คจ", "๐", "๐ค", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐คฌ", "๐ฑ", "๐จ", "๐ง", "๐", "๐ฅณ", "๐ฅบ", "๐ป", "๐ผ", "๐ฝ", "๐", "๐", "๐", "๐", "๐ป", "๐ฝ", ]; const Chat = () => { const [interlocutorId, setInterlocutorId] = useState(""); const [messages, setMessages] = useState([]); const [newMessage, setNewMessage] = useState(""); const [showEmojiPicker, setShowEmojiPicker] = useState(false); const socket = useRef(null); const chatRef = useRef(null); const navigate = useNavigate(); const [myId, setMyId] = useState(""); useEffect(() => { const id = localStorage.getItem("interlocutorId"); setInterlocutorId(id); const username = localStorage.getItem("username"); setMyId(username); if (!id || !username) { displayMessage("You are not logged in!", MessageType.WARN); return () => {}; } socket.current = io("http://localhost:8099"); socket.current.on("receiveMessage", (message) => { setMessages((prev) => [...prev, message]); }); socket.current.on("connect_error", (err) => { console.error("Connection Error:", err.message); }); return () => { socket.current.disconnect(); }; }, []); useEffect(() => { // retrieveMessages().then(); const interval = setInterval(() => { retrieveMessages().then() }, 2000); return () => clearInterval(interval) }, [myId, interlocutorId]); useEffect(() => { if (chatRef.current) { chatRef.current.scrollTop = chatRef.current.scrollHeight; } }, [messages]); async function sendMessageToDB(messageData) { const { ok, data } = await post( "/chat/message/" + myId + "/" + interlocutorId, { message: messageData } ); if (!ok) { displayMessage(data.message, MessageType.ERROR); } } async function retrieveMessages() { if (!myId || !interlocutorId) return; const { ok, data } = await get("/chat/item/" + myId + "/" + interlocutorId); if (!ok) { displayMessage(data.message, MessageType.ERROR); return; } setMessages(data.chat.messages); } const sendMessage = () => { if (newMessage.trim()) { const messageData = { senderId: myId, recipientId: interlocutorId, data: newMessage, timestamp: new Date().toLocaleString(), }; socket.current.emit("sendMessage", messageData); setMessages((prev) => [...prev, messageData]); sendMessageToDB(messageData).then(); setNewMessage(""); } }; const handleKeyPress = (e) => { if (e.key === "Enter") { sendMessage(); } }; const handleEmojiSelect = (emoji) => { setNewMessage((prev) => prev + emoji); setShowEmojiPicker(false); }; return (