All files / src/components/features/notifications/NotificationItem index.tsx

37.57% Statements 59/157
100% Branches 0/0
0% Functions 0/2
37.57% Lines 59/157

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 1581x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                           1x 1x 1x 1x 1x 1x 1x                                                                                                                                                           1x 1x  
'use client';
 
import Link from 'next/link';
import { Icon } from '@/components/ui/icons';
import { cn } from '@/lib/core';
import type { NotificationPayload, NotificationType } from '@/lib/notifications/types';
 
export interface NotificationItemProps {
  /** The notification data */
  notification: NotificationPayload;
  /** Callback when notification is clicked/read */
  onMarkAsRead?: () => void;
  /** Callback when notification is clicked */
  onClick?: () => void;
}
 
const notificationIcons: Record<NotificationType, { icon: string; color: string }> = {
  ORDER_UPDATE: {
    icon: 'package',
    color: 'text-blue-500 bg-blue-100 dark:bg-blue-900/30',
  },
  PROMOTION: {
    icon: 'tag',
    color: 'text-purple-500 bg-purple-100 dark:bg-purple-900/30',
  },
  PRICE_DROP: {
    icon: 'trending-down',
    color: 'text-green-500 bg-green-100 dark:bg-green-900/30',
  },
  BACK_IN_STOCK: {
    icon: 'check-circle',
    color: 'text-emerald-500 bg-emerald-100 dark:bg-emerald-900/30',
  },
  REVIEW_RESPONSE: {
    icon: 'chat-bubble',
    color: 'text-orange-500 bg-orange-100 dark:bg-orange-900/30',
  },
  SUPPORT_RESPONSE: {
    icon: 'support',
    color: 'text-indigo-500 bg-indigo-100 dark:bg-indigo-900/30',
  },
  SYSTEM: {
    icon: 'info-circle',
    color: 'text-gray-500 bg-gray-100 dark:bg-gray-700',
  },
};
 
/**
 * Format relative time (e.g., "5m ago", "2h ago", "3d ago")
 */
function formatRelativeTime(dateString: string): string {
  const date = new Date(dateString);
  const now = new Date();
  const diffMs = now.getTime() - date.getTime();
  const diffSeconds = Math.floor(diffMs / 1000);
  const diffMinutes = Math.floor(diffSeconds / 60);
  const diffHours = Math.floor(diffMinutes / 60);
  const diffDays = Math.floor(diffHours / 24);

  if (diffSeconds < 60) {
    return 'Just now';
  } else if (diffMinutes < 60) {
    return `${diffMinutes}m ago`;
  } else if (diffHours < 24) {
    return `${diffHours}h ago`;
  } else if (diffDays < 7) {
    return `${diffDays}d ago`;
  } else {
    return date.toLocaleDateString();
  }
}
 
/**
 * NotificationItem - Individual notification display
 *
 * Displays a single notification with icon, title, message, and timestamp.
 * Supports click to mark as read and optional link navigation.
 */
export function NotificationItem({
  notification,
  onMarkAsRead,
  onClick,
}: NotificationItemProps) {
  const { type, title, message, link, read, createdAt } = notification;
  const iconConfig = notificationIcons[type] || notificationIcons.SYSTEM;

  const handleClick = () => {
    if (!read && onMarkAsRead) {
      onMarkAsRead();
    }
    if (onClick) {
      onClick();
    }
  };

  const content = (
    <div
      className={cn(
        'flex items-start gap-3 p-3 hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors cursor-pointer',
        !read && 'bg-blue-50/50 dark:bg-blue-900/10'
      )}
      onClick={handleClick}
      role="button"
      tabIndex={0}
      onKeyDown={(e) => {
        if (e.key === 'Enter' || e.key === ' ') {
          handleClick();
        }
      }}
    >
      {/* Icon */}
      <div
        className={cn(
          'flex-shrink-0 w-10 h-10 rounded-full flex items-center justify-center',
          iconConfig.color
        )}
      >
        <Icon name={iconConfig.icon as 'package'} className="w-5 h-5" />
      </div>

      {/* Content */}
      <div className="flex-1 min-w-0">
        <div className="flex items-start justify-between gap-2">
          <p
            className={cn(
              'text-sm font-medium text-gray-900 dark:text-white truncate',
              !read && 'font-semibold'
            )}
          >
            {title}
          </p>
          {!read && (
            <span className="flex-shrink-0 w-2 h-2 rounded-full bg-blue-500" />
          )}
        </div>
        <p className="text-sm text-gray-600 dark:text-gray-400 line-clamp-2 mt-0.5">
          {message}
        </p>
        <p className="text-xs text-gray-500 dark:text-gray-500 mt-1">
          {formatRelativeTime(createdAt)}
        </p>
      </div>
    </div>
  );

  if (link) {
    return (
      <Link href={link} className="block">
        {content}
      </Link>
    );
  }

  return content;
}
 
export default NotificationItem;