"use client"; import { useEffect, useState } from 'react'; import { useNotifications } from '@/hooks/use-notifications'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { Notification } from '@/lib/types/notification'; import { Badge } from '@/components/ui/badge'; import { formatDistanceToNow } from 'date-fns'; import { Bell, Check, CheckCheck, ExternalLink, Trash2 } from 'lucide-react'; import Link from 'next/link'; // Source icon mapping const SourceIcons: Record = { leantime:
L
, nextcloud:
N
, gitea:
G
, dolibarr:
D
, moodle:
M
, }; export default function NotificationsPage() { const { notifications, notificationCount, loading, error, fetchNotifications, markAsRead, markAllAsRead } = useNotifications(); const [activeTab, setActiveTab] = useState('all'); // Filter notifications based on active tab const filteredNotifications = activeTab === 'all' ? notifications : notifications.filter(notification => notification.source === activeTab); // Group notifications by source for counts const sourceCounts = Object.entries(notificationCount.sources).map(([source, count]) => ({ source, count: count.total, unread: count.unread })); const handleMarkAsRead = async (notification: Notification) => { if (!notification.isRead) { await markAsRead(notification.id); } }; const handleMarkAllAsRead = async () => { await markAllAsRead(); }; return (

Notifications

Manage your notifications from all connected services

All {notificationCount.unread > 0 && ( {notificationCount.unread} )} {sourceCounts.map(({ source, unread }) => ( {source} {unread > 0 && ( {unread} )} ))}
{loading ? (

Loading notifications...

) : error ? (

{error}

) : filteredNotifications.length === 0 ? (

No notifications

You don't have any {activeTab !== 'all' ? `${activeTab} ` : ''}notifications right now.

) : ( filteredNotifications.map((notification) => (
{SourceIcons[notification.source] || }

{notification.title} {!notification.isRead && ( New )}

From {notification.source} • {formatDistanceToNow(new Date(notification.timestamp), { addSuffix: true })}

{!notification.isRead && ( )} {notification.link && ( )}

{notification.message}

)) )}
); }