All files / web/src/app/vision-training/train/components NumeralSelector.tsx

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

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

import { css } from '../../../../../styled-system/css'

interface NumeralSelectorProps {
  /** Count of images per digit (0-9) */
  digitCounts: Record<number, number>
  /** Currently selected digit */
  selectedDigit: number
  /** Callback when digit is selected */
  onSelectDigit: (digit: number) => void
  /** Minimum count to be considered "good" */
  goodThreshold?: number
  /** Minimum count to be considered "minimal" */
  minimalThreshold?: number
  /** Compact mode for mobile */
  compact?: boolean
}

type HealthStatus = 'critical' | 'low' | 'good' | 'excellent'

const HEALTH_CONFIG: Record<HealthStatus, { color: string; bgColor: string; borderColor: string }> =
  {
    critical: {
      color: 'red.400',
      bgColor: 'red.950',
      borderColor: 'red.700',
    },
    low: {
      color: 'yellow.400',
      bgColor: 'yellow.950',
      borderColor: 'yellow.700',
    },
    good: {
      color: 'green.400',
      bgColor: 'green.950',
      borderColor: 'green.700',
    },
    excellent: {
      color: 'blue.400',
      bgColor: 'blue.950',
      borderColor: 'blue.700',
    },
  }

/**
 * Numeral selector bar showing all 10 digits (0-9) with counts and health indicators.
 * Click any digit to select it for browsing/capturing.
 */
export function NumeralSelector({
  digitCounts,
  selectedDigit,
  onSelectDigit,
  goodThreshold = 20,
  minimalThreshold = 5,
  compact = false,
}: NumeralSelectorProps) {
  const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  const maxCount = Math.max(...Object.values(digitCounts), 1)

  const getHealthStatus = (count: number): HealthStatus => {
    if (count < minimalThreshold) return 'critical'
    if (count < goodThreshold) return 'low'
    if (count < goodThreshold * 2) return 'good'
    return 'excellent'
  }

  const getBarHeight = (count: number): string => {
    const percentage = Math.min((count / maxCount) * 100, 100)
    return `${Math.max(percentage, 5)}%` // Minimum 5% so it's visible
  }

  return (
    <div
      data-component="numeral-selector"
      className={css({
        display: 'flex',
        gap: compact ? 1 : 1,
        p: compact ? 1 : 2,
        bg: 'gray.900',
        borderRadius: 'lg',
        justifyContent: 'center',
        flexWrap: compact ? 'wrap' : 'nowrap',
      })}
    >
      {digits.map((digit) => {
        const count = digitCounts[digit] || 0
        const health = getHealthStatus(count)
        const config = HEALTH_CONFIG[health]
        const isSelected = selectedDigit === digit

        return (
          <button
            key={digit}
            type="button"
            data-action="select-digit"
            onClick={() => onSelectDigit(digit)}
            data-digit={digit}
            data-selected={isSelected}
            data-health={health}
            data-count={count}
            className={css({
              position: 'relative',
              display: 'flex',
              flexDirection: 'column',
              alignItems: 'center',
              gap: 0.5,
              p: compact ? 1.5 : 2,
              minWidth: compact ? '32px' : '48px',
              bg: isSelected ? config.bgColor : 'gray.850',
              border: '2px solid',
              borderColor: isSelected ? config.borderColor : 'transparent',
              borderRadius: 'md',
              cursor: 'pointer',
              transition: 'all 0.15s ease',
              _hover: {
                bg: isSelected ? config.bgColor : 'gray.800',
                borderColor: isSelected ? config.borderColor : 'gray.700',
              },
              _active: {
                transform: 'translateY(0)',
              },
            })}
          >
            {/* Digit numeral */}
            <span
              data-element="digit-numeral"
              className={css({
                fontSize: compact ? 'md' : 'lg',
                fontWeight: 'bold',
                fontFamily: 'mono',
                color: isSelected ? config.color : 'gray.300',
                lineHeight: 1,
              })}
            >
              {digit}
            </span>

            {/* Count bar visualization */}
            <div
              data-element="count-bar-container"
              className={css({
                width: '100%',
                height: compact ? '16px' : '20px',
                bg: 'gray.800',
                borderRadius: 'sm',
                overflow: 'hidden',
                position: 'relative',
              })}
            >
              {/* Fill bar */}
              <div
                data-element="count-bar-fill"
                className={css({
                  position: 'absolute',
                  bottom: 0,
                  left: 0,
                  right: 0,
                  bg: config.color,
                  opacity: isSelected ? 1 : 0.6,
                  transition: 'all 0.2s ease',
                  borderRadius: 'sm',
                })}
                style={{ height: getBarHeight(count) }}
              />
              {/* Count text overlay */}
              <span
                data-element="count-text"
                className={css({
                  position: 'absolute',
                  inset: 0,
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                  fontSize: '2xs',
                  fontWeight: 'bold',
                  fontFamily: 'mono',
                  color: 'white',
                  textShadow: '0 1px 2px rgba(0,0,0,0.8)',
                  zIndex: 1,
                })}
              >
                {count}
              </span>
            </div>
          </button>
        )
      })}
    </div>
  )
}

export default NumeralSelector