@@ -156,7 +156,7 @@ export const createQuizSlice: StateCreator<
156156 } ,
157157 resetQuizWithNewData : ( newQuizData : PartialQuizData , quizId : number ) => {
158158 console . log ( '[Quiz] Loading quiz ID:' , quizId , 'Previous quiz ID:' , get ( ) . lastShownQuizId ) ;
159-
159+
160160 get ( ) . stopPassageSpeech ( ) ;
161161 get ( ) . resetQuizState ( ) ;
162162 set ( ( state ) => {
@@ -172,21 +172,33 @@ export const createQuizSlice: StateCreator<
172172 get ( ) . setShowQuestionSection ( true ) ;
173173 get ( ) . setShowExplanation ( false ) ;
174174 get ( ) . setShowContent ( true ) ;
175-
175+
176176 // Prefetch the next quiz in the background for smooth transitions
177177 console . log ( '[Quiz] Starting prefetch for next quiz...' ) ;
178178 void get ( ) . generateText ( true ) ;
179179 } ,
180180 loadNextQuiz : ( ) => {
181181 const nextQuiz = get ( ) . nextQuizAvailable ;
182182 const lastShownId = get ( ) . lastShownQuizId ;
183-
184- console . log ( '[Quiz] loadNextQuiz called. Next quiz available:' , ! ! nextQuiz , 'Next quiz ID:' , nextQuiz ?. quizId , 'Last shown ID:' , lastShownId ) ;
185-
183+ const currentQuizId = get ( ) . currentQuizId ;
184+
185+ console . log (
186+ '[Quiz] loadNextQuiz called. Next quiz available:' ,
187+ ! ! nextQuiz ,
188+ 'Next quiz ID:' ,
189+ nextQuiz ?. quizId ,
190+ 'Last shown ID:' ,
191+ lastShownId ,
192+ 'Current quiz ID:' ,
193+ currentQuizId
194+ ) ;
195+
186196 if ( nextQuiz ) {
187- // Check if the prefetched quiz is the same as the one we just showed
188- if ( nextQuiz . quizId === lastShownId ) {
189- console . warn ( '[Quiz] Prefetched quiz is the same as last shown! Clearing it and generating new quiz.' ) ;
197+ // Check if the prefetched quiz is the same as the one we just showed OR currently showing
198+ if ( nextQuiz . quizId === lastShownId || nextQuiz . quizId === currentQuizId ) {
199+ console . warn (
200+ '[Quiz] Prefetched quiz is the same as last shown or current! Clearing it and generating new quiz.'
201+ ) ;
190202 // Clear the duplicate prefetched quiz to prevent infinite loop
191203 set ( { nextQuizAvailable : null } ) ;
192204 void get ( ) . generateText ( ) ; // Generate a fresh quiz instead
@@ -246,7 +258,7 @@ export const createQuizSlice: StateCreator<
246258 generateText : async ( isPrefetch = false ) : Promise < void > => {
247259 const lastShownId = get ( ) . lastShownQuizId ;
248260 console . log ( '[Quiz] generateText called. isPrefetch:' , isPrefetch , 'lastShownId:' , lastShownId ) ;
249-
261+
250262 if ( ! isPrefetch && get ( ) . nextQuizAvailable ) {
251263 console . log ( '[Quiz] Using loadNextQuiz flow (nextQuizAvailable exists)' ) ;
252264 get ( ) . loadNextQuiz ( ) ;
@@ -261,12 +273,16 @@ export const createQuizSlice: StateCreator<
261273 get ( ) . setShowExplanation ( false ) ;
262274 }
263275 try {
264- console . log ( '[Quiz] Calling generateExerciseResponse...' , 'excludeQuizId:' , lastShownId ) ;
276+ console . log (
277+ '[Quiz] Calling generateExerciseResponse...' ,
278+ 'excludeQuizId:' ,
279+ lastShownId || get ( ) . currentQuizId
280+ ) ;
265281 const response = await generateExerciseResponse ( {
266282 passageLanguage : get ( ) . passageLanguage ,
267283 questionLanguage : get ( ) . generatedQuestionLanguage ,
268284 cefrLevel : get ( ) . cefrLevel ,
269- excludeQuizId : lastShownId ,
285+ excludeQuizId : lastShownId || get ( ) . currentQuizId ,
270286 } ) ;
271287 if ( 'error' in response && response . error ) {
272288 if ( ! isPrefetch ) {
@@ -293,16 +309,16 @@ export const createQuizSlice: StateCreator<
293309 const validatedResponse = parseResult . data ;
294310 const quizData : PartialQuizData = validatedResponse . quizData ;
295311 const newQuizId = validatedResponse . quizId ;
296-
312+
297313 if ( ! quizData . language ) quizData . language = get ( ) . passageLanguage ;
298-
314+
299315 console . log ( '[Quiz] Generated quiz ID:' , newQuizId , 'isPrefetch:' , isPrefetch ) ;
300-
316+
301317 // Check if we accidentally got the same quiz as last shown
302318 if ( newQuizId === lastShownId ) {
303319 console . warn ( '[Quiz] WARNING: Generated quiz has same ID as last shown quiz!' , newQuizId ) ;
304320 }
305-
321+
306322 if ( isPrefetch ) {
307323 console . log ( '[Quiz] Storing prefetched quiz:' , newQuizId ) ;
308324 set ( {
0 commit comments