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

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

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

import { animated, type SpringValue, to } from '@react-spring/web'
import type { SessionMode } from '@/lib/curriculum/session-mode'
import { css } from '../../../styled-system/css'

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

interface MorphingBannerProps {
  /** The session mode to display */
  sessionMode: SessionMode
  /** Callback when user clicks the action button */
  onAction: () => void
  /** Whether an action is in progress */
  isLoading?: boolean
  /** Dark mode */
  isDark: boolean
  /** Animation progress: 0 = full banner, 1 = compact banner (SpringValue for smooth animation) */
  progress: SpringValue<number>
  /** Container width (SpringValue for smooth animation) */
  containerWidth: SpringValue<number>
  /** Container height (SpringValue for smooth animation) */
  containerHeight: SpringValue<number>
}

// ============================================================================
// Interpolation helper
// ============================================================================

function lerp(a: number, b: number, t: number): number {
  return a + (b - a) * t
}

// ============================================================================
// Color Schemes
// ============================================================================

type ColorScheme = {
  bg: string
  border: string
  text: string
  subtext: string
  buttonBg: string
  buttonText: string
}

function getColorScheme(mode: SessionMode['type'], isDark: boolean): ColorScheme {
  switch (mode) {
    case 'remediation':
      return {
        bg: isDark
          ? 'linear-gradient(135deg, rgba(245, 158, 11, 0.12) 0%, rgba(217, 119, 6, 0.08) 100%)'
          : 'linear-gradient(135deg, rgba(245, 158, 11, 0.08) 0%, rgba(217, 119, 6, 0.05) 100%)',
        border: isDark ? 'rgba(245, 158, 11, 0.4)' : 'rgba(217, 119, 6, 0.3)',
        text: isDark ? '#fcd34d' : '#b45309',
        subtext: isDark ? '#d4d4d4' : '#525252',
        buttonBg: isDark
          ? 'linear-gradient(90deg, #f59e0b 0%, #d97706 100%)'
          : 'linear-gradient(90deg, #f59e0b 0%, #d97706 100%)',
        buttonText: 'white',
      }
    case 'progression':
      return {
        bg: isDark
          ? 'linear-gradient(135deg, rgba(34, 197, 94, 0.12) 0%, rgba(22, 163, 74, 0.08) 100%)'
          : 'linear-gradient(135deg, rgba(34, 197, 94, 0.08) 0%, rgba(22, 163, 74, 0.05) 100%)',
        border: isDark ? 'rgba(34, 197, 94, 0.4)' : 'rgba(34, 197, 94, 0.3)',
        text: isDark ? '#86efac' : '#166534',
        subtext: isDark ? '#d4d4d4' : '#525252',
        buttonBg: isDark
          ? 'linear-gradient(90deg, #22c55e 0%, #16a34a 100%)'
          : 'linear-gradient(90deg, #22c55e 0%, #16a34a 100%)',
        buttonText: 'white',
      }
    case 'maintenance':
      return {
        bg: isDark
          ? 'linear-gradient(135deg, rgba(59, 130, 246, 0.12) 0%, rgba(139, 92, 246, 0.08) 100%)'
          : 'linear-gradient(135deg, rgba(59, 130, 246, 0.08) 0%, rgba(139, 92, 246, 0.05) 100%)',
        border: isDark ? 'rgba(59, 130, 246, 0.4)' : 'rgba(59, 130, 246, 0.3)',
        text: isDark ? '#93c5fd' : '#1d4ed8',
        subtext: isDark ? '#d4d4d4' : '#525252',
        buttonBg: isDark
          ? 'linear-gradient(90deg, #3b82f6 0%, #2563eb 100%)'
          : 'linear-gradient(90deg, #3b82f6 0%, #2563eb 100%)',
        buttonText: 'white',
      }
  }
}

// ============================================================================
// Content Helpers
// ============================================================================

function getIcon(mode: SessionMode): string {
  switch (mode.type) {
    case 'remediation':
      return mode.blockedPromotion ? '🔒' : '💪'
    case 'progression':
      return '🌟'
    case 'maintenance':
      return '✨'
  }
}

