All files / web/src/components/practice SkillsPanel.tsx

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

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

import { useMemo } from 'react'
import { CATEGORY_PRIORITY } from '@/constants/skillCategories'
import type { SlotResult } from '@/db/schema/session-plans'
import { getCategoryDisplayName, getSkillDisplayName } from '@/utils/skillDisplay'
import { css } from '../../../styled-system/css'

interface SkillBreakdown {
  skillId: string
  correct: number
  total: number
}

interface SkillCategoryGroup {
  categoryId: string
  categoryName: string
  skills: SkillBreakdown[]
  /** Aggregate stats for the category */
  correct: number
  total: number
}

export interface SkillsPanelProps {
  results: SlotResult[]
  isDark: boolean
}

/**
 * SkillsPanel - Shows skills breakdown by category with human-readable names
 *
 * Features:
 * - Categories sorted by pedagogical order (CATEGORY_PRIORITY)
 * - Collapsible categories using <details>
 * - Progress bars with neutral blue color
 * - Human-readable skill names from SKILL_CATEGORIES
 */
export function SkillsPanel({ results, isDark }: SkillsPanelProps) {
  const skillCategories = useMemo(() => calculateSkillBreakdownByCategory(results), [results])

  if (skillCategories.length === 0) {
    return null
  }

  return (
    <div
      data-component="skills-panel"
      data-section="skill-breakdown"
      className={css({
        padding: '1rem',
        backgroundColor: isDark ? 'gray.800' : 'white',
        borderRadius: '12px',
        boxShadow: 'sm',
      })}
    >
      <h3
        className={css({
          fontSize: '1rem',
          fontWeight: 'bold',
          color: isDark ? 'gray.300' : 'gray.700',
          marginBottom: '1rem',
        })}
      >
        Skills Practiced
      </h3>

      <div
        className={css({
          display: 'flex',
          flexDirection: 'column',
          gap: '1.25rem',
        })}
      >
        {skillCategories.map((category) => (
          <details
            key={category.categoryId}
            data-element="skill-category"
            className={css({
              '& > summary': {
                listStyle: 'none',
                cursor: 'pointer',
                '&::-webkit-details-marker': { display: 'none' },
              },
            })}
          >
            {/* Category header with aggregate stats (clickable summary) */}
            <summary
              className={css({
                display: 'flex',
                alignItems: 'center',
                gap: '0.75rem',
                paddingBottom: '0.375rem',
                borderBottom: '1px solid',
                borderColor: isDark ? 'gray.700' : 'gray.200',
                _hover: {
                  backgroundColor: isDark ? 'gray.750' : 'gray.50',
                },
              })}
            >
              <div
                className={css({
                  flex: 1,
                  fontSize: '0.875rem',
                  fontWeight: 'bold',
                  color: isDark ? 'gray.200' : 'gray.800',
                })}
              >
                {category.categoryName}
              </div>
              <div
                className={css({
                  width: '80px',
                  height: '6px',
                  backgroundColor: isDark ? 'gray.700' : 'gray.200',
                  borderRadius: '3px',
                  overflow: 'hidden',
                })}
              >
                <div
                  className={css({
                    height: '100%',
                    // Neutral blue color - not implying skill-level judgment
                    backgroundColor: isDark ? 'blue.400' : 'blue.500',
                    borderRadius: '3px',
                  })}
                  style={{
                    width: `${category.total > 0 ? (category.correct / category.total) * 100 : 0}%`,
                  }}
                />
              </div>
              <div
                className={css({
                  fontSize: '0.75rem',
                  fontWeight: 'bold',
                  color: isDark ? 'blue.400' : 'blue.600',
                  minWidth: '36px',
                  textAlign: 'right',
                })}
              >
                {category.correct}/{category.total}
              </div>
            </summary>

            {/* Individual skills within category (expanded content) */}
            <div
              className={css({
                display: 'flex',
                flexDirection: 'column',
                gap: '0.375rem',
                paddingLeft: '0.75rem',
                paddingTop: '0.5rem',
              })}
            >
              {category.skills.map((skill) => (
                <div
                  key={skill.skillId}
                  data-element="skill-row"
                  className={css({
                    display: 'flex',
                    alignItems: 'center',
                    gap: '0.5rem',
                  })}
                >
                  <div
                    className={css({
                      flex: 1,
                      fontSize: '0.8125rem',
                      color: isDark ? 'gray.400' : 'gray.600',
                    })}
                  >
                    {getSkillDisplayName(skill.skillId)}
                  </div>
                  <div
                    className={css({
                      width: '60px',
                      height: '4px',
                      backgroundColor: isDark ? 'gray.700' : 'gray.200',
                      borderRadius: '2px',
                      overflow: 'hidden',
                    })}
                  >
                    <div
                      className={css({
                        height: '100%',
                        // Neutral blue color - not implying skill-level judgment
                        backgroundColor: isDark ? 'blue.400' : 'blue.500',
                        borderRadius: '2px',
                      })}
                      style={{
                        width: `${skill.total > 0 ? (skill.correct / skill.total) * 100 : 0}%`,
                      }}
                    />
                  </div>
                  <div
                    className={css({
                      fontSize: '0.6875rem',
                      color: isDark ? 'blue.400' : 'blue.600',
                      minWidth: '28px',
                      textAlign: 'right',
                    })}
                  >
                    {skill.correct}/{skill.total}
                  </div>
                </div>
              ))}
            </div>
          </details>
        ))}
      </div>
    </div>
  )
}

