All files / web/src/components/practice PracticeTimingDisplay.tsx

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

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

import { useEffect, useState } from 'react'
import type { SessionPart, SlotResult } from '@/db/schema/session-plans'
import { css } from '../../../styled-system/css'
import { SpeedMeter } from './SpeedMeter'

interface PracticeTimingDisplayProps {
  /** Session results so far */
  results: SlotResult[]
  /** Session parts (to map result partNumber to part type) */
  parts: SessionPart[]
  /** Current attempt start time (for live timer) */
  attemptStartTime: number
  /** Accumulated pause time for current attempt */
  accumulatedPauseMs: number
  /** Whether session is paused */
  isPaused: boolean
  /** Current part type */
  currentPartType: 'abacus' | 'visualization' | 'linear'
  /** Whether dark mode is enabled */
  isDark: boolean
}

// Minimum samples needed for statistical display
const MIN_SAMPLES_FOR_STATS = 3

/**
 * Format milliseconds as a human-readable duration
 */
function formatTime(ms: number): string {
  if (ms < 0) return '0s'
  const totalSeconds = Math.floor(ms / 1000)
  const minutes = Math.floor(totalSeconds / 60)
  const seconds = totalSeconds % 60

  if (minutes > 0) {
    return `${minutes}:${seconds.toString().padStart(2, '0')}`
  }
  return `${seconds}s`
}

/**
 * Format milliseconds for display as decimal seconds
 */
function formatSecondsDecimal(ms: number): string {
  return `${(ms / 1000).toFixed(1)}s`
}

/**
 * Calculate mean and standard deviation of response times
 */
function calculateStats(times: number[]): {
  mean: number
  stdDev: number
  count: number
} {
  if (times.length === 0) {
    return { mean: 0, stdDev: 0, count: 0 }
  }

  const count = times.length
  const mean = times.reduce((sum, t) => sum + t, 0) / count

  if (count < 2) {
    return { mean, stdDev: 0, count }
  }

  const squaredDiffs = times.map((t) => (t - mean) ** 2)
  const variance = squaredDiffs.reduce((sum, d) => sum + d, 0) / (count - 1)
  const stdDev = Math.sqrt(variance)

  return { mean, stdDev, count }
}

function getPartTypeLabel(type: 'abacus' | 'visualization' | 'linear'): string {
  switch (type) {
    case 'abacus':
      return 'Abacus'
    case 'visualization':
      return 'Visualize'
    case 'linear':
      return 'Linear'
  }
}

function getPartTypeEmoji(type: 'abacus' | 'visualization' | 'linear'): string {
  switch (type) {
    case 'abacus':
      return '🧮'
    case 'visualization':
      return '🧠'
    case 'linear':
      return '💭'
  }
}

/**
 * PracticeTimingDisplay - Shows timing stats during practice
 *
 * Displays:
 * - Live timer for current problem
 * - Average time per problem with SpeedMeter visualization
 * - Breakdown by part type (abacus/visualization/linear)
 */
