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

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

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 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
/**
 * CelebrationProgressionBanner
 *
 * A special version of ProgressionBanner that displays a celebration when
 * a student unlocks a new skill, then imperceptibly morphs into the normal
 * banner over ~60 seconds.
 *
 * This component interpolates 35+ CSS properties individually for a smooth,
 * magical transition that the student won't notice happening.
 */

'use client'

import { useEffect, useRef } from 'react'
import type { ProgressionMode } from '@/lib/curriculum/session-mode'
import {
  boxShadow,
  boxShadowsToCss,
  gradientStop,
  gradientToCss,
  lerp,
  lerpBoxShadows,
  lerpColor,
  lerpGradientStops,
  lerpRgbaString,
  type BoxShadow,
  type GradientStop,
  type RGBA,
} from '@/utils/interpolate'
import { fireConfettiCelebration } from '@/utils/confetti'
import { useCelebrationWindDown } from '@/hooks/useCelebrationWindDown'

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

interface CelebrationProgressionBannerProps {
  mode: ProgressionMode
  onAction: () => void
  isLoading: boolean
  variant: 'dashboard' | 'modal'
  isDark: boolean
  /** Speed multiplier for testing (1 = normal, 10 = 10x faster) */
  speedMultiplier?: number
  /** Force a specific progress value (0-1) for Storybook */
  forceProgress?: number
  /** Disable confetti for Storybook (to avoid spam) */
  disableConfetti?: boolean
}

// =============================================================================
// Style Definitions - Start (Celebration) and End (Normal) States
// =============================================================================

// Helper to create RGBA
const rgba = (r: number, g: number, b: number, a: number): RGBA => ({
  r,
  g,
  b,
  a,
})

// --- Container Background Gradients ---
const CELEBRATION_BG_LIGHT: GradientStop[] = [
  gradientStop(234, 179, 8, 0.2, 0), // yellow.500 @ 20%
  gradientStop(251, 191, 36, 0.1, 50), // yellow.400 @ 10%
  gradientStop(234, 179, 8, 0.2, 100),
]
const CELEBRATION_BG_DARK: GradientStop[] = [
  gradientStop(234, 179, 8, 0.25, 0),
  gradientStop(251, 191, 36, 0.15, 50),
  gradientStop(234, 179, 8, 0.25, 100),
]

const NORMAL_BG_LIGHT: GradientStop[] = [
  gradientStop(34, 197, 94, 0.06, 0), // green.500 @ 6%
  gradientStop(59, 130, 246, 0.04, 100), // blue.500 @ 4%
]
const NORMAL_BG_DARK: GradientStop[] = [
  gradientStop(34, 197, 94, 0.12, 0),
  gradientStop(59, 130, 246, 0.08, 100),
]

// Pad to same length (3 stops each)
const NORMAL_BG_LIGHT_PADDED: GradientStop[] = [
  ...NORMAL_BG_LIGHT.slice(0, 1),
  gradientStop(46, 163, 170, 0.05, 50), // midpoint interpolation
  ...NORMAL_BG_LIGHT.slice(1),
]
const NORMAL_BG_DARK_PADDED: GradientStop[] = [
  ...NORMAL_BG_DARK.slice(0, 1),
  gradientStop(46, 163, 170, 0.1, 50),
  ...NORMAL_BG_DARK.slice(1),
]

// --- Container Box Shadows ---
const CELEBRATION_SHADOWS: BoxShadow[] = [
  boxShadow(0, 0, 20, 0, 234, 179, 8, 0.4), // glow 1
  boxShadow(0, 0, 40, 0, 234, 179, 8, 0.2), // glow 2
]
const NORMAL_SHADOWS: BoxShadow[] = [
  boxShadow(0, 2, 8, 0, 0, 0, 0, 0.1), // subtle shadow
  boxShadow(0, 0, 0, 0, 0, 0, 0, 0), // transparent (padding)
]

