Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | 'use client';
import { useEffect, useRef, useCallback } from 'react';
import Link from 'next/link';
import { Icon } from '@/components/ui/icons';
import { cn } from '@/lib/core';
import { NotificationItem } from '../NotificationItem';
import type { NotificationPayload } from '@/lib/notifications/types';
export interface NotificationPanelProps {
/** Whether the panel is visible */
isOpen: boolean;
/** Callback to close the panel */
onClose: () => void;
/** List of notifications to display */
notifications: NotificationPayload[];
/** Number of unread notifications */
unreadCount: number;
/** Whether notifications are loading */
isLoading?: boolean;
/** Callback when a notification is marked as read */
onMarkAsRead?: (id: string) => void;
/** Callback to mark all as read */
onMarkAllAsRead?: () => void;
/** Callback when a notification is clicked */
onNotificationClick?: (notification: NotificationPayload) => void;
/** Optional class name */
className?: string;
}
/**
* NotificationPanel - Dropdown panel displaying user notifications
*
* Shows a list of notifications with actions to mark as read,
* mark all as read, and navigate to full notifications page.
*/
export function NotificationPanel({
isOpen,
onClose,
notifications,
unreadCount,
isLoading = false,
onMarkAsRead,
onMarkAllAsRead,
onNotificationClick,
className,
}: NotificationPanelProps) {
const panelRef = useRef<HTMLDivElement>(null);
// Close on click outside
useEffect(() => {
if (!isOpen) return;
function handleClickOutside(event: MouseEvent) {
if (panelRef.current && !panelRef.current.contains(event.target as Node)) {
onClose();
}
}
// Delay adding listener to prevent immediate close
const timeoutId = setTimeout(() => {
document.addEventListener('mousedown', handleClickOutside);
}, 0);
return () => {
clearTimeout(timeoutId);
document.removeEventListener('mousedown', handleClickOutside);
};
}, [isOpen, onClose]);
// Close on Escape key
useEffect(() => {
if (!isOpen) return;
function handleEscape(event: KeyboardEvent) {
if (event.key === 'Escape') {
onClose();
}
}
document.addEventListener('keydown', handleEscape);
return () => document.removeEventListener('keydown', handleEscape);
}, [isOpen, onClose]);
const handleMarkAsRead = useCallback(
(id: string) => {
onMarkAsRead?.(id);
},
[onMarkAsRead]
);
const handleNotificationClick = useCallback(
(notification: NotificationPayload) => {
onNotificationClick?.(notification);
},
[onNotificationClick]
);
if (!isOpen) return null;
return (
<div
ref={panelRef}
className={cn(
'absolute right-0 top-full mt-2 w-80 sm:w-96 bg-white dark:bg-gray-900 rounded-lg shadow-lg border border-gray-200 dark:border-gray-700 z-50 overflow-hidden',
className
)}
role="dialog"
aria-label="Notifications"
>
{/* Header */}
<div className="flex items-center justify-between px-4 py-3 border-b border-gray-200 dark:border-gray-700">
<div className="flex items-center gap-2">
<h2 className="font-semibold text-gray-900 dark:text-white">
Notifications
</h2>
{unreadCount > 0 && (
<span className="px-2 py-0.5 text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-900/50 dark:text-blue-300 rounded-full">
{unreadCount}
</span>
)}
</div>
<div className="flex items-center gap-2">
{unreadCount > 0 && (
<button
type="button"
onClick={onMarkAllAsRead}
className="text-xs text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300 font-medium"
>
Mark all read
</button>
)}
<button
type="button"
onClick={onClose}
className="p-1 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 rounded"
aria-label="Close notifications"
>
<Icon name="x" className="w-4 h-4" />
</button>
</div>
</div>
{/* Content */}
<div className="max-h-96 overflow-y-auto">
{isLoading ? (
<div className="flex items-center justify-center py-8">
<Icon name="spinner" className="w-6 h-6 animate-spin text-gray-400" />
</div>
) : notifications.length === 0 ? (
<div className="flex flex-col items-center justify-center py-8 px-4 text-center">
<Icon
name="bell"
className="w-12 h-12 text-gray-300 dark:text-gray-600 mb-3"
/>
<p className="text-sm text-gray-500 dark:text-gray-400">
No notifications yet
</p>
<p className="text-xs text-gray-400 dark:text-gray-500 mt-1">
We'll notify you when something important happens
</p>
</div>
) : (
<div className="divide-y divide-gray-100 dark:divide-gray-800">
{notifications.map((notification) => (
<NotificationItem
key={notification.id}
notification={notification}
onMarkAsRead={() => handleMarkAsRead(notification.id)}
onClick={() => handleNotificationClick(notification)}
/>
))}
</div>
)}
</div>
{/* Footer */}
{notifications.length > 0 && (
<div className="border-t border-gray-200 dark:border-gray-700 p-2">
<Link
href="/my-account"
className="block w-full text-center py-2 text-sm text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300 font-medium hover:bg-gray-50 dark:hover:bg-gray-800 rounded transition-colors"
onClick={onClose}
>
View all notifications
</Link>
</div>
)}
</div>
);
}
export default NotificationPanel;
|