All files / src/components/TopicBacklogManager TopicList.tsx

88.23% Statements 15/17
76.19% Branches 16/21
100% Functions 6/6
93.33% Lines 14/15

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                                                                            1x 10x 10x 5x           1x 10x             10x     1x             5x   5x                   5x       10x               1x                                                                                                                             1x                      
/**
 * Topic List Component (Story 5.2 - AC1, AC3)
 *
 * Displays list of topics with:
 * - Color-coded staleness indicators
 * - Usage count and last used date
 * - Selection highlighting
 * - Pagination
 */
 
import React from 'react';
import {
  List,
  ListItem,
  ListItemButton,
  ListItemText,
  Chip,
  Box,
  Typography,
  Pagination,
} from '@mui/material';
import { useTranslation } from 'react-i18next';
import type { Topic, PaginationMetadata } from '@/types/topic.types';
 
export interface TopicListProps {
  topics: Topic[];
  selectedTopicId?: string;
  onTopicSelect: (topic: Topic) => void;
  pagination: PaginationMetadata;
  onPageChange: (page: number) => void;
}
 
/**
 * Get color for staleness score (AC3)
 * - Red (<50): Too recent, high risk
 * - Yellow (50-83): Use with caution
 * - Green (>83): Safe to reuse
 */
const getColorForStaleness = (score: number): 'error' | 'warning' | 'success' => {
  Iif (score < 50) return 'error';
  if (score <= 83) return 'warning';
  return 'success';
};
 
/**
 * 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 TopicList: React.FC<TopicListProps> = ({
  topics,
  selectedTopicId,
  onTopicSelect,
  pagination,
  onPageChange,
}) => {
  const { t } = useTranslation('organizer');
 
  Iif (topics.length === 0) {
    return (
      <Box sx={{ textAlign: 'center', py: 4 }}>
        <Typography variant="body1" color="text.secondary">
          {t('topicBacklog.noTopics', 'No topics found matching your filters')}
        </Typography>
      </Box>
    );
  }
 
  return (
    <>
      <List data-testid="topic-list">
        {topics.map((topic) => (
          <ListItem
            key={topic.topicCode}
            disablePadding
            data-testid={`topic-card-${topic.topicCode}`}
            data-freshness={topic.colorZone}
          >
            <ListItemButton
              selected={topic.topicCode === selectedTopicId}
              onClick={() => onTopicSelect(topic)}
              sx={{
                borderLeft: 4,
                borderColor:
                  topic.colorZone === 'red'
                    ? 'error.main'
                    : topic.colorZone === 'yellow'
                      ? 'warning.main'
                      : topic.colorZone === 'green'
                        ? 'success.main'
                        : 'grey.300',
                mb: 1,
              }}
            >
              <ListItemText
                primary={
                  <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
                    <Typography variant="subtitle1">{topic.title}</Typography>
                    <Chip
                      label={`${topic.stalenessScore}%`}
                      size="small"
                      color={getColorForStaleness(topic.stalenessScore)}
                      data-testid={`staleness-score-${topic.topicCode}`}
                    />
                  </Box>
                }
                secondary={
                  <>
                    <Typography component="span" variant="body2" color="text.secondary">
                      {t(
                        `topicBacklog.filters.categories.${getCategoryTranslationKey(topic.category)}`,
                        topic.category
                      )}
                      {topic.lastUsedDate && (
                        <>
                          {' • '}
                          {t('topicBacklog.lastUsed', 'Last used')}:{' '}
                          {new Date(topic.lastUsedDate).toLocaleDateString()}
                        </>
                      )}
                      {topic.usageCount && topic.usageCount > 0 && (
                        <>
                          {' • '}
                          {t('topicBacklog.usageCount', 'Used {{count}} times', {
                            count: topic.usageCount,
                          })}
                        </>
                      )}
                    </Typography>
                  </>
                }
              />
            </ListItemButton>
          </ListItem>
        ))}
      </List>
 
      {/* Pagination */}
      {pagination.totalItems > pagination.limit && (
        <Box sx={{ display: 'flex', justifyContent: 'center', mt: 2 }}>
          <Pagination
            count={pagination.totalPages}
            page={pagination.page}
            onChange={(_, page) => onPageChange(page)}
            color="primary"
            data-testid="topic-pagination"
          />
        </Box>
      )}
    </>
  );
};
 
export default TopicList;