// --- Button Background Gradients ---
const CELEBRATION_BTN_LIGHT: GradientStop[] = [
  gradientStop(252, 211, 77, 1, 0), // yellow.300
  gradientStop(245, 158, 11, 1, 100), // yellow.500
]
const CELEBRATION_BTN_DARK: GradientStop[] = [
  gradientStop(252, 211, 77, 1, 0),
  gradientStop(245, 158, 11, 1, 100),
]

const NORMAL_BTN: GradientStop[] = [
  gradientStop(34, 197, 94, 1, 0), // green.500
  gradientStop(22, 163, 74, 1, 100), // green.600
]

// --- Button Box Shadows ---
const CELEBRATION_BTN_SHADOW: BoxShadow[] = [boxShadow(0, 4, 15, 0, 245, 158, 11, 0.4)]
const NORMAL_BTN_SHADOW: BoxShadow[] = [boxShadow(0, 2, 4, 0, 0, 0, 0, 0.1)]

// --- Colors ---
const COLORS = {
  // Border
  celebrationBorder: '#eab308', // yellow.500
  normalBorderLight: '#4ade80', // green.400
  normalBorderDark: '#22c55e', // green.500

  // Title
  celebrationTitleLight: '#a16207', // yellow.700
  celebrationTitleDark: '#fef08a', // yellow.200
  normalTitleLight: '#166534', // green.800
  normalTitleDark: '#86efac', // green.300

  // Subtitle
  celebrationSubtitleLight: '#374151', // gray.700
  celebrationSubtitleDark: '#e5e7eb', // gray.200
  normalSubtitleLight: '#6b7280', // gray.500
  normalSubtitleDark: '#a1a1aa', // gray.400

  // Button text
  celebrationBtnTextLight: '#111827', // gray.900 (dark text on yellow)
  celebrationBtnTextDark: '#111827',
  normalBtnText: '#ffffff', // white
}

// =============================================================================
// Style Calculation
// =============================================================================

interface InterpolatedStyles {
  // Container
  containerBackground: string
  containerBorderWidth: number
  containerBorderColor: string
  containerBorderRadius: number
  containerPadding: number
  containerBoxShadow: string

  // Emoji
  emojiSize: number
  emojiRotation: number

  // Title
  titleFontSize: number
  titleColor: string
  titleTextShadow: string

  // Subtitle
  subtitleFontSize: number
  subtitleColor: string
  subtitleMarginTop: number

  // Button
  buttonPaddingY: number
  buttonPaddingX: number
  buttonFontSize: number
  buttonBackground: string
  buttonBorderRadius: number
  buttonBoxShadow: string
  buttonColor: string

  // Shimmer
  shimmerOpacity: number

  // Layout - all interpolated, no discrete jumps
  contentGap: number
  contentJustify: number // 0 = center, 1 = flex-start (use transform)
  textMarginLeft: number
  buttonWrapperPaddingX: number
  buttonWrapperPaddingBottom: number
}

