diff --git a/frontend/src/components/chat/messages/ValidChat.jsx b/frontend/src/components/chat/messages/ValidChat.jsx index 6dde101..89514dc 100644 --- a/frontend/src/components/chat/messages/ValidChat.jsx +++ b/frontend/src/components/chat/messages/ValidChat.jsx @@ -1,6 +1,6 @@ import { useEffect, useRef, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; -import { Hash, Pencil, Trash2, Save, SendHorizontal, Loader2, AlertCircle, Pin } from "lucide-react"; +import { Hash, Pencil, Trash2, Save, SendHorizontal, Loader2, AlertCircle,Reply, Pin, X } from "lucide-react"; import socket from "../../socket/Socket"; import { useParams } from "react-router-dom"; import { clear_channel_unread } from "../../../store/unreadSlice"; @@ -32,47 +32,11 @@ function ValidChat() { const [editingContent, setEditingContent] = useState(""); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); - const [typingUsers, setTypingUsers] = useState({}); - const messagesEndRef = useRef(null); - const typingTimeoutRef = useRef(null); - const typingUserTimeoutsRef = useRef({}); - const isTypingRef = useRef(false); - - const stopTyping = () => { - if (!isTypingRef.current) { - return; - } - - clearTimeout(typingTimeoutRef.current); - isTypingRef.current = false; - socket.emit("server_stop_typing", { channel_id, server_id }); - }; - - const handleMessageChange = (e) => { - const nextMessage = e.target.value; - setchat_message(nextMessage); - - if (!channel_id || !server_id || !id) { - return; - } - - if (!nextMessage.trim()) { - stopTyping(); - return; - } - - if (!isTypingRef.current) { - isTypingRef.current = true; - socket.emit("server_typing", { - channel_id, - server_id, - username, - }); - } - - clearTimeout(typingTimeoutRef.current); - typingTimeoutRef.current = setTimeout(stopTyping, 2000); - }; + const [replyTo, setReplyTo] = useState(null); + const [showPinned, setShowPinned] = useState(false); + const pinnedMessages = all_messages.filter( + (message) => message.isPinned + ); useEffect(() => { if(socket && channel_id){ @@ -109,12 +73,16 @@ function ValidChat() { tag, id, profile_pic, + replyTo, }), }); const data = await res.json(); if (data.status !== 200) { setchat_message(chat_message); } + if (data.status === 200) { + setReplyTo(null); + } }; useEffect(() => { @@ -231,32 +199,31 @@ function ValidChat() { }; const togglePinMessage = async (message) => { - const res = await fetch(`${url}/chat/toggle_server_message_pin`, { - method: "POST", - headers: { - "Content-Type": "application/json", - "x-auth-token": localStorage.getItem("token"), - }, - body: JSON.stringify({ - server_id, - channel_id, - timestamp: message.timestamp, - sender_id: message.sender_id, - }), - }); - - const data = await res.json(); - if (data.status === 200) { - setall_messages((currentMessages) => - currentMessages.map((entry) => - String(entry.timestamp) === String(message.timestamp) && - String(entry.sender_id) === String(message.sender_id) - ? { ...entry, is_pinned: data.is_pinned } - : entry - ) - ); - } - }; + const res = await fetch(`${url}/chat/toggle_pin_message`, { + method: "POST", + headers: { + "Content-Type": "application/json", + "x-auth-token": localStorage.getItem("token"), + }, + body: JSON.stringify({ + server_id, + channel_id, + timestamp: message.timestamp, + }), + }); + + const data = await res.json(); + + if (data.status === 200) { + setall_messages((currentMessages) => + currentMessages.map((entry) => + String(entry.timestamp) === String(message.timestamp) + ? { ...entry, isPinned: data.isPinned } + : entry + ) + ); + } +}; useEffect(() => { const handleReceiveMessage = (messageData) => { @@ -306,77 +273,26 @@ function ValidChat() { ) ); }; - - const handlePinUpdatedMessage = (message_data) => { + const handlePinUpdated = (message_data) => { setall_messages((currentMessages) => (currentMessages || []).map((entry) => - String(entry.timestamp) === String(message_data.timestamp) && - entry.sender_id === message_data.sender_id - ? { ...entry, is_pinned: message_data.is_pinned } + String(entry.timestamp) === String(message_data.timestamp) + ? { ...entry, isPinned: message_data.isPinned } : entry ) ); }; - - const handleTyping = (typingData) => { - if ( - String(typingData?.server_id) !== String(server_id) || - String(typingData?.channel_id) !== String(channel_id) || - String(typingData?.from) === String(id) - ) { - return; - } - - setTypingUsers((currentUsers) => ({ - ...currentUsers, - [String(typingData.from)]: typingData.username || "Someone", - })); - - clearTimeout(typingUserTimeoutsRef.current[String(typingData.from)]); - typingUserTimeoutsRef.current[String(typingData.from)] = setTimeout(() => { - setTypingUsers((currentUsers) => { - const nextUsers = { ...currentUsers }; - delete nextUsers[String(typingData.from)]; - return nextUsers; - }); - delete typingUserTimeoutsRef.current[String(typingData.from)]; - }, 3000); - }; - - const handleStopTyping = (typingData) => { - if ( - String(typingData?.server_id) !== String(server_id) || - String(typingData?.channel_id) !== String(channel_id) - ) { - return; - } - - setTypingUsers((currentUsers) => { - const nextUsers = { ...currentUsers }; - delete nextUsers[String(typingData.from)]; - return nextUsers; - }); - clearTimeout(typingUserTimeoutsRef.current[String(typingData.from)]); - delete typingUserTimeoutsRef.current[String(typingData.from)]; - }; - //earlier it was server_message_receive which was wrong socket.on("server_message_received", handleReceiveMessage); socket.on("server_message_updated", handleUpdatedMessage); socket.on("server_message_deleted", handleDeletedMessage); - socket.on("server_message_pin_updated", handlePinUpdatedMessage); - socket.on("server_typing", handleTyping); - socket.on("server_stop_typing", handleStopTyping); + socket.on("server_message_pin_updated", handlePinUpdated); return () => { socket.off("server_message_received", handleReceiveMessage); socket.off("server_message_updated", handleUpdatedMessage); socket.off("server_message_deleted", handleDeletedMessage); - socket.off("server_message_pin_updated", handlePinUpdatedMessage); - socket.off("server_typing", handleTyping); - socket.off("server_stop_typing", handleStopTyping); - Object.values(typingUserTimeoutsRef.current).forEach(clearTimeout); - typingUserTimeoutsRef.current = {}; + socket.off("server_message_pin_updated", handlePinUpdated); }; }, [channel_id, id, server_id]); @@ -408,8 +324,18 @@ function ValidChat() {