All files / src/components/organizer/LiveControl ActiveSessionCard.tsx

70.58% Statements 24/34
54.11% Branches 46/85
45.45% Functions 5/11
75% Lines 24/32

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                        2x                               2x 2x 2x       4x 4x                     1x                     2x 2x   2x                                                   2x     2x 2x 2x           2x                     2x             2x             2x               2x                 2x                 2x   2x   2x                                                 2x                                                                                                                                                                        
import React, { useState } from 'react';
import { Card, CardContent } from '@/components/public/ui/card';
import { cn } from '@/lib/utils';
import type { components } from '@/types/generated/event-watch-api.types';
import { ExtendSessionSheet } from './ExtendSessionSheet';
import { DelaySessionSheet } from './DelaySessionSheet';
import { LogoBadge } from '@/components/shared/LogoBadge/LogoBadge';
 
type WatchSessionDetail = components['schemas']['WatchSessionDetail'];
type WatchSpeaker = WatchSessionDetail['speakers'][number];
 
function speakerName(s: WatchSpeaker): string {
  return [s.firstName, s.lastName].filter(Boolean).join(' ') || s.username || '';
}
 
interface ActiveSessionCardProps {
  activeSession: WatchSessionDetail | null;
  nextSession: WatchSessionDetail | null;
  remainingSeconds: number;
  elapsedSeconds: number;
  shouldShowExtend: boolean;
  shouldShowDelay: boolean;
  isActionInFlight: boolean;
  sendExtend: (minutes: number) => void;
  sendDelay: (minutes: number) => void;
}
 
function formatCountdown(seconds: number): string {
  const m = Math.floor(seconds / 60);
  const s = seconds % 60;
  return `${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`;
}
 
function formatTime(iso: string | null | undefined): string {
  Iif (!iso) return '';
  return new Date(iso).toLocaleTimeString('de-CH', { hour: '2-digit', minute: '2-digit' });
}
 
function speakerNames(session: WatchSessionDetail): string {
  if (!session.speakers?.length) return '';
  return session.speakers
    .map((s) => [s.firstName, s.lastName].filter(Boolean).join(' '))
    .filter(Boolean)
    .join(', ');
}
 
