From f34340de9c9b2cdbccdd639d84895c64226f880a Mon Sep 17 00:00:00 2001 From: Benjamin Admin Date: Tue, 24 Mar 2026 16:33:56 +0100 Subject: [PATCH] Fix sub-session completion flow: navigate to next incomplete sub-session Instead of returning to parent (which creates a redirect loop), the handleNext function now finds the next incomplete sub-session and opens it directly. When all sub-sessions are done, returns to session list. Also fixes openSession auto-redirect to prefer the first incomplete sub-session over the most advanced one. Co-Authored-By: Claude Opus 4.6 --- .../app/(admin)/ai/ocr-pipeline/page.tsx | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/admin-lehrer/app/(admin)/ai/ocr-pipeline/page.tsx b/admin-lehrer/app/(admin)/ai/ocr-pipeline/page.tsx index 51e1301..a573ef1 100644 --- a/admin-lehrer/app/(admin)/ai/ocr-pipeline/page.tsx +++ b/admin-lehrer/app/(admin)/ai/ocr-pipeline/page.tsx @@ -73,12 +73,17 @@ export default function OcrPipelinePage() { if (data.sub_sessions && data.sub_sessions.length > 0) { setSubSessions(data.sub_sessions) setParentSessionId(sid) - // Parent has sub-sessions — open the most advanced one automatically - const sorted = [...data.sub_sessions].sort( - (a: SubSession, b: SubSession) => (b.current_step || 0) - (a.current_step || 0), + // Parent has sub-sessions — open the first incomplete one (or most advanced if all done) + const incomplete = data.sub_sessions.find( + (s: SubSession) => !s.current_step || s.current_step < 10, ) - openSession(sorted[0].id, true) - return + const target = incomplete || [...data.sub_sessions].sort( + (a: SubSession, b: SubSession) => (b.current_step || 0) - (a.current_step || 0), + )[0] + if (target) { + openSession(target.id, true) + return + } } else if (data.parent_session_id) { // This is a sub-session — keep parent info but don't reset sub-session list setParentSessionId(data.parent_session_id) @@ -217,12 +222,28 @@ export default function OcrPipelinePage() { if (currentStep >= steps.length - 1) { // Last step completed if (parentSessionId && sessionId !== parentSessionId) { - // Sub-session completed — update its status and stay in tab view - setSubSessions((prev) => - prev.map((s) => s.id === sessionId ? { ...s, status: 'completed', current_step: 10 } : s) + // Sub-session completed — mark it and find next incomplete one + const updatedSubs = subSessions.map((s) => + s.id === sessionId ? { ...s, status: 'completed' as const, current_step: 10 } : s, ) - // Switch back to parent - handleSessionChange(parentSessionId) + setSubSessions(updatedSubs) + + // Find next incomplete sub-session + const nextIncomplete = updatedSubs.find( + (s) => s.id !== sessionId && (!s.current_step || s.current_step < 10), + ) + if (nextIncomplete) { + // Open next incomplete sub-session + openSession(nextIncomplete.id, true) + } else { + // All sub-sessions done — return to session list + setSteps(PIPELINE_STEPS.map((s, i) => ({ ...s, status: i === 0 ? 'active' : 'pending' }))) + setCurrentStep(0) + setSessionId(null) + setSubSessions([]) + setParentSessionId(null) + loadSessions() + } return } // Main session: return to session list