tahamajs/IE / Projects /front /src /components /notifications /NotificationList.tsx
tahamajs's picture
download
raw
6.01 kB
// components/notifications/NotificationList.tsx
import { useNavigate } from "react-router-dom";
import { Notification } from "@/types";
import { NotificationItem } from "./NotificationItem";
interface NotificationListProps {
notifications: Notification[];
allNotifications: Notification[];
loading: boolean;
error: string;
typeFilter: string;
onTypeChange: (type: string) => void;
onMarkRead: (id: number) => void;
onDelete: (id: number) => void;
onMarkAllRead: () => void;
onDeleteAll: () => void;
onRetry: () => void;
}
const NOTIFICATION_TYPES = [
{ value: "all", label: "All" },
{ value: "message", label: "Messages" },
{ value: "group", label: "Group" },
{ value: "like", label: "Likes" },
{ value: "comment", label: "Comments" },
{ value: "follow", label: "Follows" },
];
export function NotificationList({
notifications,
allNotifications,
loading,
error,
typeFilter,
onTypeChange,
onMarkRead,
onDelete,
onMarkAllRead,
onDeleteAll,
onRetry,
}: NotificationListProps) {
const navigate = useNavigate();
if (loading) {
return (
<div className="text-center py-16">
<div className="w-10 h-10 border-4 border-primary border-t-transparent rounded-full animate-spin mx-auto mb-4" />
<p className="text-gray-500">Loading notifications…</p>
</div>
);
}
if (error) {
return (
<div className="bg-red-50 text-red-600 p-4 rounded-2xl mb-6 flex items-center gap-3">
⚠️ {error}
<button onClick={onRetry} className="btn btn-secondary btn-sm">Retry</button>
</div>
);
}
if (allNotifications.length === 0) {
return (
<div className="text-center py-20 text-gray-400">
<div className="text-5xl mb-4">🔔</div>
<h3 className="text-lg font-semibold text-gray-500 mb-1">You’re all caught up</h3>
<p className="text-sm">Notifications for messages, likes, comments, follows, and system updates will appear here.</p>
</div>
);
}
// Group by date
const grouped: Record<string, Notification[]> = {};
notifications.forEach(n => {
const date = new Date(n.createdAt).toLocaleDateString();
if (!grouped[date]) grouped[date] = [];
grouped[date].push(n);
});
const handleNotificationClick = async (notif: Notification) => {
onMarkRead(notif.id);
if (notif.type === "message" || notif.type === "group_message") {
navigate("/messages");
} else if (notif.articleId) {
navigate(`/articles/${notif.articleId}`);
} else if (notif.type === "follow" && notif.fromUser) {
navigate(`/users/${notif.fromUser}`);
}
};
return (
<div className="space-y-6">
{/* Filter & Actions */}
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4 pb-6 border-b border-white/10">
<div className="flex flex-wrap gap-2">
{NOTIFICATION_TYPES.map(t => {
const count = t.value === "all"
? allNotifications.length
: allNotifications.filter(n => n.type.startsWith(t.value)).length;
const isActive = typeFilter === t.value;
return (
<button
key={t.value}
onClick={() => onTypeChange(t.value)}
className={`px-4 py-1.5 rounded-full text-xs font-semibold tracking-wide transition-all duration-300 ${
isActive
? "bg-gradient-to-r from-teal-500 to-blue-600 text-white shadow-lg shadow-teal-500/20 scale-105"
: "bg-slate-900/60 hover:bg-slate-800/60 text-slate-400 hover:text-slate-200 border border-white/5"
}`}
>
<span>{t.label}</span>
{count > 0 && (
<span className={`ml-1.5 px-1.5 py-0.5 rounded-md text-[10px] ${
isActive ? "bg-white/20 text-white" : "bg-slate-800 text-slate-400"
}`}>
{count}
</span>
)}
</button>
);
})}
</div>
<div className="flex gap-2">
<button
onClick={onMarkAllRead}
disabled={allNotifications.every(n => n.read)}
className="flex items-center gap-1.5 px-4 py-1.5 text-xs font-semibold rounded-full border border-teal-500/30 bg-teal-500/10 hover:bg-teal-500/20 text-teal-400 transition-all disabled:opacity-40 disabled:pointer-events-none"
>
<span>Mark read</span>
</button>
<button
onClick={onDeleteAll}
className="flex items-center gap-1.5 px-4 py-1.5 text-xs font-semibold rounded-full border border-red-500/30 bg-red-500/10 hover:bg-red-500/20 text-red-400 transition-all"
>
<span>Clear all</span>
</button>
</div>
</div>
{notifications.length === 0 ? (
<div className="text-center py-16 text-slate-500 text-sm">
No notifications match the filter "{NOTIFICATION_TYPES.find(t => t.value === typeFilter)?.label}".
</div>
) : (
<div className="space-y-8">
{Object.entries(grouped).map(([date, items]) => (
<div key={date} className="space-y-4">
<h3 className="text-xs font-bold uppercase tracking-wider text-slate-500 flex items-center gap-2">
<span className="h-1.5 w-1.5 rounded-full bg-teal-500" />
{date}
</h3>
<div className="space-y-3">
{items.map(notif => (
<NotificationItem
key={notif.id}
notification={notif}
onMarkRead={onMarkRead}
onDelete={onDelete}
onClick={() => handleNotificationClick(notif)}
/>
))}
</div>
</div>
))}
</div>
)}
</div>
);
}

Xet Storage Details

Size:
6.01 kB
·
Xet hash:
94be122926595bb51713c90ec1e19ba5c4d0a9ed7c83d302d632436d4783c209

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