All files / web/src/components/flowchart DifficultyDistributionSlider.tsx

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

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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
'use client'

import { useState, useCallback, useRef, useEffect, useMemo } from 'react'
import { css } from '../../../styled-system/css'
import { hstack, vstack } from '../../../styled-system/patterns'

export interface DifficultyDistribution {
  easy: number
  medium: number
  hard: number
}

interface DifficultyDistributionSliderProps {
  /** Current distribution (must sum to 100) */
  distribution: DifficultyDistribution
  /** Called when user drags handles */
  onChange: (distribution: DifficultyDistribution) => void
  /** Available tiers (some flowcharts may only have easy+medium) */
  availableTiers: { easy: boolean; medium: boolean; hard: boolean }
  /** Tier counts from generated examples */
  tierCounts: { easy: number; medium: number; hard: number }
  /** Minimum percentage per available tier (default: 5) */
  minPercentage?: number
}

/**
 * Multi-knob range slider for adjusting difficulty distribution.
 *
 * Visual design:
 * ```
 * Easy          Medium          Hard
 * [====|==============|====]
 *  20%       60%         20%
 * ```
 *
 * Two drag handles divide the bar into three sections.
 * Percentages update as handles are dragged.
 */
export function DifficultyDistributionSlider({
  distribution,
  onChange,
  availableTiers,
  tierCounts,
  minPercentage = 5,
}: DifficultyDistributionSliderProps) {
  const trackRef = useRef<HTMLDivElement>(null)
  const [isDragging, setIsDragging] = useState<'left' | 'right' | null>(null)

  // Calculate which tiers are active
  const activeTiers = useMemo(() => {
    const tiers: ('easy' | 'medium' | 'hard')[] = []
    if (availableTiers.easy && tierCounts.easy > 0) tiers.push('easy')
    if (availableTiers.medium && tierCounts.medium > 0) tiers.push('medium')
    if (availableTiers.hard && tierCounts.hard > 0) tiers.push('hard')
    return tiers
  }, [availableTiers, tierCounts])

  // Calculate handle positions as percentages
  const leftHandlePos = distribution.easy
  const rightHandlePos = distribution.easy + distribution.medium

  // Get position from event (mouse or touch)
  const getPositionFromEvent = useCallback((e: MouseEvent | TouchEvent): number | null => {
    if (!trackRef.current) return null
    const rect = trackRef.current.getBoundingClientRect()
    const clientX = 'touches' in e ? e.touches[0].clientX : e.clientX
    const percentage = ((clientX - rect.left) / rect.width) * 100
    return Math.max(0, Math.min(100, percentage))
  }, [])

  // Handle drag
  const handleDrag = useCallback(
    (e: MouseEvent | TouchEvent) => {
      if (!isDragging) return

      const pos = getPositionFromEvent(e)
      if (pos === null) return

      let newDistribution: DifficultyDistribution

      if (isDragging === 'left') {
        // Moving the left handle (easy/medium boundary)
        const newEasy = Math.max(minPercentage, Math.min(rightHandlePos - minPercentage, pos))
        const newMedium = rightHandlePos - newEasy
        const newHard = 100 - rightHandlePos
        newDistribution = {
          easy: Math.round(newEasy),
          medium: Math.round(newMedium),
          hard: Math.round(newHard),
        }
      } else {
        // Moving the right handle (medium/hard boundary)
        const newRight = Math.max(leftHandlePos + minPercentage, Math.min(100 - minPercentage, pos))
        const newMedium = newRight - leftHandlePos
        const newHard = 100 - newRight
        newDistribution = {
          easy: Math.round(leftHandlePos),
          medium: Math.round(newMedium),
          hard: Math.round(newHard),
        }
      }

      // Normalize to ensure sum is exactly 100
      const sum = newDistribution.easy + newDistribution.medium + newDistribution.hard
      if (sum !== 100) {
        const diff = 100 - sum
        newDistribution.medium += diff
      }

      onChange(newDistribution)
    },
    [isDragging, leftHandlePos, rightHandlePos, minPercentage, onChange, getPositionFromEvent]
  )

  // Handle drag end
  const handleDragEnd = useCallback(() => {
    setIsDragging(null)
  }, [])

  // Add/remove event listeners
  useEffect(() => {
    if (!isDragging) return

    const handleMouseMove = (e: MouseEvent) => handleDrag(e)
    const handleTouchMove = (e: TouchEvent) => handleDrag(e)
    const handleMouseUp = () => handleDragEnd()
    const handleTouchEnd = () => handleDragEnd()

    window.addEventListener('mousemove', handleMouseMove)
    window.addEventListener('touchmove', handleTouchMove)
    window.addEventListener('mouseup', handleMouseUp)
    window.addEventListener('touchend', handleTouchEnd)

    return () => {
      window.removeEventListener('mousemove', handleMouseMove)
      window.removeEventListener('touchmove', handleTouchMove)
      window.removeEventListener('mouseup', handleMouseUp)
      window.removeEventListener('touchend', handleTouchEnd)
    }
  }, [isDragging, handleDrag, handleDragEnd])

  // Handle click on track to jump to position
  const handleTrackClick = useCallback(
    (e: React.MouseEvent) => {
      if (!trackRef.current) return

      const rect = trackRef.current.getBoundingClientRect()
      const percentage = ((e.clientX - rect.left) / rect.width) * 100

      // Determine which handle to move based on click position
      const distToLeft = Math.abs(percentage - leftHandlePos)
      const distToRight = Math.abs(percentage - rightHandlePos)

      if (distToLeft < distToRight) {
        // Move left handle
        const newEasy = Math.max(
          minPercentage,
          Math.min(rightHandlePos - minPercentage, percentage)
        )
        const newMedium = rightHandlePos - newEasy
        onChange({
          easy: Math.round(newEasy),
          medium: Math.round(newMedium),
          hard: Math.round(100 - rightHandlePos),
        })
      } else {
        // Move right handle
        const newRight = Math.max(
          leftHandlePos + minPercentage,
          Math.min(100 - minPercentage, percentage)
        )
        const newMedium = newRight - leftHandlePos
        onChange({
          easy: Math.round(leftHandlePos),
          medium: Math.round(newMedium),
          hard: Math.round(100 - newRight),
        })
      }
    },
    [leftHandlePos, rightHandlePos, minPercentage, onChange]
  )

  // If only one tier is available, show a simpler display
  if (activeTiers.length <= 1) {
    return (
      <div
        data-component="difficulty-slider"
        className={vstack({ gap: '2', alignItems: 'center' })}
      >
        <p
          className={css({
            fontSize: 'sm',
            color: { base: 'gray.500', _dark: 'gray.400' },
            textAlign: 'center',
          })}
        >
          {activeTiers.length === 0
            ? 'No examples available'
            : `Only ${activeTiers[0]} problems available (${tierCounts[activeTiers[0]]} examples)`}
        </p>
      </div>
    )
  }

  // If only two tiers are available, show a simpler two-section slider
  if (activeTiers.length === 2) {
    const [tier1, tier2] = activeTiers
    const tier1Percent = tier1 === 'easy' ? distribution.easy : distribution.medium
    const tier2Percent = 100 - tier1Percent

    return (
      <div
        data-component="difficulty-slider"
        className={vstack({ gap: '3', alignItems: 'stretch' })}
      >
        {/* Labels */}
        <div className={hstack({ gap: '4', justifyContent: 'space-between' })}>
          <div className={vstack({ gap: '0', alignItems: 'flex-start' })}>
            <span
              className={css({
                fontSize: 'sm',
                fontWeight: 'semibold',
                color: getTierColor(tier1),
              })}
            >
              {getTierLabel(tier1)}
            </span>
            <span
              className={css({
                fontSize: 'xs',
                color: { base: 'gray.500', _dark: 'gray.400' },
              })}
            >
              {tierCounts[tier1]} available
            </span>
          </div>
          <div className={vstack({ gap: '0', alignItems: 'flex-end' })}>
            <span
              className={css({
                fontSize: 'sm',
                fontWeight: 'semibold',
                color: getTierColor(tier2),
              })}
            >
              {getTierLabel(tier2)}
            </span>
            <span
              className={css({
                fontSize: 'xs',
                color: { base: 'gray.500', _dark: 'gray.400' },
              })}
            >
              {tierCounts[tier2]} available
            </span>
          </div>
        </div>

        {/* Simple two-section slider */}
        <div
          ref={trackRef}
          onClick={handleTrackClick}
          className={css({
            position: 'relative',
            height: '24px',
            borderRadius: 'lg',
            overflow: 'hidden',
            cursor: 'pointer',
            display: 'flex',
          })}
        >
          <div
            className={css({ backgroundColor: getTierBgColor(tier1) })}
            style={{ width: `${tier1Percent}%` }}
          />
          <div
            className={css({ backgroundColor: getTierBgColor(tier2) })}
            style={{ width: `${tier2Percent}%` }}
          />

          {/* Handle */}
          <div
            onMouseDown={() => setIsDragging('left')}
            onTouchStart={() => setIsDragging('left')}
            className={css({
              position: 'absolute',
              top: '50%',
              transform: 'translate(-50%, -50%)',
              width: '20px',
              height: '28px',
              backgroundColor: { base: 'white', _dark: 'gray.300' },
              borderRadius: 'md',
              border: '2px solid',
              borderColor: { base: 'gray.400', _dark: 'gray.500' },
              cursor: 'grab',
              boxShadow: 'md',
              zIndex: 10,
              _hover: { borderColor: { base: 'blue.500', _dark: 'blue.400' } },
              _active: { cursor: 'grabbing' },
            })}
            style={{ left: `${tier1Percent}%` }}
          />
        </div>

        {/* Percentages */}
        <div className={hstack({ gap: '4', justifyContent: 'space-between' })}>
          <span className={css({ fontSize: 'lg', fontWeight: 'bold', color: getTierColor(tier1) })}>
            {Math.round(tier1Percent)}%
          </span>
          <span className={css({ fontSize: 'lg', fontWeight: 'bold', color: getTierColor(tier2) })}>
            {Math.round(tier2Percent)}%
          </span>
        </div>
      </div>
    )
  }

  // Full three-tier slider
  return (
    <div data-component="difficulty-slider" className={vstack({ gap: '3', alignItems: 'stretch' })}>
      {/* Labels */}
      <div className={hstack({ gap: '2', justifyContent: 'space-between' })}>
        <div className={vstack({ gap: '0', alignItems: 'flex-start' })}>
          <span
            className={css({
              fontSize: 'sm',
              fontWeight: 'semibold',
              color: { base: 'emerald.600', _dark: 'emerald.400' },
            })}
          >
            Easy
          </span>
          <span
            className={css({
              fontSize: 'xs',
              color: { base: 'gray.500', _dark: 'gray.400' },
            })}
          >
            {tierCounts.easy} available
          </span>
        </div>
        <div className={vstack({ gap: '0', alignItems: 'center' })}>
          <span
            className={css({
              fontSize: 'sm',
              fontWeight: 'semibold',
              color: { base: 'amber.600', _dark: 'amber.400' },
            })}
          >
            Medium
          </span>
          <span
            className={css({
              fontSize: 'xs',
              color: { base: 'gray.500', _dark: 'gray.400' },
            })}
          >
            {tierCounts.medium} available
          </span>
        </div>
        <div className={vstack({ gap: '0', alignItems: 'flex-end' })}>
          <span
            className={css({
              fontSize: 'sm',
              fontWeight: 'semibold',
              color: { base: 'rose.600', _dark: 'rose.400' },
            })}
          >
            Hard
          </span>
          <span
            className={css({
              fontSize: 'xs',
              color: { base: 'gray.500', _dark: 'gray.400' },
            })}
          >
            {tierCounts.hard} available
          </span>
        </div>
      </div>

      {/* Track */}
      <div
        ref={trackRef}
        onClick={handleTrackClick}
        className={css({
          position: 'relative',
          height: '24px',
          borderRadius: 'lg',
          overflow: 'hidden',
          cursor: 'pointer',
          display: 'flex',
        })}
      >
        {/* Easy section */}
        <div
          className={css({
            backgroundColor: { base: 'emerald.400', _dark: 'emerald.600' },
            transition: 'width 0.1s',
          })}
          style={{ width: `${distribution.easy}%` }}
        />
        {/* Medium section */}
        <div
          className={css({
            backgroundColor: { base: 'amber.400', _dark: 'amber.600' },
            transition: 'width 0.1s',
          })}
          style={{ width: `${distribution.medium}%` }}
        />
        {/* Hard section */}
        <div
          className={css({
            backgroundColor: { base: 'rose.400', _dark: 'rose.600' },
            transition: 'width 0.1s',
          })}
          style={{ width: `${distribution.hard}%` }}
        />

        {/* Left handle (easy/medium boundary) */}
        <div
          data-handle="left"
          onMouseDown={() => setIsDragging('left')}
          onTouchStart={() => setIsDragging('left')}
          className={css({
            position: 'absolute',
            top: '50%',
            transform: 'translate(-50%, -50%)',
            width: '16px',
            height: '28px',
            backgroundColor: { base: 'white', _dark: 'gray.300' },
            borderRadius: 'md',
            border: '2px solid',
            borderColor:
              isDragging === 'left'
                ? { base: 'blue.500', _dark: 'blue.400' }
                : { base: 'gray.400', _dark: 'gray.500' },
            cursor: 'grab',
            boxShadow: 'md',
            zIndex: 10,
            transition: 'border-color 0.15s',
            _hover: { borderColor: { base: 'blue.500', _dark: 'blue.400' } },
            _active: { cursor: 'grabbing' },
          })}
          style={{ left: `${distribution.easy}%` }}
        />

        {/* Right handle (medium/hard boundary) */}
        <div
          data-handle="right"
          onMouseDown={() => setIsDragging('right')}
          onTouchStart={() => setIsDragging('right')}
          className={css({
            position: 'absolute',
            top: '50%',
            transform: 'translate(-50%, -50%)',
            width: '16px',
            height: '28px',
            backgroundColor: { base: 'white', _dark: 'gray.300' },
            borderRadius: 'md',
            border: '2px solid',
            borderColor:
              isDragging === 'right'
                ? { base: 'blue.500', _dark: 'blue.400' }
                : { base: 'gray.400', _dark: 'gray.500' },
            cursor: 'grab',
            boxShadow: 'md',
            zIndex: 10,
            transition: 'border-color 0.15s',
            _hover: { borderColor: { base: 'blue.500', _dark: 'blue.400' } },
            _active: { cursor: 'grabbing' },
          })}
          style={{ left: `${distribution.easy + distribution.medium}%` }}
        />
      </div>

      {/* Percentages */}
      <div className={hstack({ gap: '2', justifyContent: 'space-between' })}>
        <span
          className={css({
            fontSize: 'lg',
            fontWeight: 'bold',
            color: { base: 'emerald.600', _dark: 'emerald.400' },
          })}
        >
          {distribution.easy}%
        </span>
        <span
          className={css({
            fontSize: 'lg',
            fontWeight: 'bold',
            color: { base: 'amber.600', _dark: 'amber.400' },
          })}
        >
          {distribution.medium}%
        </span>
        <span
          className={css({
            fontSize: 'lg',
            fontWeight: 'bold',
            color: { base: 'rose.600', _dark: 'rose.400' },
          })}
        >
          {distribution.hard}%
        </span>
      </div>
    </div>
  )
}

// Helper functions for tier colors
function getTierLabel(tier: 'easy' | 'medium' | 'hard'): string {
  switch (tier) {
    case 'easy':
      return 'Easy'
    case 'medium':
      return 'Medium'
    case 'hard':
      return 'Hard'
  }
}

function getTierColor(tier: 'easy' | 'medium' | 'hard') {
  switch (tier) {
    case 'easy':
      return { base: 'emerald.600', _dark: 'emerald.400' }
    case 'medium':
      return { base: 'amber.600', _dark: 'amber.400' }
    case 'hard':
      return { base: 'rose.600', _dark: 'rose.400' }
  }
}

function getTierBgColor(tier: 'easy' | 'medium' | 'hard') {
  switch (tier) {
    case 'easy':
      return { base: 'emerald.400', _dark: 'emerald.600' }
    case 'medium':
      return { base: 'amber.400', _dark: 'amber.600' }
    case 'hard':
      return { base: 'rose.400', _dark: 'rose.600' }
  }
}