All files / web/src/components/toys/number-line/constants ConstantInfoCard.tsx

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

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

import { useEffect, useRef, useState } from 'react'
import type { MathConstant } from './constantsData'
import { useTTS } from '@/hooks/useTTS'
import { useAudioManagerInstance } from '@/contexts/AudioManagerContext'
import { EXPLORATION_IDS } from '../talkToNumber/explorationRegistry'

interface ConstantInfoCardProps {
  constant: MathConstant
  /** Screen X position of the constant on the canvas (CSS px) */
  screenX: number
  /** Width of the canvas container (CSS px) */
  containerWidth: number
  /** Height of the canvas container (CSS px) */
  containerHeight: number
  /** Center Y of the axis line (CSS px) */
  centerY: number
  isDark: boolean
  onDismiss: () => void
  /** Called when user taps "Explore" to launch the constant's demo */
  onExplore?: (constantId: string) => void
  /** Called when user taps "Call" to start a voice conversation with this number */
  onCallNumber?: (value: number) => void
}

const CARD_WIDTH = 220
const CARD_PAD = 12

export function ConstantInfoCard({
  constant,
  screenX,
  containerWidth,
  containerHeight,
  centerY,
  isDark,
  onDismiss,
  onExplore,
  onCallNumber,
}: ConstantInfoCardProps) {
  const cardRef = useRef<HTMLDivElement>(null)
  const audioManager = useAudioManagerInstance()

  // TTS: auto-speak on mount
  const speak = useTTS(`constant-${constant.id}`, {
    tone: 'Warmly explaining a math concept to a curious child.',
    say: { en: constant.ttsExplanation },
  })

  useEffect(() => {
    console.log(`[ConstantInfoCard] 🟢 MOUNT effect — calling speak() for "${constant.id}"`)
    speak()
    return () => {
      console.log(
        `[ConstantInfoCard] 🔴 CLEANUP — calling audioManager.stop() for "${constant.id}"`
      )
      // Cancel in-flight audio on unmount (prevents React strict mode
      // double-mount from producing two overlapping narrations)
      audioManager.stop()
    }
  }, [speak, audioManager])

  // Dismiss on click outside
  useEffect(() => {
    function handlePointerDown(e: PointerEvent) {
      if (cardRef.current && !cardRef.current.contains(e.target as Node)) {
        onDismiss()
      }
    }
    // Use a small delay so the tap that opened the card doesn't immediately close it
    const timer = setTimeout(() => {
      document.addEventListener('pointerdown', handlePointerDown)
    }, 50)
    return () => {
      clearTimeout(timer)
      document.removeEventListener('pointerdown', handlePointerDown)
    }
  }, [onDismiss])

  // Position: clamp horizontally, place above or below axis
  const clampedX = Math.max(
    CARD_PAD,
    Math.min(containerWidth - CARD_WIDTH - CARD_PAD, screenX - CARD_WIDTH / 2)
  )
  const spaceAbove = centerY
  const spaceBelow = containerHeight - centerY
  const placeAbove = spaceAbove > spaceBelow
  const rawTop = placeAbove ? Math.max(CARD_PAD, centerY - 140) : centerY + 20
  // Clamp so the card never extends past the container bottom.
  // Reserve at least 200px for the card so the Explore button is visible.
  const top = Math.min(rawTop, containerHeight - CARD_PAD - 200)

  const hasImages = !!(constant.metaphorImage && constant.mathImage)
  const [imageTab, setImageTab] = useState<'metaphor' | 'math'>('metaphor')

  const bg = isDark ? 'rgba(30, 30, 40, 0.92)' : 'rgba(255, 255, 255, 0.92)'
  const textColor = isDark ? '#f3f4f6' : '#1f2937'
  const subtextColor = isDark ? '#9ca3af' : '#6b7280'
  const symbolColor = isDark ? '#f59e0b' : '#4338ca'

  return (
    <div
      ref={cardRef}
      data-component="constant-info-card"
      style={{
        position: 'absolute',
        left: clampedX,
        top,
        width: CARD_WIDTH,
        maxHeight: containerHeight - top - CARD_PAD,
        overflowY: 'auto',
        padding: '12px 14px',
        borderRadius: 10,
        backgroundColor: bg,
        backdropFilter: 'blur(8px)',
        boxShadow: isDark ? '0 4px 20px rgba(0,0,0,0.5)' : '0 4px 20px rgba(0,0,0,0.12)',
        zIndex: 10,
        pointerEvents: 'auto',
        animation: 'fadeInUp 0.2s ease-out',
      }}
    >
      {/* MathML symbol */}
      <div
        data-element="constant-symbol"
        style={{
          fontSize: 28,
          fontWeight: 700,
          color: symbolColor,
          lineHeight: 1.3,
          marginBottom: 6,
        }}
        dangerouslySetInnerHTML={{ __html: constant.mathml }}
      />

      <div
        data-element="constant-name"
        style={{
          fontSize: 13,
          fontWeight: 600,
          color: textColor,
          marginBottom: 2,
        }}
      >
        {constant.name}
      </div>

      <div
        data-element="constant-value"
        style={{
          fontSize: 12,
          fontFamily: 'monospace',
          color: subtextColor,
          marginBottom: 6,
        }}
      >
        {formatConstantValue(constant.value)}
      </div>

      {/* Illustration with tab toggle */}
      {hasImages && (
        <div data-element="constant-illustration" style={{ marginBottom: 8 }}>
          <div
            data-element="illustration-tabs"
            style={{
              display: 'flex',
              gap: 4,
              marginBottom: 6,
            }}
          >
            {(['metaphor', 'math'] as const).map((tab) => {
              const active = imageTab === tab
              return (
                <button
                  key={tab}
                  data-action={`illustration-tab-${tab}`}
                  onClick={() => setImageTab(tab)}
                  style={{
                    flex: 1,
                    padding: '3px 0',
                    fontSize: 11,
                    fontWeight: active ? 600 : 400,
                    color: active ? (isDark ? '#1f2937' : '#fff') : subtextColor,
                    backgroundColor: active ? symbolColor : 'transparent',
                    border: `1px solid ${active ? symbolColor : isDark ? 'rgba(255,255,255,0.15)' : 'rgba(0,0,0,0.12)'}`,
                    borderRadius: 4,
                    cursor: 'pointer',
                  }}
                >
                  {tab === 'metaphor' ? 'Imagine' : 'Math'}
                </button>
              )
            })}
          </div>
          <ThemedImage
            baseSrc={(imageTab === 'metaphor' ? constant.metaphorImage : constant.mathImage)!}
            isDark={isDark}
            alt={`${constant.name} — ${imageTab === 'metaphor' ? 'metaphor' : 'math'} illustration`}
            style={{
              width: '100%',
              aspectRatio: '1 / 1',
              borderRadius: 6,
              objectFit: 'cover',
            }}
          />
        </div>
      )}

      <div
        data-element="constant-description"
        style={{
          fontSize: 12,
          color: subtextColor,
          lineHeight: 1.4,
        }}
      >
        {constant.description}
      </div>

      {/* Explore button — only for constants with demos */}
      {onExplore && EXPLORATION_IDS.has(constant.id) && (
        <button
          data-action="explore-constant"
          onClick={() => {
            console.log(`[ConstantInfoCard] 🔵 EXPLORE clicked for "${constant.id}"`)
            onExplore(constant.id)
            onDismiss()
          }}
          style={{
            marginTop: 8,
            padding: '10px 12px',
            minHeight: 44,
            fontSize: 13,
            fontWeight: 600,
            color: isDark ? '#1f2937' : '#fff',
            backgroundColor: symbolColor,
            border: 'none',
            borderRadius: 8,
            cursor: 'pointer',
            width: '100%',
          }}
        >
          Explore
        </button>
      )}

      {/* Call button — voice conversation with the constant */}
      {onCallNumber && (
        <button
          data-action="call-constant"
          onClick={() => {
            onCallNumber(constant.value)
            onDismiss()
          }}
          style={{
            marginTop: 6,
            padding: '10px 12px',
            minHeight: 44,
            fontSize: 13,
            fontWeight: 600,
            color: isDark ? '#e9d5ff' : '#6d28d9',
            backgroundColor: isDark ? 'rgba(139, 92, 246, 0.2)' : 'rgba(139, 92, 246, 0.1)',
            border: `1px solid ${isDark ? 'rgba(139, 92, 246, 0.3)' : 'rgba(139, 92, 246, 0.25)'}`,
            borderRadius: 8,
            cursor: 'pointer',
            width: '100%',
          }}
        >
          Call {constant.name}
        </button>
      )}

      {/* Inline keyframe animation */}
      <style>{`
        @keyframes fadeInUp {
          from { opacity: 0; transform: translateY(6px); }
          to { opacity: 1; transform: translateY(0); }
        }
      `}</style>
    </div>
  )
}

function ThemedImage({
  baseSrc,
  isDark,
  alt,
  style,
}: {
  baseSrc: string
  isDark: boolean
  alt: string
  style: React.CSSProperties
}) {
  const [useFallback, setUseFallback] = useState(false)

  useEffect(() => {
    setUseFallback(false)
  }, [isDark])

  const theme = isDark ? 'dark' : 'light'
  const themedSrc = !useFallback ? baseSrc.replace('.png', `-${theme}.png`) : baseSrc

  return (
    /* eslint-disable-next-line @next/next/no-img-element */
    <img
      data-element="constant-illustration-image"
      src={themedSrc}
      alt={alt}
      style={style}
      onError={() => {
        if (!useFallback) setUseFallback(true)
      }}
    />
  )
}

function formatConstantValue(value: number): string {
  // Show enough digits to be interesting but not overwhelming
  if (Number.isInteger(value)) return value.toString()
  const str = value.toPrecision(10)
  // Remove trailing zeros after decimal point
  return str.replace(/\.?0+$/, '')
}