All files / web/src/components/toys/coordinate-plane/challenge AnnotationConnectors.tsx

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

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

import { useMemo } from 'react'
import type { WordProblem, AnnotationTag } from '../wordProblems/types'
import type { CoordinatePlaneState } from '../types'
import { worldToScreen2D } from '../../shared/coordinateConversions'

/** Colors matching WordProblemCard tag colors */
const TAG_COLORS: Partial<Record<AnnotationTag, string>> = {
  slope: '#f59e0b',
  intercept: '#3b82f6',
  target: '#ef4444',
  answer: '#10b981',
  point1: '#8b5cf6',
  point2: '#ec4899',
}

interface AnnotationConnectorsProps {
  problem: WordProblem
  spanRefs: Map<AnnotationTag, HTMLSpanElement>
  stateRef: React.MutableRefObject<CoordinatePlaneState>
  canvasWidth: number
  canvasHeight: number
  revealStep: number
  isDark: boolean
  containerRef: React.RefObject<HTMLDivElement | null>
}

interface ConnectorLine {
  tag: AnnotationTag
  fromX: number
  fromY: number
  toX: number
  toY: number
  color: string
  index: number
}

export function AnnotationConnectors({
  problem,
  spanRefs,
  stateRef,
  canvasWidth,
  canvasHeight,
  revealStep,
  isDark,
  containerRef,
}: AnnotationConnectorsProps) {
  const connectors = useMemo(() => {
    const container = containerRef.current
    if (!container) return []

    const containerRect = container.getBoundingClientRect()
    const state = stateRef.current
    const lines: ConnectorLine[] = []

    // Get annotated spans that have geometric targets
    const annotatedSpans = problem.spans.filter(
      (s) => s.tag && s.tag !== 'context' && s.tag !== 'question'
    )

    annotatedSpans.forEach((span, index) => {
      if (index >= revealStep) return // Not yet revealed
      if (!span.tag) return

      const color = TAG_COLORS[span.tag]
      if (!color) return

      // Get text span position
      const spanEl = spanRefs.get(span.tag)
      if (!spanEl) return

      const spanRect = spanEl.getBoundingClientRect()
      const fromX = spanRect.left + spanRect.width / 2 - containerRect.left
      const fromY = spanRect.bottom - containerRect.top

      // Compute target geometry position
      const target = getGeometryTarget(span.tag, problem, state, canvasWidth, canvasHeight)
      if (!target) return

      lines.push({
        tag: span.tag,
        fromX,
        fromY,
        toX: target.x,
        toY: target.y,
        color,
        index,
      })
    })

    return lines
  }, [problem, spanRefs, stateRef, canvasWidth, canvasHeight, revealStep, containerRef])

  if (connectors.length === 0) return null

  return (
    <svg
      data-component="annotation-connectors"
      style={{
        position: 'absolute',
        inset: 0,
        width: '100%',
        height: '100%',
        pointerEvents: 'none',
        zIndex: 51,
      }}
    >
      {connectors.map((c) => (
        <ConnectorLine key={c.tag} connector={c} isDark={isDark} />
      ))}
    </svg>
  )
}

function ConnectorLine({ connector: c, isDark }: { connector: ConnectorLine; isDark: boolean }) {
  // Animate the line drawing using stroke-dasharray/offset
  const length = Math.sqrt((c.toX - c.fromX) ** 2 + (c.toY - c.fromY) ** 2)

  return (
    <g>
      <line
        x1={c.fromX}
        y1={c.fromY}
        x2={c.toX}
        y2={c.toY}
        stroke={c.color}
        strokeWidth={1.5}
        strokeDasharray={length}
        strokeDashoffset={0}
        opacity={0.7}
        style={{
          transition: 'stroke-dashoffset 400ms ease-out',
        }}
      />
      {/* Target dot */}
      <circle cx={c.toX} cy={c.toY} r={4} fill={c.color} opacity={0.8} />
    </g>
  )
}

/** Map annotation tags to geometric positions on the canvas */
function getGeometryTarget(
  tag: AnnotationTag,
  problem: WordProblem,
  state: CoordinatePlaneState,
  canvasWidth: number,
  canvasHeight: number
): { x: number; y: number } | null {
  const toScreen = (wx: number, wy: number) =>
    worldToScreen2D(
      wx,
      wy,
      state.center.x,
      state.center.y,
      state.pixelsPerUnit.x,
      state.pixelsPerUnit.y,
      canvasWidth,
      canvasHeight
    )

  const { slope, intercept } = problem.equation
  const m = slope.num / slope.den
  const b = intercept.num / intercept.den

  switch (tag) {
    case 'slope': {
      // Point at rise/run triangle midpoint — roughly at x=1 on the line
      const x = 1
      const y = m * x + b
      return toScreen(x, y)
    }
    case 'intercept': {
      // Y-intercept point
      return toScreen(0, b)
    }
    case 'target': {
      // Horizontal line at y = target — show at the solution x
      return toScreen(problem.answer.x, problem.answer.y)
    }
    case 'answer': {
      // The solution intersection point
      return toScreen(problem.answer.x, problem.answer.y)
    }
    case 'point1': {
      // First given point (level 4)
      const spans = problem.spans.find((s) => s.tag === 'point1')
      if (spans?.value != null) {
        // Point value is encoded in the span — but for now use the answer
        return toScreen(0, b)
      }
      return null
    }
    case 'point2': {
      // Second given point (level 4)
      return toScreen(problem.answer.x, problem.answer.y)
    }
    default:
      return null
  }
}