All files / web/src/components/toys/number-line/primes PrimeTooltip.tsx

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

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

import type { PrimeTickInfo } from '../types'
import { factorize } from './sieve'
import { primeColorHex } from './primeColors'
import { getSpecialPrimeLabels, LABEL_COLORS } from './specialPrimes'
import type { SpecialPrimeLabel } from './specialPrimes'

export interface PartyState {
  invited: boolean
  canInvite: boolean
  rejectReason?: string
  emoji: string
}

interface PrimeTooltipProps {
  value: number
  primeInfo: PrimeTickInfo
  /** Screen X position of the tick (CSS px) */
  screenX: number
  /** Y position below the tick label area (CSS px) */
  tooltipY: number
  /** Width of the canvas container (CSS px) */
  containerWidth: number
  isDark: boolean
  /** Optional note from landmark/interestingness data (e.g., "Record gap of 72") */
  landmarkNote?: string
  /** When provided, shows a "Take the tour!" link for prime numbers */
  onStartTour?: () => void
  /** Called when the mouse enters/leaves the tooltip (to prevent hover-clear) */
  onMouseEnter?: () => void
  onMouseLeave?: () => void
  /** Hopping party invite state for this value */
  partyState?: PartyState
  /** Toggle invite/remove for this value */
  onToggleInvite?: (value: number) => void
}

const TOOLTIP_PAD = 8

/**
 * Format an exponent as a superscript string using Unicode superscript digits.
 */
function superscript(n: number): string {
  const superDigits = '\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079'
  return String(n)
    .split('')
    .map((d) => superDigits[parseInt(d)])
    .join('')
}

function SpecialLabel({ label, isDark }: { label: SpecialPrimeLabel; isDark: boolean }) {
  const color = LABEL_COLORS[label.type][isDark ? 'dark' : 'light']
  return (
    <div
      data-element="special-prime-label"
      style={{
        fontSize: 10,
        color,
        marginTop: 2,
        lineHeight: 1.3,
      }}
    >
      {label.text}
    </div>
  )
}