export function PracticeTimingDisplay({
  results,
  parts,
  attemptStartTime,
  accumulatedPauseMs,
  isPaused,
  currentPartType,
  isDark,
}: PracticeTimingDisplayProps) {
  // Live-updating current problem timer
  const [currentElapsedMs, setCurrentElapsedMs] = useState(0)

  // Update current timer every 100ms
  useEffect(() => {
    if (isPaused) return

    const updateTimer = () => {
      const elapsed = Date.now() - attemptStartTime - accumulatedPauseMs
      setCurrentElapsedMs(Math.max(0, elapsed))
    }

    updateTimer()
    const interval = setInterval(updateTimer, 100)
    return () => clearInterval(interval)
  }, [attemptStartTime, accumulatedPauseMs, isPaused])

  // Calculate overall stats
  const allTimes = results.map((r) => r.responseTimeMs)
  const overallStats = calculateStats(allTimes)
  const hasEnoughData = overallStats.count >= MIN_SAMPLES_FOR_STATS

  // Calculate per-part-type stats
  const partTypeStats = (['abacus', 'visualization', 'linear'] as const).map((type) => {
    const typeTimes = results
      .filter((r) => {
        const part = parts[r.partNumber - 1]
        return part && part.type === type
      })
      .map((r) => r.responseTimeMs)
    return {
      type,
      ...calculateStats(typeTimes),
    }
  })

  // Filter to only part types with data
  const activePartTypes = partTypeStats.filter((s) => s.count > 0)

  // Calculate threshold for SpeedMeter (mean + 2 stddev, clamped)
  const threshold = hasEnoughData
    ? Math.max(30_000, Math.min(overallStats.mean + 2 * overallStats.stdDev, 5 * 60 * 1000))
    : 60_000 // Default 1 minute if not enough data

  return (
    <div
      data-component="practice-timing-display"
      className={css({
        display: 'flex',
        flexDirection: 'column',
        gap: '0.75rem',
        padding: '0.75rem',
        backgroundColor: isDark ? 'gray.800' : 'white',
        borderRadius: '12px',
        border: '1px solid',
        borderColor: isDark ? 'gray.700' : 'gray.200',
      })}
    >
      {/* Current problem timer - prominent display */}
      <div
        data-element="current-timer"
        className={css({
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'space-between',
          padding: '0.5rem 0.75rem',
          backgroundColor: isDark ? 'gray.700' : 'gray.100',
          borderRadius: '8px',
        })}
      >
        <div
          className={css({
            display: 'flex',
            alignItems: 'center',
            gap: '0.5rem',
          })}
        >
          <span className={css({ fontSize: '1rem' })}>{getPartTypeEmoji(currentPartType)}</span>
          <span
            className={css({
              fontSize: '0.75rem',
              fontWeight: '600',
              color: isDark ? 'gray.300' : 'gray.600',
              textTransform: 'uppercase',
            })}
          >
            This problem
          </span>
        </div>
        <span
          className={css({
            fontFamily: 'monospace',
            fontSize: '1.25rem',
            fontWeight: 'bold',
            color:
              currentElapsedMs > threshold
                ? isDark
                  ? 'red.400'
                  : 'red.500'
                : currentElapsedMs > overallStats.mean + overallStats.stdDev
                  ? isDark
                    ? 'yellow.400'
                    : 'yellow.600'
                  : isDark
                    ? 'green.400'
                    : 'green.600',
          })}
        >
          {formatTime(currentElapsedMs)}
        </span>
      </div>

      {/* Speed meter - shows where current time falls relative to average */}
      {hasEnoughData && (
        <div data-element="speed-visualization">
          <SpeedMeter
            meanMs={overallStats.mean}
            stdDevMs={overallStats.stdDev}
            thresholdMs={threshold}
            currentTimeMs={currentElapsedMs}
            isDark={isDark}
            compact={true}
          />
        </div>
      )}

      {/* Per-part-type breakdown */}
      {activePartTypes.length > 1 && (
        <div
          data-element="part-type-breakdown"
          className={css({
            display: 'flex',
            gap: '0.5rem',
            flexWrap: 'wrap',
          })}
        >
          {activePartTypes.map((stats) => (
            <div
              key={stats.type}
              data-part-type={stats.type}
              className={css({
                display: 'flex',
                alignItems: 'center',
                gap: '0.375rem',
                padding: '0.25rem 0.5rem',
                backgroundColor:
                  stats.type === currentPartType
                    ? isDark
                      ? 'blue.900'
                      : 'blue.100'
                    : isDark
                      ? 'gray.700'
                      : 'gray.100',
                borderRadius: '6px',
                border: stats.type === currentPartType ? '1px solid' : 'none',
                borderColor: isDark ? 'blue.700' : 'blue.300',
              })}
            >
              <span className={css({ fontSize: '0.875rem' })}>{getPartTypeEmoji(stats.type)}</span>
              <span
                className={css({
                  fontSize: '0.6875rem',
                  color: isDark ? 'gray.400' : 'gray.500',
                })}
              >
                {getPartTypeLabel(stats.type)}
              </span>
              <span
                className={css({
                  fontFamily: 'monospace',
                  fontSize: '0.75rem',
                  fontWeight: 'bold',
                  color: isDark ? 'gray.200' : 'gray.700',
                })}
              >
                {formatSecondsDecimal(stats.mean)}
              </span>
              <span
                className={css({
                  fontSize: '0.625rem',
                  color: isDark ? 'gray.500' : 'gray.400',
                })}
              >
                ({stats.count})
              </span>
            </div>
          ))}
        </div>
      )}

      {/* Simple average display when not enough data for full visualization */}
      {!hasEnoughData && overallStats.count > 0 && (
        <div
          data-element="simple-average"
          className={css({
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            gap: '0.5rem',
            fontSize: '0.75rem',
            color: isDark ? 'gray.400' : 'gray.500',
          })}
        >
          <span>Avg so far:</span>
          <span
            className={css({
              fontFamily: 'monospace',
              fontWeight: 'bold',
              color: isDark ? 'gray.200' : 'gray.700',
            })}
          >
            {formatSecondsDecimal(overallStats.mean)}
          </span>
          <span>({overallStats.count} problems)</span>
        </div>
      )}
    </div>
  )
}