function calculateStyles(
  progress: number,
  oscillation: number,
  isDark: boolean,
  variant: 'dashboard' | 'modal'
): InterpolatedStyles {
  const t = progress // 0 = celebration, 1 = normal

  // Get theme-appropriate values
  const celebrationBg = isDark ? CELEBRATION_BG_DARK : CELEBRATION_BG_LIGHT
  const normalBg = isDark ? NORMAL_BG_DARK_PADDED : NORMAL_BG_LIGHT_PADDED
  const celebrationBtn = isDark ? CELEBRATION_BTN_DARK : CELEBRATION_BTN_LIGHT
  const normalBorder = isDark ? COLORS.normalBorderDark : COLORS.normalBorderLight
  const celebrationTitle = isDark ? COLORS.celebrationTitleDark : COLORS.celebrationTitleLight
  const normalTitle = isDark ? COLORS.normalTitleDark : COLORS.normalTitleLight
  const celebrationSubtitle = isDark
    ? COLORS.celebrationSubtitleDark
    : COLORS.celebrationSubtitleLight
  const normalSubtitle = isDark ? COLORS.normalSubtitleDark : COLORS.normalSubtitleLight
  const celebrationBtnText = isDark ? COLORS.celebrationBtnTextDark : COLORS.celebrationBtnTextLight

  // Variant-specific sizes
  const isModal = variant === 'modal'
  const celebrationPadding = isModal ? 20 : 24
  const normalPadding = isModal ? 14 : 16
  const celebrationTitleSize = isModal ? 24 : 28
  const normalTitleSize = isModal ? 15 : 16
  const celebrationSubtitleSize = isModal ? 16 : 18
  const normalSubtitleSize = isModal ? 12 : 13
  const celebrationBtnPaddingY = isModal ? 14 : 16
  const normalBtnPaddingY = isModal ? 14 : 16
  const celebrationBtnPaddingX = isModal ? 28 : 32
  const normalBtnPaddingX = 0 // full width in normal mode
  const celebrationBtnFontSize = isModal ? 16 : 18
  const normalBtnFontSize = isModal ? 16 : 17

  // Wiggle amplitude decreases as we transition
  const wiggleAmplitude = 3 * (1 - t)
  const rotation = oscillation * wiggleAmplitude

  return {
    // Container
    containerBackground: gradientToCss(135, lerpGradientStops(celebrationBg, normalBg, t)),
    containerBorderWidth: lerp(3, 2, t),
    containerBorderColor: lerpColor(COLORS.celebrationBorder, normalBorder, t),
    containerBorderRadius: lerp(16, isModal ? 12 : 16, t),
    containerPadding: lerp(celebrationPadding, normalPadding, t),
    containerBoxShadow: boxShadowsToCss(lerpBoxShadows(CELEBRATION_SHADOWS, NORMAL_SHADOWS, t)),

    // Emoji - single emoji, just size and wiggle
    emojiSize: lerp(isModal ? 48 : 64, isModal ? 24 : 32, t),
    emojiRotation: rotation,

    // Title
    titleFontSize: lerp(celebrationTitleSize, normalTitleSize, t),
    titleColor: lerpColor(celebrationTitle, normalTitle, t),
    titleTextShadow: `0 0 ${lerp(20, 0, t)}px ${lerpRgbaString(rgba(234, 179, 8, 0.5), rgba(0, 0, 0, 0), t)}`,

    // Subtitle
    subtitleFontSize: lerp(celebrationSubtitleSize, normalSubtitleSize, t),
    subtitleColor: lerpColor(celebrationSubtitle, normalSubtitle, t),
    subtitleMarginTop: lerp(8, 2, t),

    // Button - always full width, wrapper controls visual width
    buttonPaddingY: lerp(celebrationBtnPaddingY, normalBtnPaddingY, t),
    buttonPaddingX: 0, // No horizontal padding on button itself
    buttonFontSize: lerp(celebrationBtnFontSize, normalBtnFontSize, t),
    buttonBackground: gradientToCss(135, lerpGradientStops(celebrationBtn, NORMAL_BTN, t)),
    buttonBorderRadius: lerp(12, 0, t),
    buttonBoxShadow: boxShadowsToCss(lerpBoxShadows(CELEBRATION_BTN_SHADOW, NORMAL_BTN_SHADOW, t)),
    buttonColor: lerpColor(celebrationBtnText, COLORS.normalBtnText, t),

    // Shimmer fades out
    shimmerOpacity: 1 - t,

    // Layout - all smoothly interpolated
    contentGap: lerp(4, 12, t), // Gap between emoji and text
    contentJustify: t, // 0 = centered layout, 1 = left-aligned
    textMarginLeft: lerp(0, 0, t), // Could add margin if needed
    buttonWrapperPaddingX: lerp(isModal ? 60 : 80, 0, t), // Shrinks button visually in celebration
    buttonWrapperPaddingBottom: lerp(celebrationPadding, 0, t),
  }
}

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

