All files / src/components/organizer/EventManagement TeamActivityFeed.tsx

54.41% Statements 37/68
59.72% Branches 43/72
42.85% Functions 6/14
58.06% Lines 36/62

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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379                                                                                                                      2x 70x       22x           24x         24x                                     2x         70x 70x   70x       22x         24x           24x                                       2x             27x 27x 27x 27x 27x 27x   27x 27x 75x 27x   27x               27x                   27x                 27x 2x               10x                   25x 1x                         24x                                                                                                   70x         70x 70x   70x                                                                                                                                                                                                                
/**
 * TeamActivityFeed Component (Enhanced with Notifications)
 *
 * Story BAT-7 - Notifications API Consolidation
 * AC: Display EventBridge-triggered notifications for organizers
 *
 * Displays notifications from Event Management Service:
 * - EventBridge-triggered notifications (TASK_ASSIGNED, DEADLINE_WARNING, etc.)
 * - Mark as read functionality
 * - Real-time updates via React Query
 */
 
import React from 'react';
import {
  List,
  ListItem,
  ListItemAvatar,
  ListItemText,
  Avatar,
  Typography,
  Box,
  Button,
  IconButton,
  Stack,
  Skeleton,
  Chip,
  CircularProgress,
  Badge,
  Tooltip,
} from '@mui/material';
import {
  Refresh as RefreshIcon,
  Description as DescriptionIcon,
  CheckCircle as CheckCircleIcon,
  People as PeopleIcon,
  Settings as SettingsIcon,
  Notifications as NotificationsIcon,
  Assignment as AssignmentIcon,
  Warning as WarningIcon,
  Event as EventIcon,
  DoneAll as DoneAllIcon,
  Delete as DeleteIcon,
} from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { formatDistanceToNow } from 'date-fns';
import { de, enUS } from 'date-fns/locale';
import type { Notification, NotificationType } from '@/types/notification';
import { useMarkAsRead, useBatchMarkAsRead, useDeleteNotification } from '@/hooks/useNotifications';
 
interface TeamActivityFeedProps {
  notifications: Notification[];
  totalNotifications?: number;
  isLoading?: boolean;
  onReload?: () => void;
  limit?: number;
}
 
// Notification type to icon mapping
const getNotificationIcon = (type: NotificationType) => {
  switch (type) {
    // Event notifications
    case 'EVENT_PUBLISHED':
    case 'EVENT_STATUS_CHANGED':
      return <EventIcon />;
    // Speaker workflow notifications
    case 'SPEAKER_INVITED':
    case 'SPEAKER_ACCEPTED':
    case 'SPEAKER_DECLINED':
    case 'SPEAKER_CONFIRMED':
      return <PeopleIcon />;
    case 'CONTENT_SUBMITTED':
    case 'QUALITY_REVIEW_PENDING':
    case 'QUALITY_REVIEW_APPROVED':
    case 'QUALITY_REVIEW_REQUIRES_CHANGES':
      return <DescriptionIcon />;
    case 'SLOT_ASSIGNED':
      return <CheckCircleIcon />;
    // Task notifications
    case 'TASK_ASSIGNED':
      return <AssignmentIcon />;
    case 'TASK_DEADLINE_WARNING':
    case 'DEADLINE_WARNING':
      return <WarningIcon />;
    // Other notifications
    case 'OVERFLOW_DETECTED':
    case 'VOTING_REQUIRED':
      return <NotificationsIcon />;
    default:
      return <SettingsIcon />;
  }
};
 
