tahamajs/IE / Projects /front /src /components /dashboard /NotificationsList.tsx
tahamajs's picture
download
raw
4.22 kB
import { useNavigate } from "react-router-dom";
import { Notification } from "@/types";
import { toast } from "react-toastify";
import { useState, useCallback, useRef, useEffect } from "react";
import { Bell } from "lucide-react";
interface NotificationsListProps {
notifications: Notification[];
onMarkRead: (id: number) => Promise<void>;
onLoadMore?: () => Promise<void>;
hasMore?: boolean;
loadingMore?: boolean;
}
export function NotificationsList({
notifications: rawNotifications,
onMarkRead,
onLoadMore,
hasMore = false,
loadingMore = false,
}: NotificationsListProps) {
const notifications = Array.isArray(rawNotifications) ? rawNotifications : [];
const navigate = useNavigate();
const observerRef = useRef<IntersectionObserver | null>(null);
const loadMoreRef = useRef<HTMLDivElement>(null);
const [pendingMarkRead, setPendingMarkRead] = useState<Set<number>>(new Set());
const handleMarkRead = useCallback(
async (id: number) => {
if (pendingMarkRead.has(id)) return;
setPendingMarkRead((prev) => new Set(prev).add(id));
try {
await onMarkRead(id);
toast.success("Marked as read");
} catch (err) {
toast.error("Could not mark as read");
setPendingMarkRead((prev) => {
const next = new Set(prev);
next.delete(id);
return next;
});
}
},
[onMarkRead, pendingMarkRead]
);
// Infinite scroll with IntersectionObserver
useEffect(() => {
if (!onLoadMore || !hasMore || loadingMore) return;
observerRef.current = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
onLoadMore();
}
},
{ threshold: 0.1 }
);
if (loadMoreRef.current) observerRef.current.observe(loadMoreRef.current);
return () => observerRef.current?.disconnect();
}, [onLoadMore, hasMore, loadingMore]);
const unreadCount = notifications.filter((n) => !n.read).length;
return (
<div className="bg-white dark:bg-gray-800 rounded-2xl shadow-md overflow-hidden">
<div className="px-6 py-4 border-b flex justify-between items-center">
<h3 className="font-semibold text-lg flex items-center gap-2">
<Bell className="w-5 h-5 text-primary" /> Notifications
</h3>
{unreadCount > 0 && (
<span className="bg-red-500 text-white text-xs px-2 py-0.5 rounded-full">
{unreadCount} new
</span>
)}
</div>
<div className="max-h-80 overflow-y-auto divide-y divide-gray-100 dark:divide-gray-700">
{notifications.length === 0 ? (
<div className="p-6 text-center text-gray-500">No notifications</div>
) : (
notifications.slice(0, 10).map((notif) => {
const isPending = pendingMarkRead.has(notif.id);
const isRead = notif.read || isPending;
return (
<div
key={notif.id}
className={`p-4 cursor-pointer transition ${
!isRead ? "bg-blue-50 dark:bg-gray-700/50" : "hover:bg-gray-50 dark:hover:bg-gray-700/30"
}`}
onClick={() => !isRead && handleMarkRead(notif.id)}
role="button"
tabIndex={0}
onKeyDown={(e) => e.key === "Enter" && !isRead && handleMarkRead(notif.id)}
aria-label={isRead ? "Notification (read)" : "Mark as read"}
>
<p className="text-sm">{notif.message}</p>
<span className="text-xs text-gray-400">
{new Date(notif.createdAt).toLocaleString()}
</span>
</div>
);
})
)}
{loadingMore && (
<div className="p-4 text-center text-gray-400 text-sm">Loading more...</div>
)}
<div ref={loadMoreRef} />
</div>
{notifications.length > 5 && !hasMore && (
<div
className="px-6 py-2 bg-gray-50 dark:bg-gray-700/50 text-center text-primary text-sm cursor-pointer hover:underline"
onClick={() => navigate("/notifications")}
>
View all notifications
</div>
)}
</div>
);
}

Xet Storage Details

Size:
4.22 kB
·
Xet hash:
7bc515c59708d11b4bc5f2fbb476afd35e500655ffe588035e944734746f3218

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.