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

97.01% Statements 488/503
54.41% Branches 37/68
66.66% Functions 2/3
97.01% Lines 488/503

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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 5041x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 27x 27x 27x 27x 24x 24x 24x 24x 24x 27x 1x 1x 1x 1x 1x 27x 1x 1x 1x 1x 1x 27x 1x 1x 1x 1x 1x 27x 27x 1x 1x 1x 1x 1x 1x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 4x 2x 2x 2x 28x 28x 28x 28x 27x 1x 1x 1x 1x 26x 26x                               26x 26x 26x 26x 26x 26x 26x 26x 28x 28x 28x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 28x 28x 28x 28x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 26x 26x 26x 26x 26x 26x 26x 26x 27x 27x 28x 28x 28x 28x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 24x 24x 28x 28x 28x 28x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 23x 23x 28x 28x 28x 28x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 1x 1x  
'use client'
 
/**
 * GameBreakResultsScreen - Interstitial screen showing game results after a game break
 *
 * Displays the game results before returning to practice, giving kids a moment
 * to see how they did in the game. Uses a similar pattern to PartTransitionScreen
 * with a countdown timer and skip button.
 */
 
import { useCallback, useEffect, useRef, useState } from 'react'
import { useTheme } from '@/contexts/ThemeContext'
import type { GameResultsReport } from '@/lib/arcade/game-sdk/types'
import { css } from '../../../styled-system/css'
 
// ============================================================================
// Constants
// ============================================================================
 
/** Default countdown duration in milliseconds */
export const RESULTS_COUNTDOWN_MS = 5000
 
// ============================================================================
// Types
// ============================================================================
 
export interface GameBreakResultsScreenProps {
  /** Whether the results screen is visible */
  isVisible: boolean
  /** The game results to display */
  results: GameResultsReport
  /** Student info for display */
  student: {
    name: string
    emoji: string
  }
  /** Optional countdown duration in ms (default 5000) */
  countdownMs?: number
  /** Called when the results screen completes (countdown or skip) */
  onComplete: () => void
}
 
// ============================================================================
// Helper Functions
// ============================================================================
 
/**
 * Get background color based on result theme
 */
function getThemeColors(
  theme: GameResultsReport['resultTheme'],
  isDark: boolean
): { bg: string; border: string; text: string } {
  switch (theme) {
    case 'success':
      return {
        bg: isDark ? 'rgba(34, 197, 94, 0.15)' : 'rgba(34, 197, 94, 0.1)',
        border: isDark ? 'rgba(34, 197, 94, 0.4)' : 'rgba(34, 197, 94, 0.5)',
        text: isDark ? '#86efac' : '#16a34a',
      }
    case 'good':
      return {
        bg: isDark ? 'rgba(59, 130, 246, 0.15)' : 'rgba(59, 130, 246, 0.1)',
        border: isDark ? 'rgba(59, 130, 246, 0.4)' : 'rgba(59, 130, 246, 0.5)',
        text: isDark ? '#93c5fd' : '#2563eb',
      }
    case 'needs-practice':
      return {
        bg: isDark ? 'rgba(234, 179, 8, 0.15)' : 'rgba(234, 179, 8, 0.1)',
        border: isDark ? 'rgba(234, 179, 8, 0.4)' : 'rgba(234, 179, 8, 0.5)',
        text: isDark ? '#fde047' : '#ca8a04',
      }
    default:
      return {
        bg: isDark ? 'rgba(156, 163, 175, 0.15)' : 'rgba(156, 163, 175, 0.1)',
        border: isDark ? 'rgba(156, 163, 175, 0.3)' : 'rgba(156, 163, 175, 0.4)',
        text: isDark ? '#d1d5db' : '#4b5563',
      }
  }
}
 
// ============================================================================
// Component
// ============================================================================
 
