|
1 | | -import React, { useEffect, useState, useCallback } from 'react'; |
| 1 | +import React, { useEffect, useState, useCallback, useRef } from 'react'; |
2 | 2 | import { ArrowLeft, BarChart3, Zap } from 'lucide-react'; |
3 | 3 | import { Link, useLocation } from 'react-router-dom'; |
4 | 4 | import QuestionCard from '../components/practice/QuestionCard'; |
@@ -55,6 +55,10 @@ const PracticePage: React.FC = () => { |
55 | 55 | }); |
56 | 56 | const [isTransitioning, setIsTransitioning] = useState(false); |
57 | 57 |
|
| 58 | + // Use ref to track previous filters for accurate change detection |
| 59 | + const prevFiltersRef = useRef(filters); |
| 60 | + const isFirstRender = useRef(true); |
| 61 | + |
58 | 62 | // Apply filters with progress data for bookmarks |
59 | 63 | const filteredQuestions = React.useMemo(() => { |
60 | 64 | return questions.filter(question => { |
@@ -211,10 +215,21 @@ const PracticePage: React.FC = () => { |
211 | 215 | } |
212 | 216 | }, [displayQuestions.length, currentQuestionIndex]); |
213 | 217 |
|
| 218 | + // Only reset when filters actually change (not on initial mount) |
214 | 219 | useEffect(() => { |
215 | | - // Reset to first question when filters change (but not when recommended questions change) |
216 | | - setCurrentQuestionIndex(0); |
217 | | - setShowExplanation(false); |
| 220 | + if (isFirstRender.current) { |
| 221 | + isFirstRender.current = false; |
| 222 | + prevFiltersRef.current = filters; |
| 223 | + return; |
| 224 | + } |
| 225 | + |
| 226 | + // Check if filters have actually changed |
| 227 | + const filtersChanged = JSON.stringify(prevFiltersRef.current) !== JSON.stringify(filters); |
| 228 | + if (filtersChanged) { |
| 229 | + setCurrentQuestionIndex(0); |
| 230 | + setShowExplanation(false); |
| 231 | + prevFiltersRef.current = filters; |
| 232 | + } |
218 | 233 | }, [filters]); |
219 | 234 |
|
220 | 235 | // Add keyboard shortcuts for navigation |
|
0 commit comments