import React, { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Bell, Check, Trash2, Filter, CheckCircle, Loader2 } from "lucide-react"; import { supabase } from "@/lib/supabase"; import { useAuth } from "@/lib/auth"; interface Notification { id: string; user_id: string; type: "achievement" | "message" | "event" | "system" | "marketplace"; title: string; description: string; read: boolean; timestamp: Date; action_url?: string; created_at?: Date; } export default function Notifications() { const { user } = useAuth(); const [notifications, setNotifications] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { if (user?.id) fetchNotifications(); }, [user]); const fetchNotifications = async () => { try { const { data, error } = await supabase .from('notifications') .select('*') .eq('user_id', user?.id) .order('created_at', { ascending: false }); if (!error && data) { setNotifications(data.map(n => ({ ...n, timestamp: new Date(n.created_at), actionUrl: n.action_url }))); } } catch (err) { console.error('Error fetching notifications:', err); } finally { setLoading(false); } }; const [filterType, setFilterType] = useState(null); const handleMarkAsRead = async (id: string) => { try { await supabase.from('notifications').update({ read: true }).eq('id', id); setNotifications(n => n.map(notif => notif.id === id ? { ...notif, read: true } : notif ) ); } catch (err) { console.error('Error marking as read:', err); } }; const handleMarkAllAsRead = async () => { try { await supabase.from('notifications').update({ read: true }).eq('user_id', user?.id); setNotifications(n => n.map(notif => ({ ...notif, read: true }))); } catch (err) { console.error('Error marking all as read:', err); } }; const handleDelete = async (id: string) => { try { await supabase.from('notifications').delete().eq('id', id); setNotifications(n => n.filter(notif => notif.id !== id)); } catch (err) { console.error('Error deleting notification:', err); } }; const unreadCount = notifications.filter(n => !n.read).length; const filteredNotifications = filterType ? notifications.filter(n => n.type === filterType) : notifications; const getTypeColor = (type: Notification["type"]) => { const colors = { achievement: "bg-yellow-500/10 border-yellow-500/20", message: "bg-blue-500/10 border-blue-500/20", event: "bg-purple-500/10 border-purple-500/20", marketplace: "bg-green-500/10 border-green-500/20", system: "bg-slate-500/10 border-slate-500/20" }; return colors[type]; }; const getTypeIcon = (type: Notification["type"]) => { const icons = { achievement: "🏆", message: "💬", event: "📅", marketplace: "🛍️", system: "⚙️" }; return icons[type]; }; const formatTime = (date: Date) => { const now = new Date(); const diff = now.getTime() - date.getTime(); 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 date.toLocaleDateString(); }; return (
{/* Header */}

Notifications

{unreadCount > 0 ? `${unreadCount} unread` : "All caught up!"}

{unreadCount > 0 && ( )}
{/* Filter Tabs */} setFilterType(null)} className="data-[state=active]:bg-cyan-500/20 data-[state=active]:text-cyan-400" > All setFilterType("achievement")} className="data-[state=active]:bg-cyan-500/20 data-[state=active]:text-cyan-400" > Achievements setFilterType("message")} className="data-[state=active]:bg-cyan-500/20 data-[state=active]:text-cyan-400" > Messages setFilterType("event")} className="data-[state=active]:bg-cyan-500/20 data-[state=active]:text-cyan-400" > Events setFilterType("marketplace")} className="data-[state=active]:bg-cyan-500/20 data-[state=active]:text-cyan-400" > Marketplace {/* Notifications List */}
{filteredNotifications.length === 0 ? (

No {filterType ? `${filterType}` : ""} notifications

) : ( filteredNotifications.map(notification => (
{getTypeIcon(notification.type)}

{notification.title}

{!notification.read && ( )}

{notification.description}

{formatTime(notification.timestamp)}

{!notification.read && ( )} {(notification.actionUrl || (notification as any).action_url) && ( )}
)) )}
{/* Settings Section */}

Notification Preferences

{[ { label: "Achievement Notifications", enabled: true }, { label: "Message Alerts", enabled: true }, { label: "Event Reminders", enabled: true }, { label: "Marketplace Updates", enabled: false } ].map((pref, idx) => (
))}
); }