import { useState, useEffect, useRef } from "react" import { motion, AnimatePresence } from "framer-motion" import { toast } from "react-toastify" import { useNavigate } from "react-router-dom" import { Bell, X, Check, AlertCircle, Info, CheckCircle, AlertTriangle, Package, ShoppingCart, Heart, Star, Truck, Gift, Crown, Zap, Trash2, Settings, Eye, EyeOff, } from "lucide-react" import { subscribeToNotifications, getNotifications, markNotificationAsRead, markAllNotificationsAsRead as markAllReadService, deleteNotification as deleteNotificationService, clearAllNotifications as clearAllService, getUnreadCount as getUnreadCountService } from "../utils/notificationService" import "./NotificationCenter.css" const NotificationCenter = () => { const navigate = useNavigate() const [isOpen, setIsOpen] = useState(false) const [notifications, setNotifications] = useState([]) const [unreadCount, setUnreadCount] = useState(0) const [filter, setFilter] = useState("all") const [showSettings, setShowSettings] = useState(false) const notificationRef = useRef(null) const timeoutRef = useRef(null) const notificationTypes = { success: { icon: CheckCircle, color: "#4ecdc4", bgColor: "rgba(78, 205, 196, 0.1)" }, warning: { icon: AlertTriangle, color: "#f39c12", bgColor: "rgba(243, 156, 18, 0.1)" }, error: { icon: AlertCircle, color: "#e74c3c", bgColor: "rgba(231, 76, 60, 0.1)" }, info: { icon: Info, color: "#667eea", bgColor: "rgba(102, 126, 234, 0.1)" }, order: { icon: Package, color: "#9b59b6", bgColor: "rgba(155, 89, 182, 0.1)" }, cart: { icon: ShoppingCart, color: "#2ecc71", bgColor: "rgba(46, 204, 113, 0.1)" }, wishlist: { icon: Heart, color: "#e91e63", bgColor: "rgba(233, 30, 99, 0.1)" }, review: { icon: Star, color: "#ff9800", bgColor: "rgba(255, 152, 0, 0.1)" }, shipping: { icon: Truck, color: "#00bcd4", bgColor: "rgba(0, 188, 212, 0.1)" }, promotion: { icon: Gift, color: "#ff5722", bgColor: "rgba(255, 87, 34, 0.1)" }, } useEffect(() => { const unsubscribe = subscribeToNotifications((updatedNotifications) => { setNotifications(updatedNotifications) setUnreadCount(getUnreadCountService()) }) setNotifications(getNotifications()) setUnreadCount(getUnreadCountService()) return unsubscribe }, []) useEffect(() => { const handleClickOutside = (event) => { if (notificationRef.current && !notificationRef.current.contains(event.target)) { setIsOpen(false) setShowSettings(false) } } document.addEventListener("mousedown", handleClickOutside) return () => document.removeEventListener("mousedown", handleClickOutside) }, []) const generateRandomNotification = () => { const types = Object.keys(notificationTypes) const randomType = types[Math.floor(Math.random() * types.length)] const messages = { success: ["Payment processed successfully!", "Account verified!", "Settings updated!"], warning: ["Low stock alert for wishlist item", "Payment method expires soon", "Profile incomplete"], error: ["Payment failed", "Connection timeout", "Invalid coupon code"], info: ["New features available", "System maintenance scheduled", "Privacy policy updated"], order: ["New order received", "Order status updated", "Order ready for pickup"], cart: ["Item added to cart", "Cart saved for later", "Price drop in cart"], wishlist: ["Item back in stock", "Price drop alert", "Similar item suggestion"], review: ["Review published", "Review helpful vote", "Review response received"], shipping: ["Package out for delivery", "Delivery attempted", "Package delivered"], promotion: ["Flash sale started", "Exclusive member offer", "Cashback available"], } const titles = { success: ["Success!", "Completed", "Done"], warning: ["Attention", "Notice", "Warning"], error: ["Error", "Failed", "Problem"], info: ["Information", "Update", "News"], order: ["Order Update", "New Order", "Order Status"], cart: ["Cart Update", "Shopping Cart", "Cart Alert"], wishlist: ["Wishlist Alert", "Wishlist Update", "Saved Item"], review: ["Review Update", "Customer Review", "Feedback"], shipping: ["Shipping Update", "Delivery Status", "Package Info"], promotion: ["Special Offer", "Promotion", "Deal Alert"], } return { id: Date.now(), type: randomType, title: titles[randomType][Math.floor(Math.random() * titles[randomType].length)], message: messages[randomType][Math.floor(Math.random() * messages[randomType].length)], timestamp: new Date(), read: false, priority: ["low", "medium", "high"][Math.floor(Math.random() * 3)], } } const formatTimestamp = (timestamp) => { const now = new Date() const diff = now - timestamp const minutes = Math.floor(diff / 60000) const hours = Math.floor(diff / 3600000) const days = Math.floor(diff / 86400000) if (minutes < 1) return "Just now" if (minutes < 60) return `${minutes}m ago` if (hours < 24) return `${hours}h ago` if (days < 7) return `${days}d ago` return timestamp.toLocaleDateString() } const markAsRead = (id) => { markNotificationAsRead(id) setUnreadCount(getUnreadCountService()) } const markAllAsRead = () => { markAllReadService() setUnreadCount(0) } const deleteNotification = (id) => { deleteNotificationService(id) setUnreadCount(getUnreadCountService()) toast.success("Notification removed!", { position: "top-right", autoClose: 2000, }) } const clearAllNotifications = () => { clearAllService() setUnreadCount(0) toast.success("All notifications cleared!", { position: "top-right", autoClose: 2000, }) } const filteredNotifications = notifications.filter((notification) => { if (filter === "all") return true if (filter === "unread") return !notification.read return notification.type === filter }) const toggleNotificationPanel = () => { setIsOpen(!isOpen) } const handleNotificationAction = (notification) => { markAsRead(notification.id) switch (notification.type) { case 'order': navigate('/orders') toast.success('Navigating to Orders page') break case 'cart': navigate('/cart') toast.success('Navigating to Cart page') break case 'wishlist': navigate('/wishlist') toast.success('Navigating to Wishlist page') break case 'review': navigate('/productlist') toast.success('Navigating to Products page') break case 'shipping': navigate('/orders') toast.success('Navigating to Orders page') break case 'promotion': navigate('/productlist') toast.success('Navigating to Products page') break case 'success': navigate('/profile') toast.success('Navigating to Profile page') break case 'warning': navigate('/profile') toast.success('Navigating to Profile page') break case 'error': navigate('/cart') toast.success('Navigating to Cart page') break case 'info': navigate('/productlist') toast.success('Navigating to Products page') break default: navigate('/productlist') toast.success('Navigating to Products page') break } setIsOpen(false) } return (
You're all caught up!
{notification.message}
{}