fix: calculate total problems correctly in preview API
Bug fix: - Preview API was not calculating `total` field (pages × problemsPerPage) - This caused only 1 page to generate even when config specified 4 pages - Now correctly calculates total, rows, and other derived fields Added debug logging: - Log problem count and config in generatePreview - Log Typst sources page count - Log preview page count in shared worksheet viewer - These logs help diagnose multi-page rendering issues This fixes the issue where shared worksheets only showed the first page even when the config specified multiple pages. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -17,7 +17,14 @@ export async function POST(request: Request) {
|
|||||||
return NextResponse.json({ success: false, error: 'Missing config' }, { status: 400 })
|
return NextResponse.json({ success: false, error: 'Missing config' }, { status: 400 })
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add date and seed if missing
|
// Calculate derived state fields
|
||||||
|
const problemsPerPage = config.problemsPerPage ?? 20
|
||||||
|
const pages = config.pages ?? 1
|
||||||
|
const cols = config.cols ?? 5
|
||||||
|
const rows = Math.ceil((problemsPerPage * pages) / cols)
|
||||||
|
const total = problemsPerPage * pages
|
||||||
|
|
||||||
|
// Add date, seed, and derived fields if missing
|
||||||
const fullConfig: WorksheetFormState = {
|
const fullConfig: WorksheetFormState = {
|
||||||
...config,
|
...config,
|
||||||
date:
|
date:
|
||||||
@@ -28,6 +35,8 @@ export async function POST(request: Request) {
|
|||||||
year: 'numeric',
|
year: 'numeric',
|
||||||
}),
|
}),
|
||||||
seed: config.seed || Date.now() % 2147483647,
|
seed: config.seed || Date.now() % 2147483647,
|
||||||
|
rows,
|
||||||
|
total,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate preview
|
// Generate preview
|
||||||
|
|||||||
@@ -115,7 +115,13 @@ export function generateWorksheetPreview(config: WorksheetFormState): PreviewRes
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate Typst sources (one per page)
|
// Generate Typst sources (one per page)
|
||||||
|
console.log('[generatePreview] Generated problems:', problems.length, 'for config:', {
|
||||||
|
pages: validatedConfig.pages,
|
||||||
|
problemsPerPage: validatedConfig.problemsPerPage,
|
||||||
|
total: validatedConfig.total,
|
||||||
|
})
|
||||||
const typstSources = generateTypstSource(validatedConfig, problems)
|
const typstSources = generateTypstSource(validatedConfig, problems)
|
||||||
|
console.log('[generatePreview] Generated Typst sources:', typstSources.length, 'pages')
|
||||||
|
|
||||||
// Compile each page source to SVG (using stdout for single-page output)
|
// Compile each page source to SVG (using stdout for single-page output)
|
||||||
const pages: string[] = []
|
const pages: string[] = []
|
||||||
|
|||||||
Reference in New Issue
Block a user