diff --git a/client/src/components/Negotiate/Chat.jsx b/client/src/components/Negotiate/Chat.jsx index 798c4c1..8c8c9d2 100644 --- a/client/src/components/Negotiate/Chat.jsx +++ b/client/src/components/Negotiate/Chat.jsx @@ -25,88 +25,110 @@ export default function Chat() { const [prevRoomID, setPrevRoomID] = useState(null); const [userType, setUserType] = useState('buyers'); const [contacts, setContacts] = useState(null); - const [message,setmessage]=useState(); - const[messages, setMessages] = useState(); + const [message, setmessage] = useState(); + const [messages, setMessages] = useState([]); const navigate = useNavigate(); - const [currperson,setperson]=useState(); + const [currperson, setperson] = useState(); const [loading, setLoading] = useState(true); - useEffect(()=>{ - if(!currentUser){ + const chatDisplayRef = useRef(null); // Ref to chat display container + const bottomRef = useRef(null); // Ref to the last message to scroll into view + + useEffect(() => { + if (!currentUser) { return; } //establish new socket connection const newSocket = io.connect(`http://localhost:${SERVER_PORT}`); setSocket(newSocket); - loadContacts(currentUser.uid, userType).then((result)=>{ + loadContacts(currentUser.uid, userType).then((result) => { setContacts(result); setLoading(false); }) - console.log("current user id ", currentUser.uid); + // console.log("current user id ", currentUser.uid); return () => { - console.log("disconnected"); + // console.log("disconnected"); newSocket.disconnect(); }; - },[currentUser]) + }, [currentUser]) - useEffect(()=>{ - if(contacts!=null){ + useEffect(() => { + if (contacts != null) { // console.log("contacts ", contacts); } - },[contacts]) + }, [contacts]) - useEffect(()=>{ + useEffect(() => { //monitor events - if(socket){ + if (socket) { socket.on("connect", () => { - console.log("current socket ID:", socket.id); // Now it's guaranteed to be defined + // console.log("current socket ID:", socket.id); // Now it's guaranteed to be defined }); - socket.on("recieve-message", (received_message) => { - console.log("recieved message ", received_message.message, " from room ,", received_message.room); - setMessages((prevMessages) => [...prevMessages, received_message]); + socket.on("recieve-message", (data) => { + // console.log(data); + const newMessage = { user: data.user, text: data.text, room : data.room }; + setMessages((prevMessages) => [...prevMessages, newMessage]); }); - socket.on("joined-room", (room)=>{ - console.log(`${socket.id} joined room ${room}`); + socket.on("joined-room", (room) => { + // console.log(`${socket.id} joined room ${room}`); }) - socket.on("left-room", (room)=>{ - console.log(`${socket.id} left room ${room}`); + socket.on("left-room", (room) => { + // console.log(`${socket.id} left room ${room}`); }) } - return ()=>{ - if(socket){ + return () => { + if (socket) { socket.off("connect"); socket.off("recieve-message"); socket.off("joined-room"); socket.off("left-room"); } } - }, [socket]) + }, [socket, messages]) - useEffect(()=>{ + useEffect(() => { //switch rooms/contacts - if(socket){ - switchRooms(socket, prevRoomID, currentRoomID); - loadMessage(currentRoomID).then((result)=>{setMessages(result)}); - } + if (socket) { + switchRooms(socket, prevRoomID, currentRoomID); + loadMessage(currentRoomID).then((result) => { setMessages(result) }); + } }, [currentRoomID]) - useEffect(()=>{ - console.log(messages); - + useEffect(() => { + // console.log("messages array updated"); + if (bottomRef.current) { + bottomRef.current.scrollIntoView({ behavior: 'smooth' }); + } }, [messages]) - const handleClick = async ()=>{ - //broadcast message - await socket.emit("send-message", {message : `message from ${socket.id}`, room:currentRoomID}); + const handleClick = async (event) => { + if (message.trim()){ + if(currentRoomID){ + // Optimistically add the message to the messages state + const newMessage = { user: currentUser.uid, text: message, room : currentRoomID }; + + setMessages((prevMessages) => [...prevMessages, newMessage]); + // Clear input + event.target.value=""; + setmessage(''); + // Emit the message to the server + await socket.emit("send-message", newMessage); + // console.log("Sent message:", message); + + // Upload message to Firebase + uploadMessage(currentRoomID, message, currentUser.uid); + }else{ + console.log("select room"); + } + } - //upload message to database - uploadMessage(currentRoomID,message,currentUser.uid); - } + }; + const handleChange = (event) => { setmessage(event.target.value); }; @@ -120,67 +142,48 @@ export default function Chat() { } return ( -
-
-
-
- navigate()}> -

Chats

-
-
- {/*
- - lavi +
+
+
+
+ navigate()}> +

Chats

-
- - supreeth -
-
- - dilip -
-
- - isha +
+ {Object.keys(contacts).map((key) => { + const contact = contacts[key]; + return ( + + ); + })}
-
- - samyak -
*/} - {Object.keys(contacts).map((key) => { - const contact = contacts[key]; - return ( - - ); - })}
-
-
-
- - - {currperson} +
+
+ + + {currperson} -
- - - +
+ + + +
-
-
+
{messages && messages.length > 0 ? ( messages.map((msg, index) => (
No messages yet.
)} +
{/* Scroll target */} +
+
+