|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { useNotificationStore, type ShipmentNotification } from '../../stores/notification.store'; |
| 5 | +import { cn } from '@/lib/utils'; |
| 6 | +import { ShipmentStatus } from '@/types/shipment.types'; |
| 7 | + |
| 8 | +// Human-readable status labels |
| 9 | +const getStatusLabel = (status: ShipmentStatus): string => { |
| 10 | + const labels: Record<ShipmentStatus, string> = { |
| 11 | + [ShipmentStatus.PENDING]: 'Pending', |
| 12 | + [ShipmentStatus.ACCEPTED]: 'Accepted', |
| 13 | + [ShipmentStatus.IN_TRANSIT]: 'In Transit', |
| 14 | + [ShipmentStatus.DELIVERED]: 'Delivered', |
| 15 | + [ShipmentStatus.COMPLETED]: 'Completed', |
| 16 | + [ShipmentStatus.CANCELLED]: 'Cancelled', |
| 17 | + [ShipmentStatus.DISPUTED]: 'Disputed', |
| 18 | + }; |
| 19 | + return labels[status] || status; |
| 20 | +}; |
| 21 | + |
| 22 | +// Format relative time |
| 23 | +const formatRelativeTime = (dateString: string): string => { |
| 24 | + const date = new Date(dateString); |
| 25 | + const now = new Date(); |
| 26 | + const diffMs = now.getTime() - date.getTime(); |
| 27 | + const diffMins = Math.floor(diffMs / 60000); |
| 28 | + const diffHours = Math.floor(diffMs / 3600000); |
| 29 | + const diffDays = Math.floor(diffMs / 86400000); |
| 30 | + |
| 31 | + if (diffMins < 1) return 'Just now'; |
| 32 | + if (diffMins < 60) return `${diffMins}m ago`; |
| 33 | + if (diffHours < 24) return `${diffHours}h ago`; |
| 34 | + if (diffDays < 7) return `${diffDays}d ago`; |
| 35 | + return date.toLocaleDateString(); |
| 36 | +}; |
| 37 | + |
| 38 | +export function NotificationBell() { |
| 39 | + const [isOpen, setIsOpen] = useState(false); |
| 40 | + const { notifications, unreadCount, markAllAsRead } = useNotificationStore(); |
| 41 | + |
| 42 | + const handleToggle = () => { |
| 43 | + if (!isOpen && unreadCount > 0) { |
| 44 | + markAllAsRead(); |
| 45 | + } |
| 46 | + setIsOpen(!isOpen); |
| 47 | + }; |
| 48 | + |
| 49 | + const displayCount = unreadCount > 9 ? '9+' : unreadCount.toString(); |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className="relative"> |
| 53 | + {/* Bell Icon Button */} |
| 54 | + <button |
| 55 | + onClick={handleToggle} |
| 56 | + className="relative p-2 rounded-md hover:bg-accent transition-colors" |
| 57 | + aria-label="Notifications" |
| 58 | + > |
| 59 | + <svg |
| 60 | + xmlns="http://www.w3.org/2000/svg" |
| 61 | + width="20" |
| 62 | + height="20" |
| 63 | + viewBox="0 0 24 24" |
| 64 | + fill="none" |
| 65 | + stroke="currentColor" |
| 66 | + strokeWidth="2" |
| 67 | + strokeLinecap="round" |
| 68 | + strokeLinejoin="round" |
| 69 | + className="text-foreground" |
| 70 | + > |
| 71 | + <path d="M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9" /> |
| 72 | + <path d="M10.3 21a1.94 1.94 0 0 0 3.4 0" /> |
| 73 | + </svg> |
| 74 | + {unreadCount > 0 && ( |
| 75 | + <span className="absolute -top-1 -right-1 min-w-[18px] h-[18px] flex items-center justify-center bg-red-500 text-white text-xs font-bold rounded-full px-1"> |
| 76 | + {displayCount} |
| 77 | + </span> |
| 78 | + )} |
| 79 | + </button> |
| 80 | + |
| 81 | + {/* Dropdown */} |
| 82 | + {isOpen && ( |
| 83 | + <> |
| 84 | + {/* Backdrop */} |
| 85 | + <div |
| 86 | + className="fixed inset-0 z-40" |
| 87 | + onClick={() => setIsOpen(false)} |
| 88 | + /> |
| 89 | + |
| 90 | + {/* Dropdown Panel */} |
| 91 | + <div className="absolute right-0 top-full mt-2 w-80 bg-background border rounded-lg shadow-lg z-50 overflow-hidden"> |
| 92 | + <div className="p-3 border-b"> |
| 93 | + <h3 className="font-semibold text-sm">Notifications</h3> |
| 94 | + </div> |
| 95 | + |
| 96 | + <div className="max-h-96 overflow-y-auto"> |
| 97 | + {notifications.length === 0 ? ( |
| 98 | + <div className="p-4 text-center text-muted-foreground text-sm"> |
| 99 | + No notifications yet |
| 100 | + </div> |
| 101 | + ) : ( |
| 102 | + <ul className="divide-y"> |
| 103 | + {notifications.slice(0, 20).map((notification) => ( |
| 104 | + <NotificationItem |
| 105 | + key={notification.id} |
| 106 | + notification={notification} |
| 107 | + /> |
| 108 | + ))} |
| 109 | + </ul> |
| 110 | + )} |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + </> |
| 114 | + )} |
| 115 | + </div> |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +function NotificationItem({ notification }: { notification: ShipmentNotification }) { |
| 120 | + const statusColor = { |
| 121 | + [ShipmentStatus.PENDING]: 'bg-yellow-500', |
| 122 | + [ShipmentStatus.ACCEPTED]: 'bg-blue-500', |
| 123 | + [ShipmentStatus.IN_TRANSIT]: 'bg-orange-500', |
| 124 | + [ShipmentStatus.DELIVERED]: 'bg-green-500', |
| 125 | + [ShipmentStatus.COMPLETED]: 'bg-green-700', |
| 126 | + [ShipmentStatus.CANCELLED]: 'bg-red-500', |
| 127 | + [ShipmentStatus.DISPUTED]: 'bg-red-700', |
| 128 | + }[notification.status] || 'bg-gray-500'; |
| 129 | + |
| 130 | + return ( |
| 131 | + <li className={cn('p-3 hover:bg-accent/50 transition-colors', !notification.read && 'bg-primary/5')}> |
| 132 | + <div className="flex items-start gap-3"> |
| 133 | + <div className={cn('w-2 h-2 rounded-full mt-2 flex-shrink-0', statusColor)} /> |
| 134 | + <div className="flex-1 min-w-0"> |
| 135 | + <p className="text-sm font-medium truncate"> |
| 136 | + {getStatusLabel(notification.status)} |
| 137 | + </p> |
| 138 | + <p className="text-xs text-muted-foreground truncate"> |
| 139 | + {notification.origin} → {notification.destination} |
| 140 | + </p> |
| 141 | + <div className="flex items-center justify-between mt-1"> |
| 142 | + <span className="text-xs text-muted-foreground"> |
| 143 | + {notification.trackingNumber} |
| 144 | + </span> |
| 145 | + <span className="text-xs text-muted-foreground"> |
| 146 | + {formatRelativeTime(notification.updatedAt)} |
| 147 | + </span> |
| 148 | + </div> |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + </li> |
| 152 | + ); |
| 153 | +} |
0 commit comments