export function PrimeTooltip({
  value,
  primeInfo,
  screenX,
  tooltipY,
  containerWidth,
  isDark,
  landmarkNote,
  onStartTour,
  onMouseEnter,
  onMouseLeave,
  partyState,
  onToggleInvite,
}: PrimeTooltipProps) {
  const bg = isDark ? 'rgba(30, 30, 40, 0.92)' : 'rgba(255, 255, 255, 0.92)'
  const textColor = isDark ? '#f3f4f6' : '#1f2937'

  // Get special properties for primes
  const specialLabels = primeInfo.isPrime ? getSpecialPrimeLabels(value) : []

  // Show tour link for primes
  const showTourLink = primeInfo.isPrime && onStartTour

  // Show invite action for integers ≥ 2
  const showInviteAction = partyState && onToggleInvite && value >= 2

  // Wider tooltip when there are special properties or landmark notes or party state
  const tooltipWidth =
    specialLabels.length > 0 || landmarkNote || showTourLink || showInviteAction ? 220 : 180

  // Clamp horizontal position
  const clampedX = Math.max(
    TOOLTIP_PAD,
    Math.min(containerWidth - tooltipWidth - TOOLTIP_PAD, screenX - tooltipWidth / 2)
  )

  let mainContent: React.ReactNode

  if (primeInfo.classification === 'one') {
    mainContent = (
      <span data-element="tooltip-text" style={{ color: textColor, fontSize: 12 }}>
        <strong>1</strong> is neither prime nor composite
      </span>
    )
  } else if (primeInfo.isPrime) {
    const color = primeColorHex(value, isDark)
    mainContent = (
      <span data-element="tooltip-text" style={{ color: textColor, fontSize: 12 }}>
        <span
          data-element="prime-dot"
          style={{
            display: 'inline-block',
            width: 8,
            height: 8,
            borderRadius: '50%',
            backgroundColor: color,
            marginRight: 6,
            verticalAlign: 'middle',
          }}
        />
        <strong>{value}</strong> is prime
      </span>
    )
  } else {
    // Composite: show factorization with colored factors
    const factors = factorize(value)
    mainContent = (
      <span data-element="tooltip-text" style={{ color: textColor, fontSize: 12 }}>
        <strong>{value}</strong>
        {' = '}
        {factors.map((f, i) => (
          <span key={f.prime}>
            {i > 0 && ' \u00d7 '}
            <span style={{ color: primeColorHex(f.prime, isDark), fontWeight: 600 }}>
              {f.prime}
              {f.exponent > 1 && superscript(f.exponent)}
            </span>
          </span>
        ))}
      </span>
    )
  }

  const isInteractive = showTourLink || showInviteAction

  return (
    <div
      data-component="prime-tooltip"
      onMouseEnter={isInteractive ? onMouseEnter : undefined}
      onMouseLeave={isInteractive ? onMouseLeave : undefined}
      style={{
        position: 'absolute',
        left: clampedX,
        top: tooltipY,
        width: tooltipWidth,
        padding: '6px 10px',
        borderRadius: 8,
        backgroundColor: bg,
        backdropFilter: 'blur(8px)',
        boxShadow: isDark ? '0 2px 12px rgba(0,0,0,0.5)' : '0 2px 12px rgba(0,0,0,0.1)',
        zIndex: 10,
        pointerEvents: isInteractive ? 'auto' : 'none',
        whiteSpace: 'nowrap',
      }}
    >
      {mainContent}
      {specialLabels.length > 0 && (
        <div
          data-element="special-labels"
          style={{
            marginTop: 3,
            paddingTop: 3,
            borderTop: `1px solid ${isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.08)'}`,
          }}
        >
          {specialLabels.map((label, i) => (
            <SpecialLabel key={i} label={label} isDark={isDark} />
          ))}
        </div>
      )}
      {landmarkNote && (
        <div
          data-element="landmark-note"
          style={{
            fontSize: 10,
            color: isDark ? '#ffa070' : '#a04020',
            marginTop: 3,
            paddingTop: specialLabels.length > 0 ? 0 : 3,
            borderTop:
              specialLabels.length > 0
                ? 'none'
                : `1px solid ${isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.08)'}`,
            lineHeight: 1.3,
            fontStyle: 'italic',
          }}
        >
          {landmarkNote}
        </div>
      )}
      {showTourLink && (
        <div
          data-element="tour-link"
          style={{
            marginTop: 4,
            paddingTop: 4,
            borderTop: `1px solid ${isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.08)'}`,
          }}
        >
          <button
            data-action="start-prime-tour"
            onClick={onStartTour}
            style={{
              background: 'none',
              border: 'none',
              padding: 0,
              margin: 0,
              fontSize: 11,
              fontWeight: 600,
              color: isDark ? '#c4b5fd' : '#7c3aed',
              cursor: 'pointer',
              textDecoration: 'underline',
              textUnderlineOffset: 2,
            }}
          >
            Explore primes
          </button>
        </div>
      )}
      {showInviteAction && (
        <div
          data-element="party-invite"
          style={{
            marginTop: 4,
            paddingTop: 4,
            borderTop: `1px solid ${isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.08)'}`,
          }}
        >
          {partyState.invited ? (
            <button
              data-action="remove-from-party"
              onClick={() => onToggleInvite!(value)}
              style={{
                background: 'none',
                border: 'none',
                padding: 0,
                margin: 0,
                fontSize: 11,
                fontWeight: 600,
                color: isDark ? '#fca5a5' : '#dc2626',
                cursor: 'pointer',
              }}
            >
              {partyState.emoji} Remove from party
            </button>
          ) : partyState.canInvite ? (
            <button
              data-action="invite-to-party"
              onClick={() => onToggleInvite!(value)}
              style={{
                background: 'none',
                border: 'none',
                padding: 0,
                margin: 0,
                fontSize: 11,
                fontWeight: 600,
                color: isDark ? '#86efac' : '#16a34a',
                cursor: 'pointer',
              }}
            >
              {partyState.emoji} Invite to hopping party
            </button>
          ) : (
            <span
              data-element="invite-disabled"
              title={partyState.rejectReason ?? 'Cannot invite'}
              style={{
                fontSize: 11,
                color: isDark ? 'rgba(255,255,255,0.35)' : 'rgba(0,0,0,0.3)',
              }}
            >
              {partyState.emoji} {partyState.rejectReason ?? 'Cannot invite'}
            </span>
          )}
        </div>
      )}
    </div>
  )
}