export function GameBreakResultsScreen({
  isVisible,
  results,
  student,
  countdownMs = RESULTS_COUNTDOWN_MS,
  onComplete,
}: GameBreakResultsScreenProps) {
  const { resolvedTheme } = useTheme()
  const isDark = resolvedTheme === 'dark'
 
  // Track countdown state
  const [countdownStartTime] = useState(() => Date.now())
  const [elapsedMs, setElapsedMs] = useState(0)
  const animationFrameRef = useRef<number | null>(null)
  const hasCompletedRef = useRef(false)
 
  // Handle skip
  const handleSkip = useCallback(() => {
    if (!hasCompletedRef.current) {
      hasCompletedRef.current = true
      onComplete()
    }
  }, [onComplete])
 
  // Countdown timer using requestAnimationFrame for smooth updates
  useEffect(() => {
    if (!isVisible) {
      hasCompletedRef.current = false
      setElapsedMs(0)
      return
    }
 
    const updateCountdown = () => {
      const now = Date.now()
      const elapsed = now - countdownStartTime
      setElapsedMs(elapsed)

      if (elapsed >= countdownMs) {
        // Countdown complete
        if (!hasCompletedRef.current) {
          hasCompletedRef.current = true
          onComplete()
        }
      } else {
        // Continue updating
        animationFrameRef.current = requestAnimationFrame(updateCountdown)
      }
    }
 
    animationFrameRef.current = requestAnimationFrame(updateCountdown)
 
    return () => {
      if (animationFrameRef.current) {
        cancelAnimationFrame(animationFrameRef.current)
      }
    }
  }, [isVisible, countdownStartTime, countdownMs, onComplete])
 
  if (!isVisible) return null
 
  // Calculate countdown values
  const remainingMs = Math.max(0, countdownMs - elapsedMs)
  const remainingSeconds = Math.ceil(remainingMs / 1000)
  const percentRemaining = (remainingMs / countdownMs) * 100
 
  // SVG parameters for countdown circle
  const viewBoxSize = 100
  const center = viewBoxSize / 2
  const radius = 42
  const circumference = 2 * Math.PI * radius
  const strokeDashoffset = circumference * (1 - percentRemaining / 100)
 
  // Color based on time remaining
  const countdownColor =
    percentRemaining > 50 ? (isDark ? '#22c55e' : '#16a34a') : isDark ? '#eab308' : '#ca8a04'
 
  // Theme colors for the header
  const themeColors = getThemeColors(results.resultTheme, isDark)
 
  // Filter highlighted stats
  const highlightedStats = results.customStats?.filter((s) => s.highlight) ?? []
  const regularStats = results.customStats?.filter((s) => !s.highlight) ?? []
 
  return (
    <div
      data-component="game-break-results-screen"
      data-game={results.gameName}
      data-theme={results.resultTheme}
      className={css({
        position: 'fixed',
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
        backgroundColor: isDark ? 'rgba(0, 0, 0, 0.95)' : 'rgba(0, 0, 0, 0.85)',
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center',
        justifyContent: 'center',
        zIndex: 1000,
        padding: '1.5rem',
      })}
    >
      {/* Main content card */}
      <div
        data-element="results-content"
        className={css({
          display: 'flex',
          flexDirection: 'column',
          alignItems: 'center',
          gap: '1.25rem',
          padding: '1.5rem 2rem',
          maxWidth: '420px',
          width: '100%',
          backgroundColor: isDark ? 'gray.800' : 'white',
          borderRadius: '24px',
          boxShadow: '0 25px 50px -12px rgba(0, 0, 0, 0.4)',
        })}
      >
        {/* Game icon and headline */}
        <div
          data-element="header"
          className={css({
            display: 'flex',
            flexDirection: 'column',
            alignItems: 'center',
            gap: '0.75rem',
            width: '100%',
            padding: '1rem',
            borderRadius: '16px',
          })}
          style={{
            backgroundColor: themeColors.bg,
            border: `2px solid ${themeColors.border}`,
          }}
        >
          {/* Game icon */}
          <span
            className={css({
              fontSize: '3rem',
            })}
          >
            {results.gameIcon}
          </span>
 
          {/* Headline */}
          <div className={css({ textAlign: 'center' })}>
            <h2
              className={css({
                fontSize: '1.5rem',
                fontWeight: 'bold',
                marginBottom: '0.25rem',
              })}
              style={{ color: themeColors.text }}
            >
              {results.headline ?? 'Game Complete!'}
            </h2>
            {results.subheadline && (
              <p
                className={css({
                  fontSize: '0.9375rem',
                  color: isDark ? 'gray.400' : 'gray.600',
                })}
              >
                {results.subheadline}
              </p>
            )}
          </div>
        </div>
 
        {/* Player info (single player or winner) */}
        {results.playerResults.length === 1 && (
          <div
            data-element="player-result"
            className={css({
              display: 'flex',
              alignItems: 'center',
              gap: '0.5rem',
              fontSize: '1rem',
              color: isDark ? 'gray.300' : 'gray.600',
            })}
          >
            <span>{results.playerResults[0].playerEmoji}</span>
            <span>{results.playerResults[0].playerName}</span>
            {results.playerResults[0].score !== undefined && (
              <span
                className={css({
                  fontWeight: '600',
                  color: isDark ? 'gray.200' : 'gray.700',
                })}
              >
                - {results.playerResults[0].score} points
              </span>
            )}
          </div>
        )}
 
        {/* Highlighted stats */}
        {highlightedStats.length > 0 && (
          <div
            data-element="highlighted-stats"
            className={css({
              display: 'flex',
              flexWrap: 'wrap',
              gap: '0.75rem',
              justifyContent: 'center',
              width: '100%',
            })}
          >
            {highlightedStats.map((stat, idx) => (
              <div
                key={idx}
                className={css({
                  display: 'flex',
                  alignItems: 'center',
                  gap: '0.5rem',
                  padding: '0.5rem 0.875rem',
                  backgroundColor: isDark ? 'gray.700' : 'gray.100',
                  borderRadius: '10px',
                  fontSize: '1rem',
                })}
              >
                {stat.icon && <span>{stat.icon}</span>}
                <span
                  className={css({
                    color: isDark ? 'gray.400' : 'gray.500',
                    fontSize: '0.875rem',
                  })}
                >
                  {stat.label}:
                </span>
                <span
                  className={css({
                    fontWeight: '600',
                    color: isDark ? 'gray.100' : 'gray.800',
                  })}
                >
                  {stat.value}
                </span>
              </div>
            ))}
          </div>
        )}
 
        {/* Regular stats grid */}
        {regularStats.length > 0 && (
          <div
            data-element="stats-grid"
            className={css({
              display: 'grid',
              gridTemplateColumns: 'repeat(2, 1fr)',
              gap: '0.5rem 1rem',
              width: '100%',
              padding: '0.5rem',
            })}
          >
            {regularStats.map((stat, idx) => (
              <div
                key={idx}
                className={css({
                  display: 'flex',
                  alignItems: 'center',
                  gap: '0.375rem',
                  fontSize: '0.875rem',
                })}
              >
                {stat.icon && (
                  <span className={css({ fontSize: '0.875rem', opacity: 0.8 })}>{stat.icon}</span>
                )}
                <span
                  className={css({
                    color: isDark ? 'gray.400' : 'gray.500',
                  })}
                >
                  {stat.label}:
                </span>
                <span
                  className={css({
                    fontWeight: '500',
                    color: isDark ? 'gray.200' : 'gray.700',
                  })}
                >
                  {stat.value}
                </span>
              </div>
            ))}
          </div>
        )}
 
        {/* Completion info */}
        {results.itemsCompleted !== undefined && results.itemsTotal !== undefined && (
          <div
            data-element="completion"
            className={css({
              display: 'flex',
              alignItems: 'center',
              gap: '0.5rem',
              fontSize: '0.9375rem',
              color: isDark ? 'gray.300' : 'gray.600',
            })}
          >
            <span>Completed:</span>
            <span
              className={css({
                fontWeight: '600',
                color: isDark ? 'gray.100' : 'gray.800',
              })}
            >
              {results.itemsCompleted}/{results.itemsTotal}
            </span>
            {results.completionPercent !== undefined && (
              <span className={css({ color: isDark ? 'gray.400' : 'gray.500' })}>
                ({results.completionPercent}%)
              </span>
            )}
          </div>
        )}
 
        {/* Countdown timer */}
        <div
          data-element="countdown"
          className={css({
            display: 'flex',
            flexDirection: 'column',
            alignItems: 'center',
            gap: '0.75rem',
            marginTop: '0.5rem',
          })}
        >
          {/* Circular countdown */}
          <div
            className={css({
              width: '70px',
              height: '70px',
              position: 'relative',
            })}
          >
            <svg
              viewBox={`0 0 ${viewBoxSize} ${viewBoxSize}`}
              className={css({
                width: '100%',
                height: '100%',
                transform: 'rotate(-90deg)',
              })}
            >
              {/* Background circle */}
              <circle
                cx={center}
                cy={center}
                r={radius}
                fill="none"
                stroke={isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)'}
                strokeWidth="8"
              />
              {/* Progress arc */}
              <circle
                cx={center}
                cy={center}
                r={radius}
                fill="none"
                stroke={countdownColor}
                strokeWidth="8"
                strokeLinecap="round"
                strokeDasharray={circumference}
                strokeDashoffset={strokeDashoffset}
                className={css({
                  transition: 'stroke-dashoffset 0.1s linear, stroke 0.3s ease',
                })}
              />
            </svg>
            {/* Seconds in center */}
            <div
              className={css({
                position: 'absolute',
                top: '50%',
                left: '50%',
                transform: 'translate(-50%, -50%)',
                fontSize: '1.25rem',
                fontWeight: 'bold',
                fontFamily: 'var(--font-mono, monospace)',
                color: countdownColor,
              })}
            >
              {remainingSeconds}
            </div>
          </div>
 
          {/* Continue button */}
          <button
            data-action="continue-to-practice"
            onClick={handleSkip}
            className={css({
              padding: '0.75rem 1.5rem',
              fontSize: '1rem',
              fontWeight: '600',
              color: isDark ? 'gray.300' : 'gray.600',
              backgroundColor: isDark ? 'gray.700' : 'gray.200',
              border: 'none',
              borderRadius: '8px',
              cursor: 'pointer',
              transition: 'all 0.15s ease',
              _hover: {
                backgroundColor: isDark ? 'gray.600' : 'gray.300',
              },
              _active: {
                transform: 'scale(0.98)',
              },
            })}
          >
            Back to Practice
          </button>
        </div>
      </div>
    </div>
  )
}
 
export default GameBreakResultsScreen