export function CelebrationProgressionBanner({
  mode,
  onAction,
  isLoading,
  variant,
  isDark,
  speedMultiplier = 1,
  forceProgress,
  disableConfetti = false,
}: CelebrationProgressionBannerProps) {
  const confettiFiredRef = useRef(false)

  const { progress, shouldFireConfetti, oscillation, onConfettiFired, isCelebrating } =
    useCelebrationWindDown({
      skillId: mode.nextSkill.skillId,
      tutorialRequired: mode.tutorialRequired,
      enabled: true,
      speedMultiplier,
      forceProgress,
    })

  // Fire confetti once (unless disabled for Storybook)
  useEffect(() => {
    if (shouldFireConfetti && !confettiFiredRef.current && !disableConfetti) {
      confettiFiredRef.current = true
      fireConfettiCelebration()
      onConfettiFired()
    }
  }, [shouldFireConfetti, onConfettiFired, disableConfetti])

  // If not celebrating at all, render the normal banner
  if (!isCelebrating && progress >= 1) {
    return (
      <NormalProgressionBanner
        mode={mode}
        onAction={onAction}
        isLoading={isLoading}
        variant={variant}
        isDark={isDark}
      />
    )
  }

  // Calculate all interpolated styles
  const styles = calculateStyles(progress, oscillation, isDark, variant)

  return (
    <div
      data-element="session-mode-banner"
      data-mode="progression"
      data-variant={variant}
      data-celebrating={isCelebrating}
      data-progress={progress.toFixed(3)}
      style={{
        position: 'relative',
        background: styles.containerBackground,
        borderWidth: `${styles.containerBorderWidth}px`,
        borderStyle: 'solid',
        borderColor: styles.containerBorderColor,
        borderRadius: `${styles.containerBorderRadius}px`,
        boxShadow: styles.containerBoxShadow,
        overflow: 'hidden',
      }}
    >
      {/* Shimmer overlay - fades out */}
      <div
        style={{
          position: 'absolute',
          inset: 0,
          background:
            'linear-gradient(90deg, transparent 0%, rgba(255,255,255,0.3) 50%, transparent 100%)',
          backgroundSize: '200% 100%',
          animation: 'celebrationShimmer 2s linear infinite',
          opacity: styles.shimmerOpacity,
          pointerEvents: 'none',
        }}
      />

      {/* Content area - always row layout, centering achieved via flexbox */}
      <div
        style={{
          padding: `${styles.containerPadding}px`,
          display: 'flex',
          flexDirection: 'row',
          alignItems: 'center',
          justifyContent: 'flex-start',
          gap: `${styles.contentGap}px`,
        }}
      >
        {/* Emoji - single emoji with size and wiggle animation */}
        <span
          style={{
            fontSize: `${styles.emojiSize}px`,
            lineHeight: 1,
            flexShrink: 0,
            display: 'inline-block',
            transform: `rotate(${styles.emojiRotation}deg)`,
          }}
        >
          🌟
        </span>

        {/* Text content */}
        <div style={{ flex: 1, minWidth: 0 }}>
          {/* Title - same text throughout, only styling changes */}
          <div
            style={{
              fontSize: `${styles.titleFontSize}px`,
              fontWeight: 'bold',
              color: styles.titleColor,
              textShadow: styles.titleTextShadow,
            }}
          >
            New Skill Unlocked: <strong>{mode.nextSkill.displayName}</strong>
          </div>

          {/* Subtitle - same text throughout */}
          <div
            style={{
              fontSize: `${styles.subtitleFontSize}px`,
              color: styles.subtitleColor,
              marginTop: `${styles.subtitleMarginTop}px`,
            }}
          >
            Ready to start the tutorial
          </div>
        </div>
      </div>

      {/* Button wrapper - padding interpolates to control visual button width */}
      <div
        style={{
          padding: `0 ${styles.buttonWrapperPaddingX}px ${styles.buttonWrapperPaddingBottom}px`,
        }}
      >
        <button
          type="button"
          data-action="start-progression"
          onClick={onAction}
          disabled={isLoading}
          style={{
            width: '100%',
            padding: `${styles.buttonPaddingY}px 16px`,
            fontSize: `${styles.buttonFontSize}px`,
            fontWeight: 'bold',
            background: styles.buttonBackground,
            color: styles.buttonColor,
            borderRadius: `${styles.buttonBorderRadius}px`,
            border: 'none',
            boxShadow: styles.buttonBoxShadow,
            cursor: isLoading ? 'not-allowed' : 'pointer',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            gap: '8px',
          }}
        >
          {isLoading ? 'Starting...' : 'Begin Tutorial →'}
        </button>
      </div>

      {/* Inject keyframes for shimmer animation */}
      <style>
        {`
          @keyframes celebrationShimmer {
            0% { background-position: -200% 0; }
            100% { background-position: 200% 0; }
          }
        `}
      </style>
    </div>
  )
}