// Notification type to color mapping
const getNotificationColor = (
  type: NotificationType,
  priority: string
): 'primary' | 'success' | 'info' | 'warning' | 'error' | 'default' => {
  // Priority overrides type-based color for urgent notifications
  Iif (priority === 'URGENT') return 'error';
  Iif (priority === 'HIGH') return 'warning';
 
  switch (type) {
    // Event notifications
    case 'EVENT_PUBLISHED':
    case 'EVENT_STATUS_CHANGED':
      return 'success';
    // Speaker workflow
    case 'SPEAKER_INVITED':
    case 'SPEAKER_ACCEPTED':
    case 'SPEAKER_CONFIRMED':
      return 'primary';
    case 'SPEAKER_DECLINED':
      return 'warning';
    // Content/Quality
    case 'CONTENT_SUBMITTED':
    case 'QUALITY_REVIEW_APPROVED':
      return 'success';
    case 'QUALITY_REVIEW_REQUIRES_CHANGES':
      return 'warning';
    case 'QUALITY_REVIEW_PENDING':
      return 'info';
    // Tasks
    case 'TASK_ASSIGNED':
      return 'primary';
    case 'TASK_DEADLINE_WARNING':
    case 'DEADLINE_WARNING':
      return 'warning';
    // Critical
    case 'OVERFLOW_DETECTED':
    case 'VOTING_REQUIRED':
      return 'error';
    default:
      return 'default';
  }
};
 
