"use client" import { useState } from "react" import Link from "next/link" import { useRouter } from "next/navigation" import { toast } from "sonner" import { Bell, CheckCheck, MessageSquare, Megaphone, PenTool, GraduationCap } from "lucide-react" import { Badge } from "@/shared/components/ui/badge" import { Button } from "@/shared/components/ui/button" import { Card, CardContent } from "@/shared/components/ui/card" import { EmptyState } from "@/shared/components/ui/empty-state" import { formatDate } from "@/shared/lib/utils" import { markAllNotificationsAsReadAction, markNotificationAsReadAction } from "../actions" import type { Notification, NotificationType } from "../types" const TYPE_ICON: Record = { message: MessageSquare, announcement: Megaphone, homework: PenTool, grade: GraduationCap, } const TYPE_LABEL: Record = { message: "Message", announcement: "Announcement", homework: "Homework", grade: "Grade", } export function NotificationList({ notifications }: { notifications: Notification[] }) { const router = useRouter() const [isWorking, setIsWorking] = useState(false) const hasUnread = notifications.some((n) => !n.isRead) const handleMarkAllRead = async () => { setIsWorking(true) try { const res = await markAllNotificationsAsReadAction() if (res.success) { toast.success(res.message) router.refresh() } else { toast.error(res.message || "Failed to mark all as read") } } catch { toast.error("Failed to mark all as read") } finally { setIsWorking(false) } } const handleMarkRead = async (id: string) => { try { const res = await markNotificationAsReadAction(id) if (res.success) { router.refresh() } } catch { toast.error("Failed to mark as read") } } return (

Notifications

Stay updated on your latest activities.

{hasUnread ? ( ) : null}
{notifications.length === 0 ? ( ) : (
{notifications.map((n) => { const Icon = TYPE_ICON[n.type] ?? Bell return (
{n.title} {!n.isRead ? New : null}
{n.content ? (

{n.content}

) : null}
{TYPE_LABEL[n.type]} {formatDate(n.createdAt)} {!n.isRead ? ( ) : null} {n.link ? ( View ) : null}
) })}
)}
) }