// components/admin-notifications.tsx 'use client'; import React from 'react'; import { Heart, MessageCircle, BookOpen, List } from 'lucide-react'; import { Card, CardContent, CardHeader, CardTitle, } from '@/components/ui/card'; import Image from 'next/image'; import { formatDistanceToNow } from 'date-fns'; interface Notification { id: string; type: 'like' | 'comment' | 'course_created' | 'playlist_created'; user?: { id: string; name?: string; image?: string; email: string; } | null; video?: { id: string; title: string; }; course?: { id: string; title: string; code: string; }; playlist?: { id: string; title: string; courseTitle: string; }; content: string | null; createdAt: Date; } export function AdminNotifications() { const [notifications, setNotifications] = React.useState([]); const [isLoading, setIsLoading] = React.useState(true); const [error, setError] = React.useState(null); React.useEffect(() => { const fetchNotifications = async () => { try { setIsLoading(true); const res = await fetch('/api/admin/notifications'); if (res.ok) { const data = await res.json(); setNotifications( data.notifications.map((n: any) => ({ ...n, createdAt: new Date(n.createdAt), })) ); } else { setError('Failed to fetch notifications'); } } catch (err) { console.error('Failed to fetch notifications:', err); setError('Error loading notifications'); } finally { setIsLoading(false); } }; fetchNotifications(); // Refresh notifications every 30 seconds const interval = setInterval(fetchNotifications, 30000); return () => clearInterval(interval); }, []); if (isLoading) { return ( Activity

Loading notifications...

); } if (error) { return ( Activity

{error}

); } if (notifications.length === 0) { return ( Activity

No activity yet

); } return ( Activity ({notifications.length}) {notifications.map((notification) => (
{notification.user?.image && (
{notification.user.name
)} {notification.type === 'course_created' && (
)} {notification.type === 'playlist_created' && (
)}
{notification.type === 'like' && ( )} {notification.type === 'comment' && ( )} {notification.type === 'course_created' && ( )} {notification.type === 'playlist_created' && ( )}

{notification.user ? ( notification.user.name || notification.user.email ) : ( 'System' )}

{formatDistanceToNow(notification.createdAt, { addSuffix: true, })}

{notification.type === 'like' && `liked ${notification.video?.title}`} {notification.type === 'comment' && `commented on ${notification.video?.title}`} {notification.type === 'course_created' && `created course: ${notification.course?.title}`} {notification.type === 'playlist_created' && `created playlist: ${notification.playlist?.title}`}

{notification.content && (

"{notification.content}"

)}
))}
); }