Skip to content

Commit 3fe9cc5

Browse files
fix: remove redundant requests
1 parent 156ae04 commit 3fe9cc5

File tree

2 files changed

+12
-64
lines changed

2 files changed

+12
-64
lines changed

src/hooks/useAnalysisController/useAnalysisController.ts

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,8 @@ import { StockfishEngineContext } from 'src/contexts/StockfishEngineContext'
2222
import {
2323
collectEngineAnalysisData,
2424
generateAnalysisCacheKey,
25-
applyEngineAnalysisData,
2625
} from 'src/lib/analysisStorage'
27-
import {
28-
storeEngineAnalysis,
29-
getEngineAnalysis,
30-
} from 'src/api/analysis/analysis'
26+
import { storeEngineAnalysis } from 'src/api/analysis/analysis'
3127

3228
export interface GameAnalysisProgress {
3329
currentMoveIndex: number
@@ -146,80 +142,31 @@ export const useAnalysisController = (
146142
const saveAnalysisToBackendRef = useRef(saveAnalysisToBackend)
147143
saveAnalysisToBackendRef.current = saveAnalysisToBackend
148144

149-
const loadStoredAnalysis = useCallback(async () => {
150-
console.log(
151-
'loadStoredAnalysis called for game:',
152-
game.id,
153-
'type:',
154-
game.type,
155-
)
156-
157-
if (
158-
!game.id ||
159-
game.type === 'custom-pgn' ||
160-
game.type === 'custom-fen' ||
161-
game.type === 'tournament'
162-
) {
163-
console.log('Skipping analysis load - game not eligible')
164-
return
165-
}
166-
167-
try {
168-
console.log('Fetching stored analysis for game:', game.id)
169-
const storedAnalysis = await getEngineAnalysis(game.id)
170-
console.log('Received stored analysis:', storedAnalysis)
171-
172-
if (storedAnalysis && storedAnalysis.positions.length > 0) {
173-
applyEngineAnalysisData(game.tree, storedAnalysis.positions)
174-
setAnalysisState((prev) => prev + 1) // Trigger UI updates
175-
console.log(
176-
'Loaded stored analysis:',
177-
storedAnalysis.positions.length,
178-
'positions',
179-
)
180-
} else {
181-
console.log('No stored analysis found for game:', game.id)
182-
}
183-
} catch (error) {
184-
console.warn('Failed to load stored analysis:', error)
185-
// Don't show error to user as this is background functionality
186-
}
187-
}, [game.id, game.type])
188-
189-
// Load stored analysis when game changes
190145
useEffect(() => {
191-
// Reset states for new game
192146
setHasUnsavedAnalysis(false)
193147
setIsAutoSaving(false)
194148
setLastSavedCacheKey('')
195-
loadStoredAnalysis()
196-
}, [loadStoredAnalysis])
149+
}, [game.id, game.type])
197150

198-
// Mark analysis as unsaved when new analysis comes in
199151
useEffect(() => {
200152
if (analysisState > 0) {
201153
setHasUnsavedAnalysis(true)
202154
}
203155
}, [analysisState])
204156

205-
// Setup auto-save timer
206157
useEffect(() => {
207-
// Clear existing timer
208158
if (autoSaveTimerRef.current) {
209159
clearInterval(autoSaveTimerRef.current)
210160
}
211161

212-
// Set up new timer to save every 10 seconds
213162
autoSaveTimerRef.current = setInterval(() => {
214163
saveAnalysisToBackendRef.current()
215164
}, 10000)
216165

217-
// Cleanup on unmount or game change - save one last time
218166
return () => {
219167
if (autoSaveTimerRef.current) {
220168
clearInterval(autoSaveTimerRef.current)
221169
}
222-
// Final save when component unmounts or game changes
223170
saveAnalysisToBackendRef.current()
224171
}
225172
}, [game.id])
@@ -505,15 +452,14 @@ export const useAnalysisController = (
505452
resetProgress: resetGameAnalysisProgress,
506453
isEnginesReady: stockfish.isReady() && maia.status === 'ready',
507454
saveAnalysis: saveAnalysisToBackend,
508-
loadStoredAnalysis,
509455
autoSave: {
510456
hasUnsavedChanges: hasUnsavedAnalysis,
511457
isSaving: isAutoSaving,
512458
status: isAutoSaving
513-
? 'saving'
459+
? ('saving' as const)
514460
: hasUnsavedAnalysis
515-
? 'unsaved'
516-
: 'saved',
461+
? ('unsaved' as const)
462+
: ('saved' as const),
517463
},
518464
},
519465
}

src/pages/analysis/[...id].tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,20 @@ const AnalysisPage: NextPage = () => {
7777
}, [initialTourCheck, startTour, tourState.ready])
7878
const [currentId, setCurrentId] = useState<string[]>(id as string[])
7979

80-
// Helper function to load and apply stored analysis
8180
const loadStoredAnalysis = useCallback(async (game: AnalyzedGame) => {
82-
// Only load for games that have a proper ID (not custom local games)
83-
if (!game.id || game.type === 'custom-pgn' || game.type === 'custom-fen') {
81+
if (
82+
!game.id ||
83+
game.type === 'custom-pgn' ||
84+
game.type === 'custom-fen' ||
85+
game.type === 'tournament'
86+
) {
8487
return
8588
}
8689

8790
try {
8891
const storedAnalysis = await getEngineAnalysis(game.id)
8992
if (storedAnalysis && storedAnalysis.positions.length > 0) {
90-
applyEngineAnalysisData(game.tree, storedAnalysis)
93+
applyEngineAnalysisData(game.tree, storedAnalysis.positions)
9194
console.log(
9295
'Loaded stored analysis:',
9396
storedAnalysis.positions.length,
@@ -96,7 +99,6 @@ const AnalysisPage: NextPage = () => {
9699
}
97100
} catch (error) {
98101
console.warn('Failed to load stored analysis:', error)
99-
// Don't show error to user as this is background functionality
100102
}
101103
}, [])
102104

0 commit comments

Comments
 (0)