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 | // Validation logic for worksheet configuration import type { WorksheetFormState, WorksheetConfig, ValidationResult, } from '@/app/create/worksheets/types' import type { DisplayRules } from './displayRules' import { getSkillById } from './skills' import { WORKSHEET_LIMITS } from './constants/validation' /** * Get current date formatted as "Month Day, Year" */ function getDefaultDate(): string { const now = new Date() return now.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric', }) } /** * Merge user display rules with skill recommendations, resolving "auto" values * * For each display rule field: * - If user value is "auto" → use skill's recommendation * - If user value is undefined → use skill's recommendation * - Otherwise → use user's explicit value (manual override) * * @param skillRules - The skill's recommended scaffolding settings * @param userRules - The user's custom scaffolding settings (may contain "auto") * @returns Fully resolved display rules with no "auto" values */ function mergeDisplayRulesWithAuto( skillRules: DisplayRules, userRules: Partial<DisplayRules> ): DisplayRules { const result: Record<string, any> = {} for (const key of Object.keys(skillRules) as Array<keyof DisplayRules>) { const userValue = userRules[key] // If user value is "auto" or undefined, use skill's recommendation // Otherwise, use user's explicit value (manual override) result[key] = userValue === 'auto' || userValue === undefined ? skillRules[key] : userValue } return result as DisplayRules } /** * Validate and create complete config from partial form state */ export function validateWorksheetConfig(formState: WorksheetFormState): ValidationResult { const errors: string[] = [] // Validate cols first (needed for rows calculation) const cols = formState.cols ?? 4 if (cols < 1 || cols > WORKSHEET_LIMITS.MAX_COLS) { errors.push(`Columns must be between 1 and ${WORKSHEET_LIMITS.MAX_COLS}`) } // ======================================== // PRIMARY STATE → DERIVED STATE // ======================================== // // This section demonstrates the core principle of our config persistence: // // PRIMARY STATE (saved, source of truth): // - problemsPerPage: How many problems per page (e.g., 20) // - pages: How many pages (e.g., 5) // // DERIVED STATE (calculated, never saved): // - total = problemsPerPage × pages (e.g., 100) // - rows = Math.ceil(problemsPerPage / cols) (e.g., 5) // // Why this matters: // - When sharing worksheets, we only save PRIMARY state // - When loading shared worksheets, we MUST calculate DERIVED state // - Never use formState.total as fallback - it may be missing for shared worksheets! // - See .claude/WORKSHEET_CONFIG_PERSISTENCE.md for full architecture // // Example bug that was fixed (2025-01): // - Shared 100-page worksheet // - formState.total was missing (correctly excluded from share) // - Old code: total = formState.total ?? 20 (WRONG!) // - Result: Generated only 20 problems → 1 page instead of 100 // - Fix: total = problemsPerPage × pages (from PRIMARY state) // // Get primary state values (source of truth for calculation) const problemsPerPage = formState.problemsPerPage ?? formState.total ?? 20 const pages = formState.pages ?? 1 // Calculate derived state: total = problemsPerPage × pages // DO NOT use formState.total as source of truth - it may be missing! const total = problemsPerPage * pages console.log('[validateWorksheetConfig] PRIMARY → DERIVED state:', { // Primary (source of truth) problemsPerPage, pages, // Derived (calculated) total, // Debug: check if formState had these values hadTotal: formState.total !== undefined, totalMatches: formState.total === total, }) if (total < 1 || total > WORKSHEET_LIMITS.MAX_TOTAL_PROBLEMS) { errors.push(`Total problems must be between 1 and ${WORKSHEET_LIMITS.MAX_TOTAL_PROBLEMS}`) } // Calculate derived state: rows based on problemsPerPage and cols const rows = Math.ceil(problemsPerPage / cols) // Validate probabilities (0-1 range) // CRITICAL: Must check for undefined/null explicitly, not use ?? operator // because 0 is a valid value (e.g., "no regrouping" skills set pAnyStart=0) const pAnyStart = formState.pAnyStart !== undefined && formState.pAnyStart !== null ? formState.pAnyStart : 0.75 const pAllStart = formState.pAllStart !== undefined && formState.pAllStart !== null ? formState.pAllStart : 0.25 if (pAnyStart < 0 || pAnyStart > 1) { errors.push('pAnyStart must be between 0 and 1') } if (pAllStart < 0 || pAllStart > 1) { errors.push('pAllStart must be between 0 and 1') } if (pAllStart > pAnyStart) { errors.push('pAllStart cannot be greater than pAnyStart') } // Validate fontSize const fontSize = formState.fontSize ?? 16 if (fontSize < WORKSHEET_LIMITS.FONT_SIZE.MIN || fontSize > WORKSHEET_LIMITS.FONT_SIZE.MAX) { errors.push( `Font size must be between ${WORKSHEET_LIMITS.FONT_SIZE.MIN} and ${WORKSHEET_LIMITS.FONT_SIZE.MAX}` ) } // V4: Validate digitRange (min and max must be 1-5, min <= max) // Note: Same range applies to both addition and subtraction const digitRange = formState.digitRange ?? { min: 2, max: 2 } if ( !digitRange.min || digitRange.min < WORKSHEET_LIMITS.DIGIT_RANGE.MIN || digitRange.min > WORKSHEET_LIMITS.DIGIT_RANGE.MAX ) { errors.push( `Digit range min must be between ${WORKSHEET_LIMITS.DIGIT_RANGE.MIN} and ${WORKSHEET_LIMITS.DIGIT_RANGE.MAX}` ) } if ( !digitRange.max || digitRange.max < WORKSHEET_LIMITS.DIGIT_RANGE.MIN || digitRange.max > WORKSHEET_LIMITS.DIGIT_RANGE.MAX ) { errors.push( `Digit range max must be between ${WORKSHEET_LIMITS.DIGIT_RANGE.MIN} and ${WORKSHEET_LIMITS.DIGIT_RANGE.MAX}` ) } if (digitRange.min > digitRange.max) { errors.push('Digit range min cannot be greater than max') } // V4: Validate operator (addition, subtraction, or mixed) const operator = formState.operator ?? 'addition' if (!['addition', 'subtraction', 'mixed'].includes(operator)) { errors.push('Operator must be "addition", "subtraction", or "mixed"') } // Validate seed (must be positive integer) const seed = formState.seed ?? Date.now() % 2147483647 if (!Number.isInteger(seed) || seed < 0) { errors.push('Seed must be a non-negative integer') } if (errors.length > 0) { return { isValid: false, errors } } // Determine orientation based on columns (portrait = 2-3 cols, landscape = 4-5 cols) const orientation = formState.orientation || (cols <= 3 ? 'portrait' : 'landscape') // Determine mode (default to 'custom' if not specified) const mode: 'custom' | 'manual' | 'mastery' = formState.mode ?? 'custom' // Shared fields for both modes const sharedFields = { // Primary state problemsPerPage, cols, pages, orientation, // Derived state total, rows, // Other fields name: formState.name?.trim() || 'Student', date: formState.date?.trim() || getDefaultDate(), pAnyStart, pAllStart, // Default interpolate based on mode: true for custom/manual, false for mastery interpolate: formState.interpolate !== undefined ? formState.interpolate : mode === 'mastery' ? false : true, // V4: Digit range for problem generation digitRange, // V4: Operator selection (addition, subtraction, or mixed) operator: formState.operator ?? 'addition', // Layout page: { wIn: orientation === 'portrait' ? 8.5 : 11, hIn: orientation === 'portrait' ? 11 : 8.5, }, margins: { left: 0.6, right: 0.6, top: 1.1, bottom: 0.7, }, fontSize, seed, prngAlgorithm: formState.prngAlgorithm ?? 'mulberry32', // V4: Include answer key pages at end of PDF includeAnswerKey: formState.includeAnswerKey ?? false, // V4: Include QR code linking to shared worksheet on each page includeQRCode: formState.includeQRCode ?? false, } // Build mode-specific config let config: WorksheetConfig if (mode === 'custom' || mode === 'mastery') { // Custom & Mastery modes: Use displayRules for conditional scaffolding // Default display rules let baseDisplayRules: DisplayRules = { carryBoxes: 'whenRegrouping', answerBoxes: 'always', placeValueColors: 'always', tenFrames: 'whenRegrouping', problemNumbers: 'always', cellBorders: 'always', borrowNotation: 'whenRegrouping', // Subtraction: show when borrowing borrowingHints: 'never', // Subtraction: no hints by default } // Mastery mode: Apply recommendedScaffolding from current skill(s) if (mode === 'mastery') { const operator = formState.operator ?? 'addition' if (operator === 'mixed') { // Mixed mode: Store SEPARATE display rules for each operator // The typstGenerator will choose which rules to apply per-problem const addSkillId = formState.currentAdditionSkillId const subSkillId = formState.currentSubtractionSkillId if (addSkillId && subSkillId) { const addSkill = getSkillById(addSkillId as any) const subSkill = getSkillById(subSkillId as any) if (addSkill?.recommendedScaffolding && subSkill?.recommendedScaffolding) { // Store both separately - will be used per-problem in typstGenerator // Note: This will be added to the config below as additionDisplayRules/subtractionDisplayRules } } } else { // Single operator: Use its recommendedScaffolding const skillId = operator === 'addition' ? formState.currentAdditionSkillId : formState.currentSubtractionSkillId if (skillId) { const skill = getSkillById(skillId as any) if (skill?.recommendedScaffolding) { baseDisplayRules = { ...skill.recommendedScaffolding } } } } } // Merge user's display rules with skill recommendations, resolving "auto" values const userDisplayRules = (formState.displayRules as any) ?? {} const displayRules: DisplayRules = mode === 'mastery' ? mergeDisplayRulesWithAuto(baseDisplayRules, userDisplayRules) : { ...baseDisplayRules, ...userDisplayRules, // Custom mode: direct override (no "auto" resolution) } console.log('[MASTERY MODE] Display rules resolved:', { mode, baseDisplayRules, userDisplayRules, resolvedDisplayRules: displayRules, }) // Build config with operator-specific display rules for mixed mode const operator = formState.operator ?? 'addition' const baseConfig = { version: 4, mode: mode as 'custom' | 'mastery', // Preserve the actual mode displayRules, difficultyProfile: formState.difficultyProfile, currentStepId: formState.currentStepId, // Mastery progression tracking ...sharedFields, } // Add operator-specific display rules for mastery+mixed mode if (mode === 'mastery' && operator === 'mixed') { const addSkillId = formState.currentAdditionSkillId const subSkillId = formState.currentSubtractionSkillId if (addSkillId && subSkillId) { const addSkill = getSkillById(addSkillId as any) const subSkill = getSkillById(subSkillId as any) if (addSkill?.recommendedScaffolding && subSkill?.recommendedScaffolding) { // Merge user's operator-specific displayRules with skill's recommended scaffolding // Resolves "auto" values to skill recommendations // Falls back to general displayRules if operator-specific rules don't exist const userAdditionRules: Partial<DisplayRules> = (formState as any).additionDisplayRules || formState.displayRules || {} const userSubtractionRules: Partial<DisplayRules> = (formState as any).subtractionDisplayRules || formState.displayRules || {} console.log('[MIXED MODE SCAFFOLDING] User rules (may contain "auto"):', { additionRules: userAdditionRules, subtractionRules: userSubtractionRules, generalRules: formState.displayRules, }) // Resolve "auto" values to skill recommendations const resolvedAdditionRules = mergeDisplayRulesWithAuto( addSkill.recommendedScaffolding, userAdditionRules ) const resolvedSubtractionRules = mergeDisplayRulesWithAuto( subSkill.recommendedScaffolding, userSubtractionRules ) config = { ...baseConfig, additionDisplayRules: resolvedAdditionRules, subtractionDisplayRules: resolvedSubtractionRules, } as any console.log('[MIXED MODE SCAFFOLDING] Final config (after resolving "auto"):', { additionDisplayRules: (config as any).additionDisplayRules, subtractionDisplayRules: (config as any).subtractionDisplayRules, }) } else { console.log('[MIXED MODE SCAFFOLDING] Missing recommendedScaffolding', { addSkill: addSkill?.name, hasAddScaffolding: !!addSkill?.recommendedScaffolding, subSkill: subSkill?.name, hasSubScaffolding: !!subSkill?.recommendedScaffolding, }) config = baseConfig as any } } else { config = baseConfig as any } } else { config = baseConfig as any } } else { // Manual mode: Use displayRules (same as Custom/Mastery) const displayRules: DisplayRules = formState.displayRules ?? { carryBoxes: 'always', answerBoxes: 'always', placeValueColors: 'always', tenFrames: 'never', problemNumbers: 'always', cellBorders: 'always', borrowNotation: 'always', borrowingHints: 'never', } config = { version: 4, mode: 'manual', displayRules, manualPreset: formState.manualPreset, ...sharedFields, } } return { isValid: true, config } } |