export const TeamActivityFeed: React.FC<TeamActivityFeedProps> = ({
  notifications,
  totalNotifications,
  isLoading = false,
  onReload,
  limit,
}) => {
  const { t, i18n } = useTranslation('common');
  const locale = i18n.language === 'de' ? de : enUS;
  const navigate = useNavigate();
  const markAsReadMutation = useMarkAsRead();
  const batchMarkAsReadMutation = useBatchMarkAsRead();
  const deleteNotificationMutation = useDeleteNotification();
 
  const displayedNotifications = limit ? notifications.slice(0, limit) : notifications;
  const hasMore = limit && notifications.length > limit;
  const unreadCount = notifications.filter((n) => !n.isRead).length;
  const totalCount = totalNotifications ?? notifications.length;
 
  const handleMarkAsRead = async (notificationId: string) => {
    try {
      await markAsReadMutation.mutateAsync(notificationId);
    } catch (error) {
      console.error('Failed to mark notification as read:', error);
    }
  };
 
  const handleMarkAllAsRead = async () => {
    const unreadIds = notifications.filter((n) => !n.isRead).map((n) => n.id);
    if (unreadIds.length === 0) return;
    try {
      await batchMarkAsReadMutation.mutateAsync(unreadIds);
    } catch (error) {
      console.error('Failed to mark all notifications as read:', error);
    }
  };
 
  const handleDelete = async (notificationId: string) => {
    try {
      await deleteNotificationMutation.mutateAsync(notificationId);
    } catch (error) {
      console.error('Failed to delete notification:', error);
    }
  };
 
  // Loading state
  if (isLoading) {
    return (
      <Box>
        <Stack direction="row" justifyContent="space-between" alignItems="center" mb={2}>
          <Typography variant="h6">{t('notifications.title')}</Typography>
          <CircularProgress size={20} />
        </Stack>
        <List>
          {[1, 2, 3, 4, 5].map((index) => (
            <ListItem key={index} data-testid={`skeleton-notification-${index}`}>
              <Skeleton variant="rectangular" width="100%" height={60} />
            </ListItem>
          ))}
        </List>
      </Box>
    );
  }
 
  // Empty state
  if (notifications.length === 0) {
    return (
      <Box textAlign="center" py={4}>
        <NotificationsIcon sx={{ fontSize: 48, color: 'text.disabled', mb: 2 }} />
        <Typography variant="h6" gutterBottom>
          {t('notifications.title')}
        </Typography>
        <Typography variant="body2" color="text.secondary">
          {t('notifications.noNotifications')}
        </Typography>
      </Box>
    );
  }
 
  return (
    <Box aria-label={t('notifications.notificationFeed')}>
      {/* Header */}
      <Stack direction="row" justifyContent="space-between" alignItems="center" mb={2}>
        <Stack direction="row" spacing={1} alignItems="center">
          <Typography variant="h6">{t('notifications.title')}</Typography>
          <Badge badgeContent={unreadCount} color="error">
            <Chip
              label={totalCount}
              size="small"
              aria-label={t('notifications.totalCountAriaLabel', { count: totalCount })}
            />
          </Badge>
        </Stack>
 
        <Stack direction="row" spacing={0.5} alignItems="center">
          {unreadCount > 0 && (
            <Tooltip title={t('notifications.markAllAsRead')}>
              <IconButton
                onClick={handleMarkAllAsRead}
                disabled={batchMarkAsReadMutation.isPending}
                size="small"
                aria-label={t('notifications.markAllAsRead')}
              >
                <DoneAllIcon fontSize="small" />
              </IconButton>
            </Tooltip>
          )}
          <Tooltip title={t('notifications.reload')}>
            <IconButton
              onClick={onReload}
              disabled={isLoading}
              size="small"
              aria-label={t('notifications.reload')}
            >
              <RefreshIcon />
            </IconButton>
          </Tooltip>
        </Stack>
      </Stack>
 
      {/* Last updated timestamp */}
      <Typography variant="caption" color="text.secondary" display="block" mb={1}>
        {t('notifications.lastUpdated')}:{' '}
        {formatDistanceToNow(new Date(), { locale, addSuffix: true })}
      </Typography>
 
      {/* Notification list */}
      <List data-testid="team-activity-feed">
        {displayedNotifications.map((notification) => {
          const relativeTime = formatDistanceToNow(new Date(notification.createdAt), {
            locale,
            addSuffix: true,
          });
 
          const color = getNotificationColor(notification.notificationType, notification.priority);
          const isUnread = !notification.isRead;
 
          return (
            <ListItem
              key={notification.id}
              data-testid={`notification-item-${notification.id}`}
              sx={{
                py: 1.5,
                bgcolor: isUnread ? 'action.hover' : 'transparent',
                borderLeft: isUnread ? `4px solid` : 'none',
                borderColor: `${color}.main`,
              }}
              secondaryAction={
                <Stack direction="row" spacing={0}>
                  {isUnread && (
                    <Tooltip title={t('notifications.markAsRead')}>
                      <IconButton
                        size="small"
                        onClick={() => handleMarkAsRead(notification.id)}
                        disabled={markAsReadMutation.isPending}
                      >
                        <DoneAllIcon fontSize="small" />
                      </IconButton>
                    </Tooltip>
                  )}
                  <Tooltip title={t('notifications.delete')}>
                    <IconButton
                      size="small"
                      onClick={() => handleDelete(notification.id)}
                      disabled={deleteNotificationMutation.isPending}
                    >
                      <DeleteIcon fontSize="small" />
                    </IconButton>
                  </Tooltip>
                </Stack>
              }
            >
              <ListItemAvatar>
                <Avatar
                  sx={{
                    bgcolor: `${color}.light`,
                    color: `${color}.dark`,
                  }}
                >
                  {getNotificationIcon(notification.notificationType)}
                </Avatar>
              </ListItemAvatar>
 
              <ListItemText
                primaryTypographyProps={{ component: 'div' }}
                secondaryTypographyProps={{ component: 'div' }}
                primary={
                  <Stack
                    direction="row"
                    spacing={1}
                    alignItems="center"
                    useFlexGap
                    sx={{ flexWrap: 'wrap', rowGap: 0.5 }}
                  >
                    <Typography variant="body2" fontWeight={isUnread ? 'bold' : 'normal'}>
                      {notification.subject}
                    </Typography>
                    {notification.eventCode && (
                      <Chip label={notification.eventCode} size="small" variant="outlined" />
                    )}
                    {notification.priority === 'URGENT' && (
                      <Chip label={t('notifications.priority.urgent')} size="small" color="error" />
                    )}
                    {notification.priority === 'HIGH' && (
                      <Chip label={t('notifications.priority.high')} size="small" color="warning" />
                    )}
                  </Stack>
                }
                secondary={
                  <Stack spacing={0.5} mt={0.5}>
                    <Typography variant="caption" color="text.secondary">
                      {notification.body.length > 80
                        ? `${notification.body.substring(0, 80)}...`
                        : notification.body}
                    </Typography>
                    <Typography variant="caption" color="text.secondary">
                      {relativeTime}
                    </Typography>
                  </Stack>
                }
              />
            </ListItem>
          );
        })}
      </List>
 
      {/* View All button */}
      {hasMore && (
        <Box textAlign="center" mt={2}>
          <Button
            variant="outlined"
            size="small"
            onClick={() => navigate('/organizer/notifications')}
          >
            {t('notifications.viewAll')} ({totalCount})
          </Button>
        </Box>
      )}
    </Box>
  );
};