All files / src/components/TopicBacklogManager TopicDetailsPanel.tsx

82.14% Statements 46/56
76.27% Branches 45/59
66.66% Functions 16/24
81.81% Lines 45/55

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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446                                                                                                1x 20x             20x     1x             20x 20x 20x 20x 20x 20x     20x     20x     20x     20x 1x     1x     1x     1x         20x         20x 20x 17x             3x                 3x                   20x       20x 1x 1x     20x 1x       1x       20x 1x 1x                     1x 1x           1x     20x                                                                                                                                                               1x                       2x                                                       1x                                                           1x 1x   1x                                                                                                                                                                                                               1x                                
/**
 * Topic Details Panel Component (Story 5.2 - AC5, AC6, AC7)
 *
 * Displays topic details with:
 * - Staleness score and color coding
 * - Similar topics warning (>70% similarity)
 * - Performance metrics
 * - Topic selection action
 * - Override staleness capability
 */
 
import React, { useState } from 'react';
import {
  Paper,
  Typography,
  Box,
  Chip,
  Button,
  Alert,
  Dialog,
  DialogTitle,
  DialogContent,
  DialogContentText,
  DialogActions,
  TextField,
  List,
  ListItem,
  ListItemText,
} from '@mui/material';
import { Warning as WarningIcon, CheckCircle as CheckIcon } from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useSimilarTopics, useSelectTopicForEvent, useTopicUsageHistory } from '@/hooks/useTopics';
import { TopicHeatMap } from '@/components/TopicHeatMap';
import { topicService } from '@/services/topicService';
import type { Topic } from '@/types/topic.types';
 
export interface TopicDetailsPanelProps {
  topic: Topic;
  eventCode?: string;
  onTopicConfirm?: (topicId: string) => void;
  onEditTopic?: (topic: Topic) => void; // Callback to open edit modal
  onTopicDeleted?: () => void; // Callback after topic is deleted
}
 
/**
 * Map database category names (snake_case) to translation keys (camelCase)
 */
const getCategoryTranslationKey = (category: string): string => {
  const categoryMap: Record<string, string> = {
    technical: 'technical',
    management: 'management',
    soft_skills: 'softSkills',
    industry_trends: 'industryTrends',
    tools_platforms: 'toolsPlatforms',
  };
  return categoryMap[category] || category;
};
 
