diff --git a/src/components/CommunityChat/index.css b/src/components/CommunityChat/index.css index 6449131de..d3dd4828a 100644 --- a/src/components/CommunityChat/index.css +++ b/src/components/CommunityChat/index.css @@ -109,6 +109,13 @@ color: var(--profile-color); } +.date-separator { + padding: 1.5rem 0; + list-style-type: none; + font-size: 15px; + color: var(--darkmode-white); +} + .current-user-msg { flex-direction: row-reverse; margin-left: auto; @@ -277,6 +284,10 @@ } @media screen and (max-width: 600px) { + .date-separator { + font-size: 9px; + } + .chat-msg-text { padding: 6px 9px; } diff --git a/src/components/CommunityChat/index.jsx b/src/components/CommunityChat/index.jsx index eccf8921a..c881f9ba6 100644 --- a/src/components/CommunityChat/index.jsx +++ b/src/components/CommunityChat/index.jsx @@ -1,10 +1,10 @@ import "./index.css"; +import { ClickAwayListener, Divider } from "@mui/material"; +import { Fragment, useEffect, useRef, useState } from "react"; import { auth, db } from "../../lib/firebase"; import { playErrorSound, playSuccessSound } from "../../js/sounds"; -import { useEffect, useRef, useState } from "react"; -import { ClickAwayListener } from "@mui/material"; import CloseIcon from "@mui/icons-material/Close"; import DeleteIcon from "@mui/icons-material/DeleteOutlineOutlined"; import EditIcon from "@mui/icons-material/EditOutlined"; @@ -196,11 +196,12 @@ const ChatBox = () => { useEffect(() => { const scrollTop = () => { - window.scrollTo({ top: window.innerHeight + 800 }); + window.scrollTo({ top: document.body.scrollHeight, behavior: "smooth" }); + // Scroll to the bottom of the page + // TODO: Fix this + // chatMsgContainerRef.current.scrollTop = chatMsgContainerRef.current.scrollHeight }; - if (!isLastMsgRecieved) { - scrollTop(); - } + scrollTop(); }, [messages]); //Load messages for the first time @@ -250,7 +251,6 @@ const ChatBox = () => { ...loadedMsgs, ]; }); - if (querySnapshot.empty) { setIsLastMsgRecieved(true); } @@ -264,6 +264,44 @@ const ChatBox = () => { }; }, [loadMoreMsgs]); + // Function to format the message creation date + const formatDate = (timestamp) => { + const messageDate = new Date(timestamp * 1000); // Convert seconds to milliseconds + const today = new Date(); + const yesterday = new Date(today); + yesterday.setDate(today.getDate() - 1); + + const options = { day: "2-digit", month: "2-digit", year: "numeric" }; + + if (messageDate.toDateString() === today.toDateString()) { + return "Today"; + } else if (messageDate.toDateString() === yesterday.toDateString()) { + return "Yesterday"; + } else { + // Format the actual date as 'dd/mm/yyyy' + return messageDate.toLocaleDateString("en-IN", options); + } + }; + + // Function to group messages by date + const groupMessagesByDate = (messages) => { + const groupedMessages = {}; + messages.forEach((message) => { + const formattedDate = formatDate(message?.createdAt?.seconds); + if (!groupedMessages[formattedDate]) { + groupedMessages[formattedDate] = []; + } + groupedMessages[formattedDate].push(message); + }); + return groupedMessages; + }; + + const [groupedMessages, setGroupedMessages] = useState({}); + + useEffect(() => { + setGroupedMessages(groupMessagesByDate(messages)); + }, [messages]); + return ( <>
@@ -278,85 +316,104 @@ const ChatBox = () => { ) : (