Skip to content

feat: Grouped messages based on date and added time for the messages #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 90 additions & 8 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"date-fns": "^3.6.0",
"emoji-mart": "^5.6.0",
"gender-detection-from-name": "^1.8.0",
"jwt-decode": "^3.1.2",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/ChatComponents/ChatBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function ChatBar({data,select}) {
</div>
</div>
<div className='flex flex-col items-end'>
<div className='text-xs max-[800px]:hidden font-medium cursor-pointer text-[#979797]' >{`${String(dateObject.getHours()%12).padStart(2,'0')}:${String(dateObject.getMinutes()).padStart(2,'0')} ${dateObject.getHours()>=12?'PM':'AM'}`}</div>
<div className='text-xs max-[800px]:hidden font-medium cursor-pointer text-[#979797]' >{`${String(dateObject.getHours()%12 || 12).padStart(2,'0')}:${String(dateObject.getMinutes()).padStart(2,'0')} ${dateObject.getHours()>=12?'PM':'AM'}`}</div>
<div className='mt-1'>
{data.notify&&<Badge>1</Badge>}
</div>
Expand Down
81 changes: 55 additions & 26 deletions frontend/src/components/ChatComponents/ChatMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { updateChatBar } from "../../services/Actions/Chat/action";
import useSound from "use-sound";
import { addIncomingUserChatBar } from "../../services/Actions/Chat/action";
import notifySound from "../../assets/sounds/notification.mp3";
import { format, isToday, isYesterday } from "date-fns";

export default function ChatMessages() {
const isSet = useSelector((state) => state.chat.activeChat);
Expand All @@ -35,12 +36,11 @@ export default function ChatMessages() {
const isChatBarPresent = AllChats.find(
(val) => val._id === newMessageRecieved.chat._id
);

console.log("Lets test");
if (!isChatBarPresent)
{
if (!isChatBarPresent) {
dispatch(addIncomingUserChatBar(newMessageRecieved.chat));
dispatch(updateChatBar(newMessageRecieved.chat._id, newMessageRecieved.content));
dispatch(
updateChatBar(newMessageRecieved.chat._id, newMessageRecieved.content)
);
return;
}

Expand Down Expand Up @@ -109,8 +109,26 @@ export default function ChatMessages() {
clearTimeout(timer);
};
}, [data]);

if (isSet === null) return <Advertisement></Advertisement>;

const formatDateHeader = (date) => {
const messageDate = new Date(date);
if (isToday(messageDate)) {
return "Today";
} else if (isYesterday(messageDate)) {
return "Yesterday";
} else {
return format(messageDate, "d MMMM yyyy");
}
};

const isMessageNewDay = (current, previous) =>
!previous ||
formatDateHeader(current.createdAt) !==
formatDateHeader(previous.createdAt);



return (
<div
Expand All @@ -122,28 +140,39 @@ export default function ChatMessages() {
{!isLoading && data.length > 0 && (
<>
{data.map((val, index) => {
if (isSender(val.sender._id))
return (
<SenderMessage
content={val.content}
key={index}
></SenderMessage>
);
else
return (
<RecieverMessage
isGroupChat={isSet.isGroupChat}
name={val.sender.name}
img={val.sender.pic}
messages={data}
index={index}
content={val.content}
key={index}
></RecieverMessage>
);
const showDateHeader = isMessageNewDay(val, data[index - 1]);
return (
<div key={index}>
{showDateHeader && (
<div className="flex justify-center">
<div className=" rounded-md px-2.5 py-1.5 my-4 bg-slate-200 text-slate-600 font-normal text-sm">
{formatDateHeader(val.createdAt)}
</div>
</div>
)}
{isSender(val.sender._id) ? (
<SenderMessage
time={val.createdAt}
content={val.content}
key={index}
></SenderMessage>
) : (
<RecieverMessage
isGroupChat={isSet.isGroupChat}
name={val.sender.name}
img={val.sender.pic}
messages={data}
index={index}
content={val.content}
time={val.createdAt}
key={index}
></RecieverMessage>
)}
</div>
);
})}
</>
)}
</div>
);
}
}
Loading