@@ -79,26 +79,26 @@ export const useAnalysisController = (
7979 currentNode : null ,
8080 } )
8181
82- // Auto-save analysis state
8382 const [ lastSavedCacheKey , setLastSavedCacheKey ] = useState < string > ( '' )
8483 const autoSaveTimerRef = useRef < NodeJS . Timeout | null > ( null )
8584
86- // Auto-save analysis data every 10 seconds
8785 const saveAnalysisToBackend = useCallback ( async ( ) => {
88- // Only save for games that have a proper ID (not custom local games)
89- if ( ! game . id || game . type === 'custom-pgn' || game . type === 'custom-fen' ) {
86+ if (
87+ ! game . id ||
88+ game . type === 'custom-pgn' ||
89+ game . type === 'custom-fen' ||
90+ game . type === 'tournament'
91+ ) {
9092 return
9193 }
9294
9395 try {
9496 const analysisData = collectEngineAnalysisData ( game . tree )
9597
96- // Only save if there's actually some analysis to save
9798 if ( analysisData . length === 0 ) {
9899 return
99100 }
100101
101- // Check if we have meaningful analysis (decent depth or Maia data)
102102 const hasMeaningfulAnalysis = analysisData . some (
103103 ( pos ) => ( pos . stockfish && pos . stockfish . depth >= 12 ) || pos . maia ,
104104 )
@@ -107,10 +107,9 @@ export const useAnalysisController = (
107107 return
108108 }
109109
110- // Generate a cache key to avoid unnecessary saves
111110 const cacheKey = generateAnalysisCacheKey ( analysisData )
112111 if ( cacheKey === lastSavedCacheKey ) {
113- return // No new analysis since last save
112+ return
114113 }
115114
116115 await storeEngineAnalysis ( game . id , analysisData )
@@ -126,6 +125,9 @@ export const useAnalysisController = (
126125 }
127126 } , [ game . id , game . type , game . tree , lastSavedCacheKey ] )
128127
128+ const saveAnalysisToBackendRef = useRef ( saveAnalysisToBackend )
129+ saveAnalysisToBackendRef . current = saveAnalysisToBackend
130+
129131 // Setup auto-save timer
130132 useEffect ( ( ) => {
131133 // Clear existing timer
@@ -134,24 +136,19 @@ export const useAnalysisController = (
134136 }
135137
136138 // Set up new timer to save every 10 seconds
137- autoSaveTimerRef . current = setInterval ( saveAnalysisToBackend , 10000 )
139+ autoSaveTimerRef . current = setInterval ( ( ) => {
140+ saveAnalysisToBackendRef . current ( )
141+ } , 10000 )
138142
139143 // Cleanup on unmount or game change - save one last time
140144 return ( ) => {
141145 if ( autoSaveTimerRef . current ) {
142146 clearInterval ( autoSaveTimerRef . current )
143147 }
144148 // Final save when component unmounts or game changes
145- saveAnalysisToBackend ( )
149+ saveAnalysisToBackendRef . current ( )
146150 }
147- } , [ saveAnalysisToBackend ] )
148-
149- // Save immediately when analysis state changes (new analysis available)
150- useEffect ( ( ) => {
151- // Debounce the save to avoid too frequent calls
152- const timeoutId = setTimeout ( saveAnalysisToBackend , 1000 )
153- return ( ) => clearTimeout ( timeoutId )
154- } , [ analysisState , saveAnalysisToBackend ] )
151+ } , [ game . id ] )
155152
156153 // Simple batch analysis functions that reuse existing analysis infrastructure
157154 const startGameAnalysis = useCallback (
0 commit comments