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

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

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

/**
 * RetryTransitionScreen - Brief transition between epochs when retrying wrong problems
 *
 * Shows a kid-friendly message encouraging them to try the problems again,
 * with info about how many problems need retrying and which attempt this is.
 */

import { useCallback, useEffect, useState } from 'react'
import { useTheme } from '@/contexts/ThemeContext'
import { css } from '../../../styled-system/css'

// ============================================================================
// Constants
// ============================================================================

/** Countdown duration for retry transition */
export const RETRY_TRANSITION_COUNTDOWN_MS = 3000

// ============================================================================
// Types
// ============================================================================

export interface RetryTransitionScreenProps {
  /** Whether the transition screen is visible */
  isVisible: boolean
  /** Which retry epoch we're starting (1 = first retry, 2 = second retry) */
  epochNumber: number
  /** Number of problems that need retrying */
  problemCount: number
  /** Student info for display */
  student: {
    name: string
    emoji: string
  }
  /** Called when transition completes (countdown or skip) */
  onComplete: () => void
}

// ============================================================================
// Component
// ============================================================================

export function RetryTransitionScreen({
  isVisible,
  epochNumber,
  problemCount,
  student,
  onComplete,
}: RetryTransitionScreenProps) {
  const { resolvedTheme } = useTheme()
  const isDark = resolvedTheme === 'dark'

  const [countdown, setCountdown] = useState(3)

  // Reset countdown when screen becomes visible
  useEffect(() => {
    if (isVisible) {
      setCountdown(3)
    }
  }, [isVisible])

  // Countdown timer
  useEffect(() => {
    if (!isVisible) return

    const interval = setInterval(() => {
      setCountdown((prev) => {
        if (prev <= 1) {
          onComplete()
          return 0
        }
        return prev - 1
      })
    }, 1000)

    return () => clearInterval(interval)
  }, [isVisible, onComplete])

  const handleSkip = useCallback(() => {
    onComplete()
  }, [onComplete])

  if (!isVisible) return null

  // Get encouraging message based on epoch
  const getMessage = () => {
    if (epochNumber === 1) {
      return "Let's practice those again!"
    }
    return 'One more try!'
  }

  const getSubMessage = () => {
    const plural = problemCount === 1 ? 'problem' : 'problems'
    if (epochNumber === 1) {
      return `You have ${problemCount} ${plural} to practice again.`
    }
    return `${problemCount} ${plural} left. You can do it!`
  }

  return (
    <div
      data-component="retry-transition-screen"
      data-epoch={epochNumber}
      className={css({
        position: 'fixed',
        inset: 0,
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center',
        justifyContent: 'center',
        backgroundColor: isDark ? 'gray.900' : 'orange.50',
        zIndex: 50,
        padding: '2rem',
      })}
    >
      {/* Main content */}
      <div
        className={css({
          display: 'flex',
          flexDirection: 'column',
          alignItems: 'center',
          maxWidth: '400px',
          textAlign: 'center',
        })}
      >
        {/* Student avatar */}
        <div
          className={css({
            fontSize: '4rem',
            marginBottom: '1.5rem',
          })}
        >
          {student.emoji}
        </div>

        {/* Main message */}
        <h2
          className={css({
            fontSize: '2rem',
            fontWeight: 'bold',
            color: isDark ? 'orange.200' : 'orange.700',
            marginBottom: '1rem',
          })}
        >
          {getMessage()}
        </h2>

        {/* Sub message */}
        <p
          className={css({
            fontSize: '1.25rem',
            color: isDark ? 'gray.300' : 'gray.600',
            marginBottom: '2rem',
          })}
        >
          {getSubMessage()}
        </p>

        {/* Attempt indicator */}
        <div
          className={css({
            display: 'flex',
            alignItems: 'center',
            gap: '0.5rem',
            padding: '0.5rem 1rem',
            backgroundColor: isDark ? 'orange.900' : 'orange.100',
            borderRadius: '999px',
            marginBottom: '2rem',
          })}
        >
          <span
            className={css({
              fontSize: '0.875rem',
              fontWeight: 'bold',
              color: isDark ? 'orange.200' : 'orange.700',
            })}
          >
            Attempt {epochNumber + 1} of 3
          </span>
        </div>

        {/* Countdown / Skip */}
        <div
          className={css({
            display: 'flex',
            flexDirection: 'column',
            alignItems: 'center',
            gap: '1rem',
          })}
        >
          <div
            className={css({
              fontSize: '1.5rem',
              fontWeight: 'bold',
              color: isDark ? 'gray.400' : 'gray.500',
            })}
          >
            Starting in {countdown}...
          </div>

          <button
            type="button"
            onClick={handleSkip}
            className={css({
              padding: '0.75rem 2rem',
              fontSize: '1rem',
              fontWeight: 'bold',
              color: 'white',
              backgroundColor: isDark ? 'orange.600' : 'orange.500',
              borderRadius: '12px',
              border: 'none',
              cursor: 'pointer',
              transition: 'all 0.15s ease',
              _hover: {
                backgroundColor: isDark ? 'orange.500' : 'orange.600',
                transform: 'scale(1.05)',
              },
            })}
          >
            Let's Go!
          </button>
        </div>
      </div>
    </div>
  )
}