import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { DropdownMenuItem } from '@/components/ui/dropdown-menu'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Separator } from '@/components/ui/separator'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from '@/components/ui/tooltip'; import { CoinTransaction, HabitsData, User, UserData, WishlistData } from '@/lib/types'; import { t2d } from '@/lib/utils'; import { Info } from 'lucide-react'; import { useTranslations } from 'next-intl'; import Link from 'next/link'; interface NotificationDropdownProps { currentUser: User | null; unreadNotifications: CoinTransaction[]; displayedReadNotifications: CoinTransaction[]; habitsData: HabitsData; wishlistData: WishlistData; usersData: UserData; } // Helper function to get the name of the related item const getRelatedItemName = (tx: CoinTransaction, habitsData: HabitsData, wishlistData: WishlistData): string | undefined => { if (!tx.relatedItemId) return undefined; if (tx.type === 'HABIT_COMPLETION' || tx.type === 'TASK_COMPLETION') { return habitsData.habits.find(h => h.id === tx.relatedItemId)?.name; } if (tx.type === 'WISH_REDEMPTION') { return wishlistData.items.find(w => w.id === tx.relatedItemId)?.name; } return undefined; }; export default function NotificationDropdown({ currentUser, unreadNotifications, // Use props directly displayedReadNotifications, // Use props directly habitsData, wishlistData, usersData, }: NotificationDropdownProps) { const t = useTranslations('NotificationDropdown'); // Helper function to generate notification message, now using t const getNotificationMessage = (tx: CoinTransaction, triggeringUser?: User, relatedItemName?: string): string => { const username = triggeringUser?.username || t('defaultUsername'); const itemName = relatedItemName || t('defaultItemName'); switch (tx.type) { case 'HABIT_COMPLETION': case 'TASK_COMPLETION': return t('userCompletedItem', { username, itemName }); case 'WISH_REDEMPTION': return t('userRedeemedItem', { username, itemName }); default: return t('activityRelatedToItem', { username, itemName }); } }; if (!currentUser) { return
{t('notLoggedIn')}
; } const renderNotification = (tx: CoinTransaction, isUnread: boolean) => { const triggeringUser = usersData.users.find(u => u.id === tx.userId); const relatedItemName = getRelatedItemName(tx, habitsData, wishlistData); const message = getNotificationMessage(tx, triggeringUser, relatedItemName); // Uses the new t-aware helper const txTimestamp = t2d({ timestamp: tx.timestamp, timezone: 'UTC' }); const timeAgo = txTimestamp.toRelative(); const linkHref = `/coins?highlight=${tx.id}${tx.userId ? `&user=${tx.userId}` : ''}`; return ( // Wrap the Link with DropdownMenuItem and use asChild to pass props
{triggeringUser?.username?.charAt(0).toUpperCase() || '?'}

{message}

{timeAgo}

); }; return ( {/* Removed the outer div as width is now set on DropdownMenuContent in NotificationBell */} <>

{t('notificationsTitle')}

{t('notificationsTooltip')}

{unreadNotifications.length === 0 && displayedReadNotifications.length === 0 && (
{t('noNotificationsYet')}
)} {unreadNotifications.length > 0 && ( <> {unreadNotifications.map(tx => renderNotification(tx, true))} {displayedReadNotifications.length > 0 && } )} {displayedReadNotifications.length > 0 && ( <> {displayedReadNotifications.map(tx => renderNotification(tx, false))} )}
{/* Close the fragment */}
); }