function getTitle(mode: SessionMode): string {
  switch (mode.type) {
    case 'remediation':
      return mode.blockedPromotion ? 'Almost there!' : 'Strengthening skills'
    case 'progression':
      return mode.tutorialRequired ? 'Ready to learn!' : 'Practice new skill'
    case 'maintenance':
      return 'All skills strong!'
  }
}

function getSubtitle(mode: SessionMode): string {
  switch (mode.type) {
    case 'remediation': {
      const skills = mode.weakSkills.slice(0, 2).map((s) => s.displayName)
      if (mode.blockedPromotion) {
        return `Strengthen ${skills.join(', ')} to unlock ${mode.blockedPromotion.nextSkill.displayName}`
      }
      return `Targeting: ${skills.join(', ')}${mode.weakSkills.length > 2 ? '...' : ''}`
    }
    case 'progression':
      return mode.nextSkill.displayName
    case 'maintenance':
      return `${mode.skillCount} skills mastered`
  }
}

function getButtonText(mode: SessionMode, isLoading: boolean): string {
  if (isLoading) return '...'
  switch (mode.type) {
    case 'remediation':
      return 'Practice'
    case 'progression':
      return mode.tutorialRequired ? 'Tutorial' : 'Practice'
    case 'maintenance':
      return 'Practice'
  }
}

// ============================================================================
// Main Component
// ============================================================================

/**
 * MorphingBanner - A banner that smoothly morphs between full and compact layouts
 *
 * Layout transitions:
 * - Full (progress=0): Vertical layout, button at bottom center
 * - Compact (progress=1): Horizontal layout, button on right
 *
 * All internal positions/sizes are interpolated from the SpringValues passed in,
 * creating a smooth morphing animation as progress changes.
 */