export const ActiveSessionCard: React.FC<ActiveSessionCardProps> = ({
  activeSession,
  nextSession,
  remainingSeconds,
  elapsedSeconds,
  shouldShowExtend,
  shouldShowDelay,
  isActionInFlight,
  sendExtend,
  sendDelay,
}) => {
  const [extendOpen, setExtendOpen] = useState(false);
  const [delayOpen, setDelayOpen] = useState(false);
 
  Iif (!activeSession) {
    return (
      <Card className="mb-4 border-border bg-card text-card-foreground">
        <CardContent className="py-10 text-center">
          <div className="text-4xl mb-3">⏱</div>
          <p className="text-xl font-semibold text-muted-foreground">Keine aktive Session</p>
          {nextSession && (
            <div className="mt-5">
              <p className="text-sm text-muted-foreground uppercase tracking-widest mb-2">
                Nächste Session
              </p>
              <p className="text-xl font-bold text-foreground">{nextSession.title}</p>
              {speakerNames(nextSession) && (
                <p className="text-base text-muted-foreground mt-1">{speakerNames(nextSession)}</p>
              )}
              <p className="text-lg text-primary font-mono mt-2">
                {formatTime(nextSession.scheduledStartTime)}
              </p>
            </div>
          )}
        </CardContent>
      </Card>
    );
  }
 
  const totalSeconds =
    activeSession.durationMinutes != null
      ? activeSession.durationMinutes * 60
      : elapsedSeconds + remainingSeconds;
  const progress = totalSeconds > 0 ? Math.min((elapsedSeconds / totalSeconds) * 100, 100) : 0;
  const isOvertime = remainingSeconds === 0 && elapsedSeconds > 0;
  const activeSpeakers = activeSession.speakers ?? [];
 
  // Urgency levels mirroring Watch app SessionTimerEngine exactly:
  // normal (>300s): primary blue | caution (120–300s): primary blue
  // warning (60–120s): orange | critical (<60s): orange | overtime: red
  type UrgencyLevel = 'normal' | 'caution' | 'warning' | 'critical' | 'overtime';
  const urgency: UrgencyLevel = isOvertime
    ? 'overtime'
    : remainingSeconds <= 60
      ? 'critical'
      : remainingSeconds <= 120
        ? 'warning'
        : remainingSeconds <= 300
          ? 'caution'
          : 'normal';
 
  const countdownColor =
    urgency === 'overtime'
      ? 'text-red-400'
      : urgency === 'warning' || urgency === 'critical'
        ? 'text-orange-400'
        : 'text-primary';
 
  const progressColor =
    urgency === 'overtime'
      ? 'bg-red-500'
      : urgency === 'warning' || urgency === 'critical'
        ? 'bg-orange-500'
        : 'bg-primary';
 
  const borderColor =
    urgency === 'overtime'
      ? 'border-red-500'
      : urgency === 'warning' || urgency === 'critical'
        ? 'border-orange-500'
        : 'border-primary/40';
 
  // Status label matching Watch (German)
  const urgencyLabel =
    urgency === 'overtime'
      ? 'ÜBERZEIT'
      : urgency === 'critical' || urgency === 'warning'
        ? '2 MIN NOCH'
        : urgency === 'caution'
          ? '5 MIN NOCH'
          : 'IM PLAN';
 
  const urgencyLabelColor =
    urgency === 'overtime'
      ? 'text-red-400'
      : urgency === 'warning' || urgency === 'critical'
        ? 'text-orange-400'
        : urgency === 'caution'
          ? 'text-primary'
          : 'text-muted-foreground';
 
  // Pulse the countdown at critical (<1 min) and overtime — visual urgency cue (replaces haptics)
  const countdownPulse = urgency === 'critical' || urgency === 'overtime';
 
  const overtimeSeconds = isOvertime ? elapsedSeconds - (totalSeconds > 0 ? totalSeconds : 0) : 0;
 
  return (
    <>
      <Card className={cn('mb-4 border-2 bg-card text-card-foreground', borderColor)}>
        <CardContent className="p-5">
          {/* Live badge + time range */}
          <div className="flex items-center gap-2 mb-4">
            <span className="inline-block w-2.5 h-2.5 rounded-full bg-red-500 animate-pulse shrink-0" />
            <span className="text-red-400 font-bold tracking-[0.2em] text-sm">LIVE</span>
            <span className="ml-auto text-sm text-muted-foreground font-mono">
              {formatTime(activeSession.scheduledStartTime)}
              {' – '}
              {formatTime(activeSession.scheduledEndTime)}
            </span>
          </div>
 
          {/* Session title */}
          <h2 className="text-3xl font-bold leading-tight text-foreground mb-2">
            {activeSession.title}
          </h2>
 
          {/* Speakers — each with their company logo on a white chip (Story 15.5, feedback #5).
              Transparent logos stay legible on the dark card via LogoBadge; no logo → name only. */}
          {activeSpeakers.length > 0 && (
            <div className="flex flex-wrap items-center gap-x-4 gap-y-2 mb-4">
              {activeSpeakers.map((s, i) => (
                <span
                  key={s.username ?? i}
                  className="inline-flex items-center gap-2 text-lg text-muted-foreground"
                  data-testid="active-speaker"
                >
                  {s.companyLogoUrl && (
                    <LogoBadge
                      src={s.companyLogoUrl}
                      alt={s.company ?? ''}
                      imgStyle={{ height: 20, maxWidth: 80 }}
                      style={{ padding: '2px 5px', borderRadius: 4 }}
                    />
                  )}
                  {speakerName(s)}
                </span>
              ))}
            </div>
          )}
 
          {/* Countdown */}
          <div className="text-center py-4">
            <p
              className={cn(
                'text-8xl font-bold font-mono leading-none',
                countdownColor,
                countdownPulse && 'animate-pulse'
              )}
            >
              {isOvertime
                ? `+${formatCountdown(overtimeSeconds)}`
                : formatCountdown(remainingSeconds)}
            </p>
            <p
              className={cn('text-sm font-bold tracking-[0.2em] mt-2 uppercase', urgencyLabelColor)}
            >
              {urgencyLabel}
            </p>
          </div>
 
          {/* Progress bar */}
          <div className="w-full h-2 bg-muted rounded-full mb-5 overflow-hidden">
            <div
              className={cn('h-full rounded-full transition-all duration-1000', progressColor)}
              style={{ width: `${progress}%` }}
            />
          </div>
 
          {/* Action buttons */}
          <div className="flex gap-3">
            {shouldShowDelay && (
              <button
                disabled={isActionInFlight}
                onClick={() => setDelayOpen(true)}
                className="flex-1 py-4 rounded-xl text-base font-bold border-2 border-amber-500 text-amber-400 hover:bg-amber-500/10 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
              >
                ← Verzögern
              </button>
            )}
            {shouldShowExtend && (
              <button
                onClick={() => setExtendOpen(true)}
                className="flex-1 py-4 rounded-xl text-base font-bold border-2 border-primary text-primary hover:bg-primary/10 transition-colors"
              >
                ↔ Zeit anpassen
              </button>
            )}
          </div>
        </CardContent>
      </Card>
 
      <ExtendSessionSheet
        open={extendOpen}
        onClose={() => setExtendOpen(false)}
        onExtend={sendExtend}
      />
      <DelaySessionSheet
        open={delayOpen}
        onClose={() => setDelayOpen(false)}
        onDelay={sendDelay}
        disabled={isActionInFlight}
      />
    </>
  );
};