/**
 * Calculate skill breakdown grouped by category
 */
function calculateSkillBreakdownByCategory(results: SlotResult[]): SkillCategoryGroup[] {
  // First, collect all skills with their stats
  const skillMap = new Map<string, { correct: number; total: number }>()

  for (const result of results) {
    for (const skillId of result.skillsExercised) {
      const current = skillMap.get(skillId) || { correct: 0, total: 0 }
      current.total++
      if (result.isCorrect) current.correct++
      skillMap.set(skillId, current)
    }
  }

  // Group skills by category
  const categoryMap = new Map<
    string,
    { skills: SkillBreakdown[]; correct: number; total: number }
  >()

  for (const [skillId, stats] of skillMap.entries()) {
    const categoryId = skillId.split('.')[0] || 'other'
    const current = categoryMap.get(categoryId) || {
      skills: [],
      correct: 0,
      total: 0,
    }

    current.skills.push({
      skillId,
      ...stats,
    })
    current.correct += stats.correct
    current.total += stats.total

    categoryMap.set(categoryId, current)
  }

  // Sort categories by pedagogical order, then build result
  const result: SkillCategoryGroup[] = []

  for (const categoryId of CATEGORY_PRIORITY) {
    const categoryData = categoryMap.get(categoryId)
    if (categoryData && categoryData.skills.length > 0) {
      // Sort skills within category by total count (most practiced first)
      categoryData.skills.sort((a, b) => b.total - a.total)

      result.push({
        categoryId,
        categoryName: getCategoryDisplayName(categoryId),
        skills: categoryData.skills,
        correct: categoryData.correct,
        total: categoryData.total,
      })
    }
  }

  // Add any categories not in the predefined order (shouldn't happen, but just in case)
  for (const [categoryId, categoryData] of categoryMap.entries()) {
    if (!CATEGORY_PRIORITY.includes(categoryId as (typeof CATEGORY_PRIORITY)[number])) {
      categoryData.skills.sort((a, b) => b.total - a.total)

      result.push({
        categoryId,
        categoryName: getCategoryDisplayName(categoryId),
        skills: categoryData.skills,
        correct: categoryData.correct,
        total: categoryData.total,
      })
    }
  }

  return result
}

export default SkillsPanel