export function MorphingBanner({
  sessionMode,
  onAction,
  isLoading = false,
  isDark,
  progress,
  containerWidth,
  containerHeight,
}: MorphingBannerProps) {
  const colors = getColorScheme(sessionMode.type, isDark)
  const icon = getIcon(sessionMode)
  const title = getTitle(sessionMode)
  const subtitle = getSubtitle(sessionMode)
  const buttonText = getButtonText(sessionMode, isLoading)

  // Button dimensions (constants)
  const fullButtonHeight = 48
  const compactButtonWidth = 90
  const compactButtonHeight = 32

  // Icon sizes (constants)
  const fullIconSize = 32
  const compactIconSize = 16

  // Icon positions (constants for full mode)
  const fullIconX = 20
  const fullIconY = 16
  const compactIconX = 8

  // Text positions (constants)
  const fullTextX = 60
  const fullTextY = 16
  const compactTextX = 32

  // Interpolated button styles using to()
  const buttonStyles = to([progress, containerWidth, containerHeight], (p, width, height) => {
    // Full mode: full width, flush with bottom
    const fullButtonWidth = width
    const fullButtonX = 0
    const fullButtonY = height - fullButtonHeight
    // Compact mode: small button on right
    const compactButtonX = width - compactButtonWidth - 8
    const compactButtonY = (height - compactButtonHeight) / 2

    return {
      left: lerp(fullButtonX, compactButtonX, p),
      top: lerp(fullButtonY, compactButtonY, p),
      width: lerp(fullButtonWidth, compactButtonWidth, p),
      height: lerp(fullButtonHeight, compactButtonHeight, p),
      fontSize: lerp(16, 13, p),
      paddingLeft: lerp(24, 12, p),
      paddingRight: lerp(24, 12, p),
      // Border radius: flat bottom in full mode, rounded in compact
      borderTopLeftRadius: lerp(0, 8, p),
      borderTopRightRadius: lerp(0, 8, p),
      borderBottomLeftRadius: lerp(0, 8, p),
      borderBottomRightRadius: lerp(0, 8, p),
    }
  })

  // Interpolated icon styles
  const iconStyles = to([progress, containerHeight], (p, height) => {
    const compactIconY = (height - compactIconSize) / 2
    return {
      left: lerp(fullIconX, compactIconX, p),
      top: lerp(fullIconY, compactIconY, p),
      fontSize: lerp(fullIconSize, compactIconSize, p),
    }
  })

  // Interpolated text styles
  const textStyles = to([progress, containerHeight], (p, height) => {
    const compactTextY = (height - 16) / 2
    return {
      left: lerp(fullTextX, compactTextX, p),
      top: lerp(fullTextY, compactTextY, p),
      right: lerp(24, compactButtonWidth + 16, p),
    }
  })

  // Title font size
  const titleFontSize = to(progress, (p) => lerp(16, 13, p))

  // Title margin bottom
  const titleMarginBottom = to(progress, (p) => lerp(4, 0, p))

  // Subtitle opacity (fades out in compact mode)
  const subtitleOpacity = to(progress, (p) => 1 - p)

  return (
    <div
      data-component="morphing-banner"
      data-mode={sessionMode.type}
      className={css({
        position: 'relative',
        width: '100%',
        height: '100%',
        borderRadius: '12px',
        overflow: 'hidden',
        border: '1px solid',
      })}
      style={{
        background: colors.bg,
        borderColor: colors.border,
      }}
    >
      {/* Icon */}
      <animated.span
        style={{
          position: 'absolute',
          left: iconStyles.to((s) => s.left),
          top: iconStyles.to((s) => s.top),
          fontSize: iconStyles.to((s) => s.fontSize),
          lineHeight: 1,
        }}
      >
        {icon}
      </animated.span>

      {/* Text content */}
      <animated.div
        style={{
          position: 'absolute',
          left: textStyles.to((s) => s.left),
          top: textStyles.to((s) => s.top),
          right: textStyles.to((s) => s.right),
        }}
      >
        {/* Title - shows subtitle text in compact mode for more useful info */}
        <animated.p
          style={{
            fontSize: titleFontSize,
            fontWeight: 600,
            color: colors.text,
            marginBottom: titleMarginBottom,
            whiteSpace: 'nowrap',
            overflow: 'hidden',
            textOverflow: 'ellipsis',
          }}
        >
          {title}
        </animated.p>
        {/* Subtitle - fades out in compact mode */}
        <animated.p
          style={{
            fontSize: 14,
            color: colors.subtext,
            opacity: subtitleOpacity,
            whiteSpace: 'nowrap',
            overflow: 'hidden',
            textOverflow: 'ellipsis',
          }}
        >
          {subtitle}
        </animated.p>
      </animated.div>

      {/* Action button */}
      <animated.button
        type="button"
        data-action="morphing-banner-action"
        onClick={onAction}
        disabled={isLoading}
        style={{
          position: 'absolute',
          left: buttonStyles.to((s) => s.left),
          top: buttonStyles.to((s) => s.top),
          width: buttonStyles.to((s) => s.width),
          height: buttonStyles.to((s) => s.height),
          fontSize: buttonStyles.to((s) => s.fontSize),
          paddingLeft: buttonStyles.to((s) => s.paddingLeft),
          paddingRight: buttonStyles.to((s) => s.paddingRight),
          borderTopLeftRadius: buttonStyles.to((s) => s.borderTopLeftRadius),
          borderTopRightRadius: buttonStyles.to((s) => s.borderTopRightRadius),
          borderBottomLeftRadius: buttonStyles.to((s) => s.borderBottomLeftRadius),
          borderBottomRightRadius: buttonStyles.to((s) => s.borderBottomRightRadius),
          background: isLoading ? '#9ca3af' : colors.buttonBg,
          color: colors.buttonText,
          border: 'none',
          fontWeight: 600,
          cursor: isLoading ? 'not-allowed' : 'pointer',
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          gap: 4,
        }}
        className={css({
          _hover: {
            filter: isLoading ? 'none' : 'brightness(1.1)',
          },
        })}
      >
        {buttonText}
        {!isLoading && <span>→</span>}
      </animated.button>
    </div>
  )
}

export default MorphingBanner