All files / web/src/components/tutorial DecompositionWithReasons.tsx

79.05% Statements 302/382
76.31% Branches 29/38
28.57% Functions 4/14
79.05% Lines 302/382

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 3831x 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 1x 1x 1x 1x 1x 1x 1x 50x 50x 50x 50x 50x 50x 6x 6x 44x 44x 44x 44x 50x 50x 50x 50x 50x 50x 50x             50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 1x 1x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 10x 10x 10x 11x 11x 11x 11x 11x 10x 10x 23x 23x 23x 23x 10x 10x 10x 23x 23x 23x 23x 18x 18x 23x 23x 23x 23x 10x                   23x 23x 23x                                                                                                 23x 23x                     23x 23x 23x 23x 23x 23x 23x 20x 20x 20x 20x 20x 28x 28x 28x 28x 28x 28x 26x 26x 26x 26x 28x 28x 28x 22x 22x 22x 22x 22x 44x 44x 44x 44x 44x 22x 22x 22x 22x 22x 22x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 28x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 28x 28x 28x 28x 20x 16x 16x 20x 20x 20x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x  
'use client'
 
import type React from 'react'
import { createContext, useContext, useEffect, useMemo } from 'react'
import { ReasonTooltip } from './ReasonTooltip'
import { useTutorialContext } from './TutorialContext'
import { useTutorialUI } from './TutorialUIContext'
import './decomposition-reasoning.css'
 
import type {
  PedagogicalRule,
  SegmentDecision,
  PedagogicalSegment,
} from '@/utils/unifiedStepGenerator'
 
export type { PedagogicalRule, SegmentDecision, PedagogicalSegment }
 
export interface TermReason {
  termIndex: number
  segmentId: string
  rule: PedagogicalRule
  shortReason: string
  bullets?: string[]
}
 
// Context for tracking active terms to highlight related segments
interface DecompositionContextType {
  activeTerms: Set<number>
  activeSegmentId: string | null
  addActiveTerm: (termIndex: number, segmentId?: string) => void
  removeActiveTerm: (termIndex: number, segmentId?: string) => void
}
 
const DecompositionContext = createContext<DecompositionContextType>({
  activeTerms: new Set(),
  activeSegmentId: null,
  addActiveTerm: () => {},
  removeActiveTerm: () => {},
})
 
interface DecompositionWithReasonsProps {
  fullDecomposition: string
  termPositions: Array<{ startIndex: number; endIndex: number }>
  segments?: PedagogicalSegment[]
  termReasons?: TermReason[]
  // NOTE: steps now comes from tutorial context, not props
}
 
interface TermSpanProps {
  termIndex: number
  text: string
  segment?: PedagogicalSegment
  reason?: TermReason
  isCurrentStep?: boolean
}
 
function TermSpan({ termIndex, text, segment, reason, isCurrentStep = false }: TermSpanProps) {
  const { activeSegmentId, addActiveTerm, removeActiveTerm } = useContext(DecompositionContext)
  const rule = reason?.rule ?? segment?.plan[0]?.rule
 
  // Only show styling for terms that have pedagogical reasoning
  if (!rule) {
    return <span className="term term--plain">{text}</span>
  }
 
  // Determine CSS classes based on current step only
  const cssClasses = [
    'term',
    isCurrentStep && 'term--current', // New class for current step highlighting
  ]
    .filter(Boolean)
    .join(' ')
 
  // Individual term hover handlers for two-level highlighting
  const handleTermHover = (isHovering: boolean) => {
    if (isHovering) {
      addActiveTerm(termIndex, segment?.id)
    } else {
      removeActiveTerm(termIndex, segment?.id)
    }
  }
 
  return (
    <span
      className={cssClasses}
      onMouseEnter={() => handleTermHover(true)}
      onMouseLeave={() => handleTermHover(false)}
      style={{ cursor: 'pointer' }}
    >
      {text}
    </span>
  )
}
 
// Component for rendering a segment group with unified hover target
interface SegmentGroupProps {
  segment: PedagogicalSegment
  fullDecomposition: string
  termPositions: Array<{ startIndex: number; endIndex: number }>
  termReasons?: TermReason[]
  children: React.ReactNode
}
 
