All files / src/components/SlotAssignment/UnassignedSpeakersList UnassignedSpeakersList.tsx

88.88% Statements 24/27
84.9% Branches 45/53
80% Functions 8/10
88.88% Lines 24/27

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                                                                                                      2x                       77x     77x 49x 1x       77x 77x 77x 77x     77x 1x                     76x                                                                                     1x                               1x                                                               160x 160x 160x       160x   160x   160x                     1x                                                           2x                                                                                                       1x 1x                                      
/**
 * UnassignedSpeakersList Component (Story 5.7 - Task 4b GREEN Phase)
 *
 * Displays unassigned speakers list with filter controls
 * AC12: Show unassigned speakers list with real-time updates
 * AC5: Draggable speaker cards with grab handle
 * AC7: View preferences button per speaker
 */
 
import React, { useEffect, useRef } from 'react';
import {
  Box,
  Typography,
  Card,
  CardContent,
  Chip,
  LinearProgress,
  Button,
  ButtonGroup,
  IconButton,
  Tooltip,
  Stack,
  Skeleton,
} from '@mui/material';
import { DragIndicator, Visibility } from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import { useBreakpoints } from '@/hooks/useBreakpoints';
import type { Session } from '@/types/event.types';
 
export interface UnassignedSpeakersListProps {
  sessions: Session[];
  totalSessions: number;
  onViewPreferences: (username: string) => void;
  onDragStart?: (session: Session) => (e: React.DragEvent) => void;
  activeFilter?: 'all' | 'assigned' | 'unassigned';
  onFilterChange?: (filter: 'all' | 'assigned' | 'unassigned') => void;
  isLoading?: boolean;
  /**
   * 14.C.5: when set, the matching session card is visually highlighted and
   * scrolled into view (best-effort — no-op if no session matches).
   */
  focusSessionSlug?: string | null;
  /**
   * 14.G.3 (mobile tap-to-assign): when provided, tapping a card selects it
   * (drag-drop is hostile on touch). The selected card is highlighted via
   * `selectedSessionSlug`.
   */
  onSessionTap?: (session: Session) => void;
  selectedSessionSlug?: string | null;
}
 
