From b9d73266c06e8a0348fa8dbf6ee0686f7b6f8f2f Mon Sep 17 00:00:00 2001 From: Namit Bhutani Date: Mon, 22 Jul 2024 22:15:00 +0530 Subject: [PATCH 1/6] feat: popup toast for unread notifications --- frontend/src/About.tsx | 2 +- frontend/src/components/announcements.tsx | 71 ++++++++++++++++++++--- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/frontend/src/About.tsx b/frontend/src/About.tsx index e91ffd72..6d9ebb75 100644 --- a/frontend/src/About.tsx +++ b/frontend/src/About.tsx @@ -58,7 +58,7 @@ function About() { . We thank Arunachala AM, Jason Goveas, Anurav Garg, Dharanikanth Reddy, Karthik Prakash, Shreyash Dash, Kovid Lakhera, Palaniappan R, - Meghraj Goswami, Soumitra Shewale, Kishan Abijay, Namit Bhutani, + Meghraj Goswami, Soumitra Shewale, Kishan Abijay, Namit Bhutani, Chaitanya Keyal and Samir Khairati for this rewrite of ChronoFactorem.

diff --git a/frontend/src/components/announcements.tsx b/frontend/src/components/announcements.tsx index f2e26a8c..617adbb3 100644 --- a/frontend/src/components/announcements.tsx +++ b/frontend/src/components/announcements.tsx @@ -1,3 +1,4 @@ +import { useState, useEffect } from "react"; import { Dialog, DialogContent, @@ -5,9 +6,9 @@ import { DialogHeader, DialogTitle, } from "@/components/ui/dialog"; +import { useToast } from "@/components/ui/use-toast"; import { useQuery } from "@tanstack/react-query"; import axios from "axios"; - import { DialogTrigger } from "@radix-ui/react-dialog"; import { Megaphone } from "lucide-react"; import type { z } from "zod"; @@ -29,6 +30,37 @@ function Announcements() { queryFn: fetchAnnouncements, }); + const { toast } = useToast(); + + const [readAnnouncements, setReadAnnouncements] = useState(() => { + const storedReadAnnouncements = localStorage.getItem("readAnnouncements"); + return storedReadAnnouncements ? JSON.parse(storedReadAnnouncements) : []; + }); + + useEffect(() => { + localStorage.setItem( + "readAnnouncements", + JSON.stringify(readAnnouncements), + ); + }, [readAnnouncements]); + + useEffect(() => { + const unreadAnnouncements = announcements?.filter( + (announcement) => !readAnnouncements.includes(announcement.id), + ); + + if (unreadAnnouncements && unreadAnnouncements.length > 0) { + toast({ + title: "New Announcements", + description: `You have ${unreadAnnouncements.length} unread announcement(s).`, + }); + } + }, [announcements, readAnnouncements, toast]); + + const markAsRead = (id: string) => { + setReadAnnouncements((prev) => [...prev, id]); + }; + return ( @@ -44,13 +76,29 @@ function Announcements() {
{Array.isArray(announcements) && announcements?.length ? ( announcements - ?.sort( - (a, b) => - new Date(a.createdAt as string).getTime() - - new Date(b.createdAt as string).getTime(), - ) + ?.sort((a, b) => { + const isUnreadA = !readAnnouncements.includes(a.id); + const isUnreadB = !readAnnouncements.includes(b.id); + + if (isUnreadA !== isUnreadB) { + return isUnreadA ? -1 : 1; + } + + return ( + new Date(b.createdAt as string).getTime() - + new Date(a.createdAt as string).getTime() + ); + }) + .reverse() .map((announcement) => ( -
+

{announcement.title}

@@ -60,6 +108,15 @@ function Announcements() { ).toLocaleString()}

{announcement.message}

+ {!readAnnouncements.includes(announcement.id) && ( + + )}
)) ) : ( From 78b1166a2ca81459ae10a3155a9a35256defc7b0 Mon Sep 17 00:00:00 2001 From: kishan1735 Date: Mon, 22 Jul 2024 23:45:16 +0530 Subject: [PATCH 2/6] fix: add margin to read button --- frontend/src/components/announcements.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/frontend/src/components/announcements.tsx b/frontend/src/components/announcements.tsx index 617adbb3..de2b435a 100644 --- a/frontend/src/components/announcements.tsx +++ b/frontend/src/components/announcements.tsx @@ -111,6 +111,7 @@ function Announcements() { {!readAnnouncements.includes(announcement.id) && (