All files / web/src/components/vision VisionRecordingPlayer.tsx

0% Statements 0/334
0% Branches 0/1
0% Functions 0/1
0% Lines 0/334

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
'use client'

import { useRef, useState, useCallback, useEffect } from 'react'
import { css } from '../../../styled-system/css'

/**
 * Problem marker for timeline synchronization
 */
export interface ProblemMarker {
  offsetMs: number
  problemNumber: number
  partIndex: number
  eventType: 'problem-shown' | 'answer-submitted' | 'feedback-shown'
  isCorrect?: boolean
}

interface VisionRecordingPlayerProps {
  /** URL to the video file */
  videoUrl: string
  /** Total duration in milliseconds */
  durationMs: number
  /** Problem markers for timeline */
  problemMarkers?: ProblemMarker[] | null
  /** Callback when playback position changes */
  onTimeUpdate?: (currentTimeMs: number) => void
  /** Whether to autoplay */
  autoPlay?: boolean
}

/**
 * Video player for vision recordings with problem marker timeline.
 *
 * Features:
 * - Standard HTML5 video controls (play, pause, seek, volume)
 * - Problem marker timeline below video
 * - Click markers to seek to that point
 * - Hover to see problem info
 */
export function VisionRecordingPlayer({
  videoUrl,
  durationMs,
  problemMarkers,
  onTimeUpdate,
  autoPlay = false,
}: VisionRecordingPlayerProps) {
  const videoRef = useRef<HTMLVideoElement>(null)
  const [currentTime, setCurrentTime] = useState(0)
  const [isPlaying, setIsPlaying] = useState(false)
  const [hoveredMarker, setHoveredMarker] = useState<ProblemMarker | null>(null)

  // Handle time updates
  const handleTimeUpdate = useCallback(() => {
    if (videoRef.current) {
      const timeMs = videoRef.current.currentTime * 1000
      setCurrentTime(timeMs)
      onTimeUpdate?.(timeMs)
    }
  }, [onTimeUpdate])

  // Seek to specific time
  const seekTo = useCallback((timeMs: number) => {
    if (videoRef.current) {
      videoRef.current.currentTime = timeMs / 1000
    }
  }, [])

  // Handle marker click
  const handleMarkerClick = useCallback(
    (marker: ProblemMarker) => {
      seekTo(marker.offsetMs)
    },
    [seekTo]
  )

  // Track play/pause state
  useEffect(() => {
    const video = videoRef.current
    if (!video) return

    const handlePlay = () => setIsPlaying(true)
    const handlePause = () => setIsPlaying(false)

    video.addEventListener('play', handlePlay)
    video.addEventListener('pause', handlePause)

    return () => {
      video.removeEventListener('play', handlePlay)
      video.removeEventListener('pause', handlePause)
    }
  }, [])

  // Group markers by problem for display
  const problemGroups = problemMarkers ? groupMarkersByProblem(problemMarkers) : []

  return (
    <div
      data-component="vision-recording-player"
      className={css({
        display: 'flex',
        flexDirection: 'column',
        gap: 2,
        bg: 'gray.900',
        borderRadius: 'lg',
        overflow: 'hidden',
      })}
    >
      {/* Video element */}
      {/* biome-ignore lint/a11y/useMediaCaption: vision recording playback where captions are not applicable */}
      <video
        ref={videoRef}
        src={videoUrl}
        controls
        autoPlay={autoPlay}
        onTimeUpdate={handleTimeUpdate}
        className={css({
          width: '100%',
          height: 'auto',
          display: 'block',
          bg: 'black',
        })}
      />

      {/* Problem marker timeline */}
      {problemMarkers && problemMarkers.length > 0 && (
        <div
          data-element="marker-timeline"
          className={css({
            position: 'relative',
            height: '48px',
            bg: 'gray.800',
            mx: 2,
            mb: 2,
            borderRadius: 'md',
            overflow: 'hidden',
          })}
        >
          {/* Timeline track */}
          <div
            className={css({
              position: 'absolute',
              top: '50%',
              left: 0,
              right: 0,
              height: '4px',
              bg: 'gray.700',
              transform: 'translateY(-50%)',
            })}
          />

          {/* Current time indicator */}
          <div
            className={css({
              position: 'absolute',
              top: 0,
              bottom: 0,
              width: '2px',
              bg: 'cyan.500',
              transition: 'left 0.1s linear',
            })}
            style={{ left: `${(currentTime / durationMs) * 100}%` }}
          />

          {/* Problem markers */}
          {problemGroups.map((group, idx) => {
            const position = (group.shownAt / durationMs) * 100
            const isCorrect = group.isCorrect
            const isCurrent =
              currentTime >= group.shownAt &&
              (idx === problemGroups.length - 1 || currentTime < problemGroups[idx + 1].shownAt)

            return (
              <button
                key={`problem-${group.problemNumber}-${group.partIndex}`}
                data-element="problem-marker"
                data-problem={group.problemNumber}
                data-correct={isCorrect}
                onClick={() =>
                  handleMarkerClick({
                    offsetMs: group.shownAt,
                    problemNumber: group.problemNumber,
                    partIndex: group.partIndex,
                    eventType: 'problem-shown',
                  })
                }
                onMouseEnter={() =>
                  setHoveredMarker({
                    offsetMs: group.shownAt,
                    problemNumber: group.problemNumber,
                    partIndex: group.partIndex,
                    eventType: 'problem-shown',
                    isCorrect,
                  })
                }
                onMouseLeave={() => setHoveredMarker(null)}
                className={css({
                  position: 'absolute',
                  top: '50%',
                  transform: 'translate(-50%, -50%)',
                  width: '24px',
                  height: '24px',
                  borderRadius: 'full',
                  border: '2px solid',
                  borderColor: isCurrent ? 'cyan.400' : 'transparent',
                  bg:
                    isCorrect === true ? 'green.500' : isCorrect === false ? 'red.500' : 'gray.500',
                  color: 'white',
                  fontSize: 'xs',
                  fontWeight: 'bold',
                  cursor: 'pointer',
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                  transition: 'all 0.15s',
                  _hover: {
                    transform: 'translate(-50%, -50%) scale(1.2)',
                    zIndex: 10,
                  },
                })}
                style={{ left: `${position}%` }}
              >
                {group.problemNumber}
              </button>
            )
          })}

          {/* Hover tooltip */}
          {hoveredMarker && (
            <div
              data-element="marker-tooltip"
              className={css({
                position: 'absolute',
                bottom: '100%',
                left: '50%',
                transform: 'translateX(-50%)',
                mb: 1,
                px: 2,
                py: 1,
                bg: 'gray.900',
                borderRadius: 'md',
                fontSize: 'xs',
                color: 'white',
                whiteSpace: 'nowrap',
                zIndex: 20,
              })}
            >
              Problem {hoveredMarker.problemNumber} (Part {hoveredMarker.partIndex + 1})
              {hoveredMarker.isCorrect !== undefined && (
                <span
                  className={css({
                    ml: 1,
                    color: hoveredMarker.isCorrect ? 'green.400' : 'red.400',
                  })}
                >
                  {hoveredMarker.isCorrect ? '✓' : '✗'}
                </span>
              )}
            </div>
          )}
        </div>
      )}

      {/* Recording info */}
      <div
        data-element="recording-info"
        className={css({
          display: 'flex',
          justifyContent: 'space-between',
          px: 3,
          pb: 2,
          fontSize: 'xs',
          color: 'gray.400',
        })}
      >
        <span>
          {formatDuration(currentTime)} / {formatDuration(durationMs)}
        </span>
        {problemMarkers && (
          <span>
            {problemMarkers.filter((m) => m.eventType === 'problem-shown').length} problems
          </span>
        )}
      </div>
    </div>
  )
}