export const TopicDetailsPanel: React.FC<TopicDetailsPanelProps> = ({
  topic,
  eventCode,
  onTopicConfirm,
  onEditTopic,
  onTopicDeleted,
}) => {
  const { t } = useTranslation(['organizer', 'common']);
  const queryClient = useQueryClient();
  const [showOverrideDialog, setShowOverrideDialog] = useState(false);
  const [showSimilarDialog, setShowSimilarDialog] = useState(false);
  const [showDeleteDialog, setShowDeleteDialog] = useState(false);
  const [justification, setJustification] = useState('');
 
  // Fetch similar topics for duplicate detection (AC5)
  const { data: similarTopics } = useSimilarTopics(topic.topicCode);
 
  // Fetch usage history for heat map visualization (AC2)
  const { data: usageHistory } = useTopicUsageHistory(topic.topicCode);
 
  // Mutation for selecting topic
  const selectTopicMutation = useSelectTopicForEvent();
 
  // Mutation for deleting topic
  const deleteTopicMutation = useMutation({
    mutationFn: (topicId: string) => topicService.deleteTopic(topicId),
    onSuccess: () => {
      // Invalidate topics query to refetch the list
      queryClient.invalidateQueries({ queryKey: ['topics'] });
 
      // Call parent callback
      onTopicDeleted?.();
 
      // Close dialog
      setShowDeleteDialog(false);
    },
  });
 
  // Check if topic can be deleted (never been used)
  const canDelete = !topic.usageCount || topic.usageCount === 0;
 
  /**
   * Get status message based on staleness score (AC6)
   */
  const getStatusMessage = (score: number) => {
    if (score >= 83) {
      return {
        severity: 'success' as const,
        message: t(
          'topicBacklog.details.statusGreen',
          'Safe to reuse - last used over 20 months ago'
        ),
      };
    } else if (Iscore >= 50) {
      return {
        severity: 'warning' as const,
        message: t(
          'topicBacklog.details.statusYellow',
          'Use with caution - last used 12-20 months ago'
        ),
      };
    } else {
      return {
        severity: 'error' as const,
        message: t(
          'topicBacklog.details.statusRed',
          'Too recent - last used within 12 months, high duplication risk'
        ),
      };
    }
  };
 
  const status = getStatusMessage(topic.stalenessScore);
 
  // Check if there are high similarity warnings (>70%) - AC5
  const highSimilarityTopics =
    similarTopics?.filter((st) => {
      const similarity = (topic.similarityScores ?? []).find((s) => s.topicCode === st.topicCode);
      return similarity && similarity.score > 0.7;
    }) || [];
 
  const handleSelectTopic = () => {
    Iif (highSimilarityTopics.length > 0) {
      // Show similarity warning dialog (AC5)
      setShowSimilarDialog(true);
    } else {
      confirmTopicSelection();
    }
  };
 
  const confirmTopicSelection = () => {
    Eif (eventCode) {
      selectTopicMutation.mutate(
        {
          eventCode,
          request: {
            topicCode: topic.topicCode,
            justification: justification || undefined,
          },
        },
        {
          onSuccess: () => {
            // Call parent callback to show speaker brainstorming panel
            Eif (onTopicConfirm) {
              onTopicConfirm(topic.topicCode);
            }
          },
        }
      );
    }
    setShowSimilarDialog(false);
  };
 
  return (
    <>
      <Paper sx={{ p: 3 }}>
        <Typography variant="h6" gutterBottom>
          {topic.title}
        </Typography>
 
        <Box sx={{ mb: 2 }}>
          <Chip
            label={t(
              `topicBacklog.filters.categories.${getCategoryTranslationKey(topic.category)}`,
              topic.category
            )}
            size="small"
            sx={{ mr: 1 }}
          />
          <Chip
            label={`${topic.stalenessScore}%`}
            size="small"
            color={
              topic.colorZone === 'red'
                ? 'error'
                : topic.colorZone === 'yellow'
                  ? 'warning'
                  : 'success'
            }
          />
        </Box>
 
        <Typography variant="body2" color="text.secondary" paragraph>
          {topic.description}
        </Typography>
 
        {/* Staleness Status Alert (AC6) */}
        <Alert severity={status.severity} icon={<CheckIcon />} sx={{ mb: 2 }}>
          {status.message}
        </Alert>
 
        {/* Similar Topics Warning (AC5) */}
        {highSimilarityTopics.length > 0 && (
          <Alert severity="warning" icon={<WarningIcon />} sx={{ mb: 2 }}>
            {t(
              'topicBacklog.details.similarityWarning',
              '{{count}} similar topics detected (>70% similarity)',
              { count: highSimilarityTopics.length }
            )}
          </Alert>
        )}
 
        {/* Topic Metrics */}
        <Box sx={{ mb: 2 }}>
          <Typography variant="subtitle2" gutterBottom>
            {t('topicBacklog.details.metrics', 'Metrics')}
          </Typography>
          {topic.lastUsedDate && (
            <Typography variant="body2">
              {t('topicBacklog.details.lastUsed', 'Last Used')}:{' '}
              {new Date(topic.lastUsedDate).toLocaleDateString()}
            </Typography>
          )}
          {topic.usageCount !== undefined && (
            <Typography variant="body2">
              {t('topicBacklog.details.usageCount', 'Usage Count')}: {topic.usageCount}
            </Typography>
          )}
        </Box>
 
        {/* Usage History Heat Map (AC2) */}
        {usageHistory && usageHistory.length > 0 && (
          <Box sx={{ mb: 2 }}>
            <TopicHeatMap topicId={topic.topicCode} usageHistory={usageHistory} />
          </Box>
        )}
 
        {/* Action Buttons */}
        <Box sx={{ display: 'flex', gap: 1, flexDirection: 'column' }}>
          {/* Edit Button - Always shown */}
          <Button
            variant="outlined"
            fullWidth
            onClick={() => onEditTopic?.(topic)}
            disabled={selectTopicMutation.isPending || deleteTopicMutation.isPending}
          >
            {t('topicBacklog.details.editButton', 'Edit Topic')}
          </Button>
 
          {/* Delete Button - Only shown if topic has never been used */}
          {canDelete && (
            <Button
              variant="outlined"
              color="error"
              fullWidth
              onClick={() => setShowDeleteDialog(true)}
              disabled={selectTopicMutation.isPending || deleteTopicMutation.isPending}
            >
              {t('topicBacklog.details.deleteButton', 'Delete Topic')}
            </Button>
          )}
 
          {/* Event-specific actions */}
          {eventCode && (
            <>
              <Button
                variant="contained"
                color="primary"
                fullWidth
                onClick={handleSelectTopic}
                disabled={selectTopicMutation.isPending || deleteTopicMutation.isPending}
                data-testid="confirm-topic-selection-button"
              >
                {selectTopicMutation.isPending
                  ? t('topicBacklog.details.selecting', 'Selecting...')
                  : t('topicBacklog.details.selectButton', 'Select for Event')}
              </Button>
 
              {topic.stalenessScore < 50 && (
                <Button
                  variant="outlined"
                  color="warning"
                  fullWidth
                  onClick={() => setShowOverrideDialog(true)}
                  disabled={selectTopicMutation.isPending || deleteTopicMutation.isPending}
                >
                  {t('topicBacklog.details.overrideButton', 'Override Warning')}
                </Button>
              )}
            </>
          )}
        </Box>
      </Paper>
 
      {/* Similar Topics Warning Dialog (AC5) */}
      <Dialog
        open={showSimilarDialog}
        onClose={() => setShowSimilarDialog(false)}
        maxWidth="sm"
        fullWidth
      >
        <DialogTitle>
          {t('topicBacklog.dialogs.similar.title', 'Similar Topics Detected')}
        </DialogTitle>
        <DialogContent>
          <DialogContentText>
            {t(
              'topicBacklog.dialogs.similar.message',
              'The following topics have high similarity (>70%) with your selection. Are you sure you want to proceed?'
            )}
          </DialogContentText>
          <List dense>
            {highSimilarityTopics.map((st) => {
              const similarity = (topic.similarityScores ?? []).find(
                (s) => s.topicCode === st.topicCode
              );
              return (
                <ListItem key={st.topicCode}>
                  <ListItemText
                    primary={st.title}
                    secondary={`${t('topicBacklog.dialogs.similar.similarity', 'Similarity')}: ${Math.round((similarity?.score || 0) * 100)}%`}
                  />
                </ListItem>
              );
            })}
          </List>
          <TextField
            fullWidth
            multiline
            rows={3}
            label={t('topicBacklog.dialogs.similar.justification', 'Justification (optional)')}
            value={justification}
            onChange={(e) => setJustification(e.target.value)}
            sx={{ mt: 2 }}
          />
        </DialogContent>
        <DialogActions>
          <Button onClick={() => setShowSimilarDialog(false)}>{t('common:actions.cancel')}</Button>
          <Button onClick={confirmTopicSelection} color="primary" variant="contained">
            {t('topicBacklog.dialogs.similar.confirm', 'Select Anyway')}
          </Button>
        </DialogActions>
      </Dialog>
 
      {/* Override Staleness Dialog (AC7) */}
      <Dialog
        open={showOverrideDialog}
        onClose={() => setShowOverrideDialog(false)}
        maxWidth="sm"
        fullWidth
      >
        <DialogTitle>
          {t('topicBacklog.dialogs.override.title', 'Override Staleness Warning')}
        </DialogTitle>
        <DialogContent>
          <DialogContentText>
            {t(
              'topicBacklog.dialogs.override.message',
              'This topic was used recently (staleness score: {{score}}%). Please provide justification for using it again.',
              { score: topic.stalenessScore }
            )}
          </DialogContentText>
          <TextField
            fullWidth
            required
            multiline
            rows={4}
            label={t('topicBacklog.dialogs.override.justification', 'Justification')}
            value={justification}
            onChange={(e) => setJustification(e.target.value)}
            sx={{ mt: 2 }}
            helperText={t(
              'topicBacklog.dialogs.override.helper',
              'Explain why this topic should be reused despite being recent'
            )}
          />
        </DialogContent>
        <DialogActions>
          <Button onClick={() => setShowOverrideDialog(false)}>{t('common:actions.cancel')}</Button>
          <Button
            onClick={confirmTopicSelection}
            color="primary"
            variant="contained"
            disabled={!justification.trim()}
          >
            {t('topicBacklog.dialogs.override.confirm', 'Override and Select')}
          </Button>
        </DialogActions>
      </Dialog>
 
      {/* Delete Topic Confirmation Dialog */}
      <Dialog
        open={showDeleteDialog}
        onClose={() => setShowDeleteDialog(false)}
        maxWidth="sm"
        fullWidth
      >
        <DialogTitle>{t('topicBacklog.dialogs.delete.title', 'Delete Topic')}</DialogTitle>
        <DialogContent>
          <DialogContentText>
            {t(
              'topicBacklog.dialogs.delete.message',
              'Are you sure you want to delete "{{title}}"? This action cannot be undone.',
              { title: topic.title }
            )}
          </DialogContentText>
          {deleteTopicMutation.isError && (
            <Alert severity="error" sx={{ mt: 2 }}>
              {t('topicBacklog.dialogs.delete.error', 'Failed to delete topic. Please try again.')}
            </Alert>
          )}
        </DialogContent>
        <DialogActions>
          <Button
            onClick={() => setShowDeleteDialog(false)}
            disabled={deleteTopicMutation.isPending}
          >
            {t('common:actions.cancel')}
          </Button>
          <Button
            onClick={() => deleteTopicMutation.mutate(topic.topicCode)}
            color="error"
            variant="contained"
            disabled={deleteTopicMutation.isPending}
          >
            {deleteTopicMutation.isPending
              ? t('common:actions.deleting')
              : t('topicBacklog.dialogs.delete.confirm', 'Delete Topic')}
          </Button>
        </DialogActions>
      </Dialog>
    </>
  );
};
 
export default TopicDetailsPanel;