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 | /** * Browse Mode View Component * * Allows browsing through all problems in a session during practice. * Shows problems using DetailedProblemCard. * Navigation is handled via SessionProgressIndicator in the nav bar. * Does not affect actual session progress - just for viewing. */ 'use client' import { useMemo, useState } from 'react' import { useTheme } from '@/contexts/ThemeContext' import type { ProblemSlot, SessionPart, SessionPlan, SlotResult } from '@/db/schema/session-plans' import { css } from '../../../styled-system/css' import { AttemptHistoryPanel } from './AttemptHistoryPanel' import { calculateAutoPauseInfo } from './autoPauseCalculator' import { DetailedProblemCard } from './DetailedProblemCard' import { PracticePreview } from './PracticePreview' /** * Flattened problem item with all context needed for display */ export interface LinearProblemItem { partNumber: number slotIndex: number slot: ProblemSlot part: SessionPart linearIndex: number } /** * Build a flattened list of all problems for navigation */ export function buildLinearProblemList(parts: SessionPart[]): LinearProblemItem[] { const items: LinearProblemItem[] = [] let linearIndex = 0 for (const part of parts) { for (let slotIndex = 0; slotIndex < part.slots.length; slotIndex++) { items.push({ partNumber: part.partNumber, slotIndex, slot: part.slots[slotIndex], part, linearIndex, }) linearIndex++ } } return items } /** * Convert current part/slot indices to linear index */ export function getLinearIndex( parts: SessionPart[], currentPartIndex: number, currentSlotIndex: number ): number { let index = 0 for (let i = 0; i < currentPartIndex; i++) { index += parts[i].slots.length } return index + currentSlotIndex } export interface BrowseModeViewProps { /** The session plan with all problems */ plan: SessionPlan /** Current browse index (linear) */ browseIndex: number /** The actual current practice problem index (to highlight) */ currentPracticeIndex: number /** Student ID for edit API calls */ studentId: string /** Callback when results are edited (to refetch plan) */ onResultEdited?: () => void } /** * Get the most recent result for a specific problem if it exists. * Returns the last matching result because results are appended chronologically, * so the last match is the most recent (e.g., a redo after the original). */ function getResultForProblem( results: SlotResult[], partNumber: number, slotIndex: number ): SlotResult | undefined { // Iterate from end to find most recent result for this slot for (let i = results.length - 1; i >= 0; i--) { const r = results[i] if (r.partNumber === partNumber && r.slotIndex === slotIndex) { return r } } return undefined } /** * Result with its global index in the plan.results array */ export interface ResultWithGlobalIndex { result: SlotResult globalIndex: number } /** * Get ALL results for a specific slot (including retries) with their global indices * Results are matched by partNumber and originalSlotIndex (or slotIndex for non-retries) */ function getAllResultsForSlot( results: SlotResult[], partNumber: number, slotIndex: number ): ResultWithGlobalIndex[] { const matches: ResultWithGlobalIndex[] = [] for (let i = 0; i < results.length; i++) { const r = results[i] if (r.partNumber === partNumber && (r.originalSlotIndex ?? r.slotIndex) === slotIndex) { matches.push({ result: r, globalIndex: i }) } } return matches } export function BrowseModeView({ plan, browseIndex, currentPracticeIndex, studentId, onResultEdited, }: BrowseModeViewProps) { const { resolvedTheme } = useTheme() const isDark = resolvedTheme === 'dark' // Practice preview mode - when true, show interactive practice interface const [isPracticing, setIsPracticing] = useState(false) // Build linear problem list const linearProblems = useMemo(() => buildLinearProblemList(plan.parts), [plan.parts]) const currentItem = linearProblems[browseIndex] // Get result for current browse item (for DetailedProblemCard display) const result = useMemo(() => { if (!currentItem) return undefined return getResultForProblem(plan.results, currentItem.partNumber, currentItem.slotIndex) }, [plan.results, currentItem]) // Get ALL results for current slot (including retries) for AttemptHistoryPanel const allResults = useMemo(() => { if (!currentItem) return [] return getAllResultsForSlot(plan.results, currentItem.partNumber, currentItem.slotIndex) }, [plan.results, currentItem]) // Calculate auto-pause stats at this position const autoPauseStats = useMemo(() => { if (!currentItem) return undefined // Find the position in results where this problem would be const resultsUpToHere = plan.results.filter((r) => { const rLinear = linearProblems.findIndex( (p) => p.partNumber === r.partNumber && p.slotIndex === r.slotIndex ) return rLinear < browseIndex }) return calculateAutoPauseInfo(resultsUpToHere).stats }, [plan.results, linearProblems, browseIndex, currentItem]) // Is this the current practice problem? const isCurrentPractice = browseIndex === currentPracticeIndex const isCompleted = browseIndex < currentPracticeIndex const isUpcoming = browseIndex > currentPracticeIndex if (!currentItem) { return ( <div className={css({ padding: '2rem', textAlign: 'center', color: isDark ? 'gray.400' : 'gray.600', })} > No problems to display </div> ) } return ( <div data-component="browse-mode-view" data-browse-index={browseIndex} className={css({ display: 'flex', flexDirection: 'column', gap: '1rem', padding: '1rem', maxWidth: '800px', margin: '0 auto', })} > {/* Current Practice Indicator */} {isCurrentPractice && ( <div className={css({ padding: '0.5rem 1rem', backgroundColor: isDark ? 'yellow.900' : 'yellow.50', borderRadius: '6px', border: '1px solid', borderColor: isDark ? 'yellow.700' : 'yellow.200', textAlign: 'center', fontSize: '0.875rem', fontWeight: 'bold', color: isDark ? 'yellow.200' : 'yellow.700', })} > This is your current practice problem </div> )} {/* Problem Display */} <DetailedProblemCard slot={currentItem.slot} part={currentItem.part} result={result} autoPauseStats={autoPauseStats} isDark={isDark} problemNumber={browseIndex + 1} /> {/* Action Button - Toggle practice mode */} <div data-element="browse-action" className={css({ display: 'flex', justifyContent: 'center', gap: '0.75rem', padding: '0.5rem 0', })} > <button type="button" data-action={isPracticing ? 'close-practice' : 'practice-this-problem'} onClick={() => setIsPracticing((prev) => !prev)} className={css({ padding: '0.75rem 1.5rem', fontSize: '1rem', fontWeight: 'bold', borderRadius: '8px', border: isPracticing ? '2px solid' : 'none', borderColor: isPracticing ? (isDark ? 'gray.500' : 'gray.400') : undefined, cursor: 'pointer', backgroundColor: isPracticing ? 'transparent' : isDark ? 'green.600' : 'green.500', color: isPracticing ? (isDark ? 'gray.300' : 'gray.600') : 'white', transition: 'all 0.15s ease', _hover: { backgroundColor: isPracticing ? isDark ? 'gray.700' : 'gray.100' : isDark ? 'green.500' : 'green.600', transform: 'scale(1.02)', }, _active: { transform: 'scale(0.98)', }, })} > {isPracticing ? 'Close Practice Panel' : 'Practice This Problem'} </button> </div> {/* Inline Practice Preview - shown when practicing */} {isPracticing && ( <div data-element="practice-panel" className={css({ padding: '1rem', backgroundColor: isDark ? 'blue.950' : 'blue.50', borderRadius: '12px', border: '2px solid', borderColor: isDark ? 'blue.800' : 'blue.200', })} > <div className={css({ display: 'flex', alignItems: 'center', gap: '0.5rem', marginBottom: '1rem', paddingBottom: '0.75rem', borderBottom: '1px solid', borderColor: isDark ? 'blue.800' : 'blue.200', })} > <span className={css({ fontSize: '0.875rem', fontWeight: 'bold', color: isDark ? 'blue.200' : 'blue.700', })} > Practice Panel </span> <span className={css({ fontSize: '0.75rem', color: isDark ? 'blue.400' : 'blue.500', })} > (does not affect session) </span> </div> <PracticePreview slot={currentItem.slot} part={currentItem.part} problemNumber={browseIndex + 1} onBack={() => setIsPracticing(false)} inline /> </div> )} {/* Attempt History Panel - show all attempts with edit options */} {allResults.length > 0 && currentItem.slot.problem && ( <AttemptHistoryPanel results={allResults} correctAnswer={currentItem.slot.problem.answer} isDark={isDark} studentId={studentId} planId={plan.id} onResultEdited={onResultEdited} /> )} {/* Status indicator */} {(isCompleted || isUpcoming || isCurrentPractice) && ( <div data-element="status-indicator" className={css({ textAlign: 'center', fontSize: '0.75rem', color: isDark ? 'gray.500' : 'gray.500', })} > {isCurrentPractice && '(Current problem in session)'} {isCompleted && '(Already completed in session)'} {isUpcoming && '(Not yet reached in session)'} </div> )} </div> ) } |