All files / src/components/Publishing/PublishingTimeline PublishingTimeline.tsx

97.22% Statements 70/72
90.69% Branches 39/43
100% Functions 12/12
96.87% Lines 62/64

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                                                    1x   1x               18x 18x 54x   18x 108x 58x 50x     18x 54x 54x 25x 29x 29x 25x     18x 4x     18x 54x   36x 36x 2x 2x 2x   2x 1x   1x       34x 34x 16x 16x 16x   18x 18x 18x 18x         18x 54x 54x   2x 2x 2x   2x 1x                 1x                   29x 18x 18x   18x                                                                                       54x 54x 54x   54x                           54x 54x 1x                           53x 53x 2x                           51x 51x                                                                              
import React from 'react';
import { Box, Step, StepLabel, Stepper, Typography, Chip } from '@mui/material';
import { useTranslation } from 'react-i18next';
import {
  CheckCircle as CheckIcon,
  RadioButtonUnchecked as PendingIcon,
  FiberManualRecord as CurrentIcon,
} from '@mui/icons-material';
import { PublishingPhase } from '@/types/event.types';
import { formatDate } from '@/utils/date/dateFormat';
 
interface AutoPublishSchedule {
  phase: PublishingPhase;
  scheduledDate: string;
}
 
export interface PublishingTimelineProps {
  eventCode: string;
  currentPhase: PublishingPhase;
  publishedPhases: PublishingPhase[];
  eventDate: string;
  autoPublishSchedule?: AutoPublishSchedule[];
  publishedDates?: Partial<Record<PublishingPhase, string>>;
  scheduledDates?: Partial<Record<PublishingPhase, string>>;
}
 
const PHASE_KEYS: PublishingPhase[] = ['topic', 'speakers', 'agenda'];
 