function SegmentGroup({ segment, fullDecomposition, children }: SegmentGroupProps) {
  const { addActiveTerm, removeActiveTerm } = useContext(DecompositionContext)
 
  // Get steps from tutorial context instead of props
  const { unifiedSteps: steps } = useTutorialContext()
 
  // Calculate the original term that was expanded
  // digit * 10^place gives us the original value (e.g., digit=5, place=1 -> 50)
  const originalValue = (segment.digit * 10 ** segment.place).toString()
 
  // Get provenance from the first step in this segment
  const firstStepIndex = segment.termIndices[0]
  const firstStep = steps[firstStepIndex]
  const provenance = firstStep?.provenance
 
  const handleHighlightChange = (active: boolean) => {
    // Only handle highlighting, let HoverCard manage its own open/close timing
    if (active) {
      segment.termIndices.forEach((termIndex) => addActiveTerm(termIndex, segment.id))
    } else {
      segment.termIndices.forEach((termIndex) => removeActiveTerm(termIndex, segment.id))
    }
  }
 
  return (
    <ReasonTooltip
      termIndex={segment.termIndices[0]} // Use first term for tooltip ID
      segment={segment}
      originalValue={originalValue}
      steps={steps}
      provenance={provenance} // NEW: Pass provenance data
    >
      <span
        className="segment-group"
        onMouseEnter={() => handleHighlightChange(true)}
        onMouseLeave={() => handleHighlightChange(false)}
      >
        {children}
      </span>
    </ReasonTooltip>
  )
}
 
