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

100% Statements 14/14
100% Branches 16/16
100% Functions 3/3
100% Lines 14/14

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                                                                    2x           71x 71x   71x 63x     8x 8x           8x   71x 8x 6x   2x     71x                                                     8x                                                                                                                                                                                                                                          
/**
 * ConflictDetectionAlert Component (Story 5.7 - Task 4b GREEN Phase)
 *
 * Displays conflict detection modal with resolution options
 * AC9: Warn if speaker has conflicting commitment at same time
 * Conflict types: room_overlap, speaker_double_booked, speaker_unavailable
 */
 
import React from 'react';
import {
  Dialog,
  DialogTitle,
  DialogContent,
  DialogActions,
  Button,
  Typography,
  Box,
  Chip,
  Alert,
  Stack,
  Paper,
} from '@mui/material';
import { Warning, Error as ErrorIcon, Info } from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import { useBreakpoints } from '@/hooks/useBreakpoints';
import type { TimingConflictError } from '@/types/event.types';
 
export interface ConflictDetectionAlertProps {
  conflict: TimingConflictError | null;
  isOpen: boolean;
  onClose: () => void;
  onResolve: () => void;
}
 
export const ConflictDetectionAlert: React.FC<ConflictDetectionAlertProps> = ({
  conflict,
  isOpen,
  onClose,
  onResolve,
}) => {
  const { t } = useTranslation('events');
  const { isMobile } = useBreakpoints();
 
  if (!conflict || !isOpen) {
    return null;
  }
 
  const firstConflict = conflict.conflicts[0];
  const conflictTypeLabels = {
    room_overlap: t('slotAssignment.conflicts.types.room_overlap'),
    speaker_double_booked: t('slotAssignment.conflicts.types.speaker_double_booked'),
    speaker_unavailable: t('slotAssignment.conflicts.types.speaker_unavailable'),
  };
 
  const conflictSeverity = firstConflict.type === 'speaker_unavailable' ? 'warning' : 'error';
 
  const getConflictIcon = () => {
    if (conflictSeverity === 'error') {
      return <ErrorIcon color="error" />;
    }
    return <Warning color="warning" />;
  };
 
  return (
    <Dialog
      open={isOpen}
      onClose={onClose}
      maxWidth="md"
      fullWidth
      fullScreen={isMobile}
      role="dialog"
    >
      <DialogTitle>
        <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
          {getConflictIcon()}
          <Typography variant="h6">{t('slotAssignment.conflicts.title')}</Typography>
        </Box>
      </DialogTitle>
 
      <DialogContent>
        <Stack spacing={3}>
          {/* Conflict Message */}
          <Alert severity={conflictSeverity}>
            <Typography variant="body1" sx={{ fontWeight: 500 }}>
              {conflict.message}
            </Typography>
          </Alert>
 
          {/* Conflict Details */}
          {conflict.conflicts.map((conf, index) => (
            <Paper key={index} sx={{ p: 2, bgcolor: 'background.default' }}>
              <Box sx={{ mb: 2 }}>
                <Chip
                  data-testid="conflict-type-badge"
                  label={conflictTypeLabels[conf.type]}
                  color={conflictSeverity === 'error' ? 'error' : 'warning'}
                  size="small"
                  sx={{ mb: 1 }}
                />
                <Chip
                  data-testid="conflict-severity"
                  label={t(`slotAssignment.conflicts.severity.${conflictSeverity}`)}
                  className={`severity-${conflictSeverity}`}
                  size="small"
                  sx={{ ml: 1 }}
                />
              </Box>
 
              <Typography variant="body2" color="text.secondary" gutterBottom>
                {conf.details}
              </Typography>
 
              {/* Visual Timeline */}
              {conf.conflictingTimeRange && (
                <Box
                  data-testid="conflict-timeline"
                  sx={{
                    mt: 2,
                    p: 2,
                    bgcolor: 'background.paper',
                    borderRadius: 1,
                    border: 1,
                    borderColor: conflictSeverity === 'error' ? 'error.light' : 'warning.light',
                  }}
                >
                  <Typography variant="caption" color="text.secondary" gutterBottom>
                    {t('slotAssignment.conflicts.timeRange')}
                  </Typography>
                  <Box sx={{ display: 'flex', gap: 2, mt: 1 }}>
                    <Box>
                      <Typography variant="caption" color="text.secondary">
                        {t('slotAssignment.conflicts.start')}
                      </Typography>
                      <Typography variant="body2" fontWeight="medium">
                        {new Date(conf.conflictingTimeRange.start).toLocaleTimeString('en-US', {
                          hour: '2-digit',
                          minute: '2-digit',
                        })}
                      </Typography>
                    </Box>
                    <Box>
                      <Typography variant="caption" color="text.secondary">
                        {t('slotAssignment.conflicts.end')}
                      </Typography>
                      <Typography variant="body2" fontWeight="medium">
                        {new Date(conf.conflictingTimeRange.end).toLocaleTimeString('en-US', {
                          hour: '2-digit',
                          minute: '2-digit',
                        })}
                      </Typography>
                    </Box>
                  </Box>
                </Box>
              )}
 
              {/* Conflicting Session Info */}
              {conf.conflictingSessionSlug && (
                <Box
                  data-testid="conflicting-slot"
                  sx={{
                    mt: 2,
                    p: 1.5,
                    bgcolor: 'error.lighter',
                    borderRadius: 1,
                    border: 1,
                    borderColor: 'error.light',
                  }}
                  className="conflicting-slot-highlight"
                >
                  <Typography variant="caption" color="text.secondary">
                    {t('slotAssignment.conflicts.conflictingSession')}
                  </Typography>
                  <Typography variant="body2" fontWeight="medium">
                    {conf.conflictingSessionSlug}
                  </Typography>
                </Box>
              )}
            </Paper>
          ))}
 
          {/* Resolution Options */}
          <Box>
            <Typography variant="subtitle2" gutterBottom>
              {t('slotAssignment.conflicts.resolutionOptions')}
            </Typography>
            <Stack spacing={1}>
              <Alert severity="info" icon={<Info />}>
                <Typography variant="body2">
                  {t('slotAssignment.conflicts.resolutionMessage')}
                </Typography>
              </Alert>
            </Stack>
          </Box>
        </Stack>
      </DialogContent>
 
      <DialogActions>
        <Button onClick={onClose} variant="outlined">
          {t('common:actions.cancel')}
        </Button>
        <Button onClick={onResolve} variant="contained" color="primary">
          {t('slotAssignment.conflicts.chooseDifferentSlot')}
        </Button>
      </DialogActions>
    </Dialog>
  );
};