// =============================================================================
// Normal Progression Banner (fallback when celebration is complete)
// =============================================================================

function NormalProgressionBanner({
  mode,
  onAction,
  isLoading,
  variant,
  isDark,
}: Omit<
  CelebrationProgressionBannerProps,
  'speedMultiplier' | 'forceProgress' | 'disableConfetti'
>) {
  return (
    <div
      data-element="session-mode-banner"
      data-mode="progression"
      data-variant={variant}
      style={{
        borderRadius: variant === 'modal' ? '12px' : '16px',
        overflow: 'hidden',
        border: '2px solid',
        borderColor: isDark ? '#22c55e' : '#4ade80',
        background: isDark
          ? 'linear-gradient(135deg, rgba(34, 197, 94, 0.12) 0%, rgba(59, 130, 246, 0.08) 100%)'
          : 'linear-gradient(135deg, rgba(34, 197, 94, 0.06) 0%, rgba(59, 130, 246, 0.04) 100%)',
      }}
    >
      {/* Info section */}
      <div
        style={{
          padding: variant === 'modal' ? '14px 16px' : '16px 20px',
          display: 'flex',
          gap: '12px',
          alignItems: 'center',
        }}
      >
        <span
          style={{
            fontSize: variant === 'modal' ? '24px' : '32px',
            lineHeight: 1,
          }}
        >
          🌟
        </span>
        <div style={{ flex: 1 }}>
          <p
            style={{
              fontSize: variant === 'modal' ? '15px' : '16px',
              fontWeight: 600,
              color: isDark ? '#86efac' : '#166534',
              margin: 0,
            }}
          >
            {mode.tutorialRequired ? 'New Skill Unlocked: ' : 'Ready to practice: '}
            <strong>{mode.nextSkill.displayName}</strong>
          </p>
          <p
            style={{
              fontSize: variant === 'modal' ? '12px' : '13px',
              marginTop: '2px',
              color: isDark ? '#a1a1aa' : '#6b7280',
              margin: 0,
            }}
          >
            {mode.tutorialRequired ? 'Ready to start the tutorial' : 'Continue building mastery'}
          </p>
        </div>
      </div>

      {/* Action button */}
      <button
        type="button"
        data-action="start-progression"
        onClick={onAction}
        disabled={isLoading}
        style={{
          width: '100%',
          padding: variant === 'modal' ? '14px' : '16px',
          fontSize: variant === 'modal' ? '16px' : '17px',
          fontWeight: 'bold',
          color: 'white',
          border: 'none',
          borderRadius: 0,
          cursor: isLoading ? 'not-allowed' : 'pointer',
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          gap: '8px',
          background: isLoading ? '#9ca3af' : 'linear-gradient(135deg, #22c55e 0%, #16a34a 100%)',
          boxShadow: isLoading ? 'none' : 'inset 0 1px 0 rgba(255,255,255,0.15)',
        }}
      >
        {isLoading ? 'Starting...' : mode.tutorialRequired ? 'Begin Tutorial →' : "Let's Go! →"}
      </button>
    </div>
  )
}