export function DecompositionWithReasons({
  fullDecomposition,
  termPositions,
  segments,
  termReasons,
}: DecompositionWithReasonsProps) {
  // Get context state including term highlighting
  const {
    state,
    activeTermIndices,
    setActiveTermIndices,
    activeIndividualTermIndex,
    setActiveIndividualTermIndex,
    getGroupTermIndicesFromTermIndex,
    unifiedSteps,
  } = useTutorialContext()
  const currentStepIndex = state.currentMultiStep
  const ui = useTutorialUI()
 
  // Build segment boundaries and ranges
  const _segmentRanges = useMemo(() => {
    if (!segments) return []
    return segments
      .map((seg) => ({
        segment: seg,
        startIndex: Math.min(
          ...seg.termIndices.map((i) => termPositions[i]?.startIndex ?? Infinity)
        ),
        endIndex: Math.max(...seg.termIndices.map((i) => termPositions[i]?.endIndex ?? 0)),
      }))
      .sort((a, b) => a.startIndex - b.startIndex)
  }, [segments, termPositions])
 
  // Build a quick lookup: termIndex -> segment
  const termIndexToSegment = useMemo(() => {
    const map = new Map<number, PedagogicalSegment>()
    segments?.forEach((seg) => seg.termIndices.forEach((i) => map.set(i, seg)))
    return map
  }, [segments])
 
  // Update active segment in UI context based on current step
  useEffect(() => {
    const currentSegment = termIndexToSegment.get(currentStepIndex) || null
    ui.setActiveSegment(currentSegment)
  }, [currentStepIndex, termIndexToSegment, ui])
 
  // Determine which segment should be highlighted based on active terms
  const activeSegmentId = useMemo(() => {
    if (activeTermIndices.size === 0) return null

    // Find the segment that contains any of the active terms
    for (const termIndex of activeTermIndices) {
      const segment = termIndexToSegment.get(termIndex)
      if (segment) {
        return segment.id
      }
    }
    return null
  }, [activeTermIndices, termIndexToSegment])
 
  const addActiveTerm = (termIndex: number, _segmentId?: string) => {
    console.log('🎯 TERM HOVER START - termIndex:', termIndex)

    // Debug: Get the unified steps to see provenance data
    const hoveredStep = unifiedSteps[termIndex]

    console.log('📊 Hovered step data:', {
      termIndex,
      mathematicalTerm: hoveredStep?.mathematicalTerm,
      provenance: hoveredStep?.provenance,
      hasGroupId: !!hoveredStep?.provenance?.groupId,
      groupId: hoveredStep?.provenance?.groupId,
      rhsPlace: hoveredStep?.provenance?.rhsPlace,
      rhsValue: hoveredStep?.provenance?.rhsValue,
    })

    // Set individual term highlight (orange glow)
    setActiveIndividualTermIndex(termIndex)
    console.log('🟠 Set individual term highlight:', termIndex)

    // Set group term highlights (blue glow) - for complement groups, highlight only the target column
    const groupTermIndices = getGroupTermIndicesFromTermIndex(termIndex)
    console.log('🔵 Group term indices found:', groupTermIndices)

    if (groupTermIndices.length > 0) {
      // Debug: Log all terms in the group
      console.log('📝 All terms in group:')
      groupTermIndices.forEach((idx) => {
        const step = unifiedSteps[idx]
        console.log(
          `  - Term ${idx}: "${step?.mathematicalTerm}" (termPlace: ${step?.provenance?.termPlace}, rhsPlace: ${step?.provenance?.rhsPlace}, groupId: ${step?.provenance?.groupId})`
        )
      })

      // For complement groups, highlight only the target column (rhsPlace, not individual termPlaces)
      // Use any term from the group since they all share the same rhsPlace (target column)
      setActiveTermIndices(new Set([termIndex]))
      console.log(
        '🎯 Set group highlight for target column (rhsPlace) using term index:',
        termIndex
      )
    } else {
      // This is a standalone term, just highlight it
      setActiveTermIndices(new Set([termIndex]))
      console.log('🎯 Set standalone term highlight')
    }

    console.log('✅ TERM HOVER COMPLETE')
  }
 
  const removeActiveTerm = (termIndex: number, _segmentId?: string) => {
    console.log('🚫 TERM HOVER END - termIndex:', termIndex)

    // Clear individual term highlight
    setActiveIndividualTermIndex(null)
    console.log('🟠 Cleared individual term highlight')

    // Clear group term highlights
    setActiveTermIndices(new Set())
    console.log('🔵 Cleared group term highlights')
  }
 
  // Slice the decomposition string using termPositions
  const _pieces: React.ReactNode[] = []
  const _cursor = 0
 
  // Render elements with segment groupings
  const renderElements = () => {
    const elements: React.ReactNode[] = []
    let cursor = 0
    const _currentSegmentIndex = 0
 
    for (let termIndex = 0; termIndex < termPositions.length; termIndex++) {
      const { startIndex, endIndex } = termPositions[termIndex]
      const segment = termIndexToSegment.get(termIndex)
      const reason = termReasons?.find((r) => r.termIndex === termIndex)
 
      // Add connector text before this term
      if (cursor < startIndex) {
        elements.push(
          <span key={`connector-${cursor}`}>{fullDecomposition.slice(cursor, startIndex)}</span>
        )
      }
 
      // Check if this term starts a new segment
      if (segment && segment.termIndices[0] === termIndex) {
        // This is the first term of a segment - wrap all segment terms
        const segmentElements: React.ReactNode[] = []
        let segmentCursor = startIndex
 
        for (const segTermIndex of segment.termIndices) {
          const segPos = termPositions[segTermIndex]
          if (!segPos) continue
 
          // Add connector within segment
          if (segmentCursor < segPos.startIndex) {
            segmentElements.push(
              <span key={`seg-connector-${segmentCursor}`}>
                {fullDecomposition.slice(segmentCursor, segPos.startIndex)}
              </span>
            )
          }
 
          const segText = fullDecomposition.slice(segPos.startIndex, segPos.endIndex)
          const segReason = termReasons?.find((r) => r.termIndex === segTermIndex)
 
          segmentElements.push(
            <TermSpan
              key={`seg-term-${segTermIndex}`}
              termIndex={segTermIndex}
              text={segText}
              segment={segment}
              reason={segReason}
              isCurrentStep={segTermIndex === currentStepIndex}
            />
          )
 
          segmentCursor = segPos.endIndex
        }
 
        elements.push(
          <SegmentGroup
            key={`segment-${segment.id}`}
            segment={segment}
            fullDecomposition={fullDecomposition}
            termPositions={termPositions}
            termReasons={termReasons}
          >
            {segmentElements}
          </SegmentGroup>
        )
 
        // Skip ahead past all terms in this segment
        const lastSegTermIndex = segment.termIndices[segment.termIndices.length - 1]
        const lastSegPos = termPositions[lastSegTermIndex]
        cursor = lastSegPos?.endIndex ?? endIndex
        termIndex = lastSegTermIndex // Will be incremented by for loop
      } else if (!segment) {
        // Regular term not in a segment
        const termText = fullDecomposition.slice(startIndex, endIndex)
        elements.push(
          <TermSpan
            key={`term-${termIndex}`}
            termIndex={termIndex}
            text={termText}
            segment={segment}
            reason={reason}
            isCurrentStep={termIndex === currentStepIndex}
          />
        )
        cursor = endIndex
      }
      // If this term is part of a segment but not the first, it was already handled above
    }
 
    // Add trailing text
    if (cursor < fullDecomposition.length) {
      elements.push(<span key="trailing">{fullDecomposition.slice(cursor)}</span>)
    }
 
    return elements
  }
 
  return (
    <DecompositionContext.Provider
      value={{
        activeTerms: activeTermIndices,
        activeSegmentId,
        addActiveTerm,
        removeActiveTerm,
      }}
    >
      <div className="decomposition">{renderElements()}</div>
    </DecompositionContext.Provider>
  )
}