export const UnassignedSpeakersList: React.FC<UnassignedSpeakersListProps> = ({
  sessions,
  totalSessions,
  onViewPreferences,
  onDragStart,
  activeFilter = 'unassigned',
  onFilterChange,
  isLoading = false,
  focusSessionSlug = null,
  onSessionTap,
  selectedSessionSlug = null,
}) => {
  const focusedCardRef = useRef<HTMLDivElement | null>(null);
 
  // Best-effort scroll the focused card into view when it (or the target) changes.
  useEffect(() => {
    if (focusSessionSlug && focusedCardRef.current) {
      focusedCardRef.current.scrollIntoView({ block: 'nearest' });
    }
  }, [focusSessionSlug, sessions]);
 
  const { t } = useTranslation('events');
  const { isMobile } = useBreakpoints();
  const assignedCount = totalSessions - sessions.length;
  const progressPercent = totalSessions > 0 ? Math.round((assignedCount / totalSessions) * 100) : 0;
 
  // Loading skeleton
  if (isLoading) {
    return (
      <Box data-testid="speaker-pool-sidebar" sx={{ height: '100%', p: 2 }}>
        <Skeleton data-testid="skeleton-loader" variant="rectangular" height={60} sx={{ mb: 2 }} />
        <Skeleton variant="rectangular" height={40} sx={{ mb: 2 }} />
        <Skeleton data-testid="skeleton-card" variant="rectangular" height={100} sx={{ mb: 1 }} />
        <Skeleton data-testid="skeleton-card" variant="rectangular" height={100} sx={{ mb: 1 }} />
        <Skeleton data-testid="skeleton-card" variant="rectangular" height={100} />
      </Box>
    );
  }
 
  return (
    <Box
      data-testid="speaker-pool-sidebar"
      sx={{ height: '100%', display: 'flex', flexDirection: 'column' }}
    >
      {/* Header with Progress */}
      <Box sx={{ p: 2, borderBottom: 1, borderColor: 'divider' }}>
        <Typography variant="h6" gutterBottom>
          {t('slotAssignment.speakerPool.title')}
        </Typography>
 
        {/* Progress Indicator */}
        <Box sx={{ mb: 2 }}>
          <Typography variant="body2" color="text.secondary" sx={{ mb: 1 }}>
            {t('slotAssignment.speakerPool.assigned', {
              count: assignedCount,
              total: totalSessions,
              percent: progressPercent,
            })}
          </Typography>
          <LinearProgress
            variant="determinate"
            value={progressPercent}
            role="progressbar"
            sx={{ height: 8, borderRadius: 1 }}
          />
        </Box>
 
        {/* Unassigned Badge */}
        {sessions.length > 0 && (
          <Chip
            data-testid="unassigned-badge"
            label={t('slotAssignment.speakerPool.remaining', { count: sessions.length })}
            color="warning"
            size="small"
            sx={{ mb: 2 }}
          />
        )}
 
        {/* Filter Controls */}
        <ButtonGroup size="small" fullWidth data-testid="filter-buttons">
          <Button
            data-testid="filter-all"
            onClick={() => onFilterChange?.('all')}
            variant={activeFilter === 'all' ? 'contained' : 'outlined'}
            className={activeFilter === 'all' ? 'filter-active' : ''}
          >
            {t('slotAssignment.speakerPool.filters.all')}
          </Button>
          <Button
            data-testid="filter-assigned"
            onClick={() => onFilterChange?.('assigned')}
            variant={activeFilter === 'assigned' ? 'contained' : 'outlined'}
            className={activeFilter === 'assigned' ? 'filter-active' : ''}
          >
            {t('slotAssignment.speakerPool.filters.assigned')}
          </Button>
          <Button
            data-testid="filter-unassigned"
            onClick={() => onFilterChange?.('unassigned')}
            variant={activeFilter === 'unassigned' ? 'contained' : 'outlined'}
            className={activeFilter === 'unassigned' ? 'filter-active' : ''}
          >
            {t('slotAssignment.speakerPool.filters.unassigned')}
          </Button>
        </ButtonGroup>
      </Box>
 
      {/* Speaker Cards */}
      <Box sx={{ flex: 1, overflowY: 'auto', p: 2 }}>
        {sessions.length === 0 ? (
          // Empty State
          <Box
            data-testid="empty-state"
            sx={{
              textAlign: 'center',
              py: 4,
              px: 2,
              color: 'text.secondary',
            }}
          >
            <Typography variant="h6" color="success.main" gutterBottom>
              {t('slotAssignment.speakerPool.emptyState.title')}
            </Typography>
            <Typography variant="body2">
              {t('slotAssignment.speakerPool.emptyState.message')}
            </Typography>
          </Box>
        ) : (
          <Stack spacing={2}>
            {sessions.map((session) => {
              const speaker = session.speakers?.[0];
              const username = speaker?.username || '';
              const displayName = speaker
                ? `${speaker.firstName} ${speaker.lastName}`
                : 'Unknown Speaker';
 
              const isFocused = !!focusSessionSlug && session.sessionSlug === focusSessionSlug;
              const isSelected =
                !!selectedSessionSlug && session.sessionSlug === selectedSessionSlug;
 
              return (
                <Card
                  key={session.sessionSlug}
                  ref={isFocused ? focusedCardRef : undefined}
                  data-testid={
                    isFocused
                      ? `focused-session-${session.sessionSlug}`
                      : `tray-session-${session.sessionSlug}`
                  }
                  draggable
                  onDragStart={onDragStart?.(session)}
                  onClick={onSessionTap ? () => onSessionTap(session) : undefined}
                  aria-pressed={onSessionTap ? isSelected : undefined}
                  role="article"
                  aria-label={`${t('common:role.speaker')}: ${displayName}`}
                  tabIndex={0}
                  sx={{
                    cursor: onSessionTap ? 'pointer' : 'grab',
                    ...(isSelected && {
                      borderLeft: 4,
                      borderColor: 'secondary.main',
                      boxShadow: 6,
                      bgcolor: 'action.selected',
                    }),
                    ...(isFocused &&
                      !isSelected && {
                        borderLeft: 4,
                        borderColor: 'primary.main',
                        boxShadow: 4,
                      }),
                    '&:hover': {
                      boxShadow: 3,
                      '& .drag-handle': {
                        opacity: 1,
                      },
                    },
                    '&:active': {
                      cursor: 'grabbing',
                    },
                  }}
                  onMouseEnter={(e) => {
                    (e.currentTarget as HTMLElement).style.cursor = 'grab';
                  }}
                >
                  <CardContent
                    sx={{
                      display: 'flex',
                      alignItems: 'start',
                      gap: 1,
                      p: 2,
                      '&:last-child': { pb: 2 },
                    }}
                  >
                    {/* Drag Handle */}
                    <DragIndicator
                      className="drag-handle"
                      data-testid="drag-handle"
                      sx={{ color: 'text.secondary', opacity: 0.3, transition: 'opacity 0.2s' }}
                    />
 
                    {/* Speaker Info */}
                    <Box sx={{ flex: 1, minWidth: 0 }}>
                      <Typography variant="subtitle2" fontWeight="bold" noWrap>
                        {session.title}
                      </Typography>
                      <Typography variant="caption" display="block" color="text.secondary" noWrap>
                        {displayName}
                      </Typography>
                    </Box>
 
                    {/* View Preferences — icon-only on mobile so the title + speaker stay
                        legible (mirrors the assigned-slot card); labelled button on desktop. */}
                    {isMobile ? (
                      <Tooltip title={t('slotAssignment.speakerPool.viewPreferences')}>
                        <IconButton
                          size="small"
                          edge="end"
                          onClick={(e) => {
                            e.stopPropagation();
                            onViewPreferences(username);
                          }}
                          data-testid={`view-preferences-${username}`}
                          aria-label={t('slotAssignment.speakerPool.viewPreferences')}
                          sx={{ flexShrink: 0, color: 'text.secondary' }}
                        >
                          <Visibility fontSize="small" />
                        </IconButton>
                      </Tooltip>
                    ) : (
                      <Button
                        size="small"
                        startIcon={<Visibility />}
                        onClick={(e) => {
                          e.stopPropagation();
                          onViewPreferences(username);
                        }}
                        data-testid={`view-preferences-${username}`}
                        aria-label={t('slotAssignment.speakerPool.viewPreferences')}
                        sx={{ flexShrink: 0 }}
                      >
                        {t('slotAssignment.speakerPool.viewPreferences')}
                      </Button>
                    )}
                  </CardContent>
                </Card>
              );
            })}
          </Stack>
        )}
      </Box>
    </Box>
  );
};