export const PublishingTimeline: React.FC<PublishingTimelineProps> = ({
  currentPhase,
  publishedPhases,
  eventDate,
  autoPublishSchedule = [],
  publishedDates = {},
  scheduledDates = {},
}) => {
  const { t, i18n } = useTranslation('events');
  const locale = i18n.language === 'de' ? 'de' : 'en';
  const phases = PHASE_KEYS.map((key) => ({ key, label: t(`publishing.phases.${key}`) }));
 
  const getPhaseClass = (phaseKey: string): string => {
    if (publishedPhases.includes(phaseKey as PublishingPhase)) return 'complete';
    if (phaseKey === currentPhase) return 'current';
    return 'pending';
  };
 
  const getPhaseIcon = (phaseKey: string) => {
    const phaseClass = getPhaseClass(phaseKey);
    if (phaseClass === 'complete')
      return <CheckIcon color="success" data-testid={`icon-complete-${phaseKey}`} />;
    if (phaseClass === 'current')
      return <CurrentIcon color="primary" data-testid={`icon-current-${phaseKey}`} />;
    return <PendingIcon color="disabled" data-testid={`icon-pending-${phaseKey}`} />;
  };
 
  const formatLocalDate = (dateStr: string, fmt?: string): string => {
    return formatDate(new Date(dateStr), locale, fmt);
  };
 
  const getScheduledDate = (phaseKey: string): string | null => {
    if (phaseKey === 'topic') return t('publishing.timeline.now');
 
    const schedule = autoPublishSchedule.find((s) => s.phase === phaseKey);
    if (schedule) {
      const date = new Date(schedule.scheduledDate);
      const now = new Date();
      const diffDays = Math.ceil((date.getTime() - now.getTime()) / (1000 * 60 * 60 * 24));
 
      if (diffDays > 0) {
        return t('publishing.timeline.inDays', { count: diffDays });
      }
      return formatLocalDate(schedule.scheduledDate);
    }
 
    // Default milestones based on phase
    const eventDateObj = new Date(eventDate);
    if (phaseKey === 'speakers') {
      const speakersDate = new Date(eventDateObj);
      speakersDate.setMonth(speakersDate.getMonth() - 1);
      return formatDate(speakersDate, locale);
    }
    Eif (phaseKey === 'agenda') {
      const agendaDate = new Date(eventDateObj);
      agendaDate.setDate(agendaDate.getDate() - 14);
      return formatDate(agendaDate, locale);
    }
    return null;
  };
 
  const getAutoPublishLabel = (phaseKey: string): React.ReactNode | null => {
    const schedule = autoPublishSchedule.find((s) => s.phase === phaseKey);
    if (!schedule) return null;
 
    const date = new Date(schedule.scheduledDate);
    const now = new Date();
    const diffDays = Math.ceil((date.getTime() - now.getTime()) / (1000 * 60 * 60 * 24));
 
    if (diffDays > 0) {
      return (
        <Chip
          label={t('publishing.autoPublishInDays', { days: diffDays })}
          size="small"
          color="info"
          data-testid={`auto-publish-${phaseKey}`}
        />
      );
    }
    return (
      <Chip
        label={t('publishing.autoPublishScheduled')}
        size="small"
        color="default"
        data-testid={`auto-publish-${phaseKey}`}
      />
    );
  };
 
  const activeStep = phases.findIndex((p) => p.key === currentPhase);
  const completedSteps = publishedPhases.length;
  const progressPercent = completedSteps === 0 ? 0 : (completedSteps / phases.length) * 100;
 
  return (
    <Box>
      {/* Progress Bar */}
      <Box sx={{ mb: 3 }}>
        <Box
          sx={{
            width: '100%',
            height: '8px',
            bgcolor: 'grey.200',
            borderRadius: '4px',
            overflow: 'hidden',
          }}
          data-testid="progress-line"
        >
          <Box
            sx={{
              width: `${progressPercent}%`,
              height: '100%',
              bgcolor: 'primary.main',
              transition: 'width 0.3s ease',
            }}
            data-testid="progress-fill"
          />
        </Box>
        <Typography variant="caption" color="text.secondary" sx={{ mt: 0.5, display: 'block' }}>
          {t('publishing.timeline.progress', {
            completed: completedSteps,
            total: phases.length,
            percent: Math.round(progressPercent),
          })}
        </Typography>
      </Box>
 
      {/* Timeline Stepper */}
      <Stepper
        activeStep={activeStep}
        orientation="vertical"
        sx={{
          '& .MuiStepConnector-line': {
            minHeight: '12px',
          },
        }}
      >
        {phases.map((phase) => {
          const phaseClass = getPhaseClass(phase.key);
          const scheduledDate = getScheduledDate(phase.key);
          const autoPublishLabel = getAutoPublishLabel(phase.key);
 
          return (
            <Step
              key={phase.key}
              completed={phaseClass === 'complete'}
              active={phaseClass === 'current'}
            >
              <StepLabel
                icon={getPhaseIcon(phase.key)}
                data-testid={`timeline-phase-${phase.key}`}
                className={phaseClass}
                optional={
                  <Box>
                    {(() => {
                      // Show published date if phase is published
                      const publishedDate = publishedDates[phase.key as PublishingPhase];
                      if (publishedDate && phaseClass === 'complete') {
                        return (
                          <Typography
                            variant="caption"
                            color="text.secondary"
                            data-testid={`published-date-${phase.key}`}
                          >
                            {t('publishing.timeline.publishedOn', {
                              date: formatLocalDate(publishedDate, 'd. MMM yyyy'),
                            })}
                          </Typography>
                        );
                      }
 
                      // Show scheduled date if provided
                      const schedDate = scheduledDates[phase.key as PublishingPhase];
                      if (schedDate) {
                        return (
                          <Typography
                            variant="caption"
                            color="text.secondary"
                            data-testid={`scheduled-date-${phase.key}`}
                          >
                            {t('publishing.timeline.scheduled', {
                              date: formatLocalDate(schedDate, 'd. MMM yyyy'),
                            })}
                          </Typography>
                        );
                      }
 
                      // Show default milestone date
                      Eif (scheduledDate) {
                        return (
                          <Typography
                            variant="caption"
                            color="text.secondary"
                            data-testid={`milestone-${phase.key}`}
                          >
                            {scheduledDate}
                          </Typography>
                        );
                      }
 
                      return null;
                    })()}
                    {autoPublishLabel}
                  </Box>
                }
              >
                <Typography
                  variant="subtitle1"
                  sx={{
                    fontWeight: phaseClass === 'current' ? 600 : 400,
                    color:
                      phaseClass === 'complete'
                        ? 'success.main'
                        : phaseClass === 'current'
                          ? 'primary.main'
                          : 'text.secondary',
                  }}
                >
                  {phase.label}
                </Typography>
              </StepLabel>
            </Step>
          );
        })}
      </Stepper>
    </Box>
  );
};