/**
 * Group markers by problem number and extract key info
 */
interface ProblemGroup {
  problemNumber: number
  partIndex: number
  shownAt: number
  answeredAt?: number
  isCorrect?: boolean
}

function groupMarkersByProblem(markers: ProblemMarker[]): ProblemGroup[] {
  const groups = new Map<string, ProblemGroup>()

  for (const marker of markers) {
    const key = `${marker.partIndex}-${marker.problemNumber}`

    if (!groups.has(key)) {
      groups.set(key, {
        problemNumber: marker.problemNumber,
        partIndex: marker.partIndex,
        shownAt: marker.offsetMs,
      })
    }

    const group = groups.get(key)!

    if (marker.eventType === 'problem-shown') {
      group.shownAt = Math.min(group.shownAt, marker.offsetMs)
    } else if (marker.eventType === 'answer-submitted') {
      group.answeredAt = marker.offsetMs
      group.isCorrect = marker.isCorrect
    }
  }

  // Sort by shownAt time
  return Array.from(groups.values()).sort((a, b) => a.shownAt - b.shownAt)
}

/**
 * Format duration in ms to mm:ss
 */
function formatDuration(ms: number): string {
  const totalSeconds = Math.floor(ms / 1000)
  const minutes = Math.floor(totalSeconds / 60)
  const seconds = totalSeconds % 60
  return `${minutes}:${seconds.toString().padStart(2, '0')}`
}