Explorar o código

Fix cache progress splash screen not showing

Changed cache progress WebSocket to stay connected continuously
instead of only connecting after an initial check. This ensures
the splash screen appears immediately when cache generation starts
or is already in progress when the page loads.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
tuanchris hai 3 semanas
pai
achega
0e813bc8dd
Modificáronse 1 ficheiros con 22 adicións e 38 borrados
  1. 22 38
      frontend/src/components/layout/Layout.tsx

+ 22 - 38
frontend/src/components/layout/Layout.tsx

@@ -421,24 +421,11 @@ export function Layout() {
     return () => clearInterval(interval)
     return () => clearInterval(interval)
   }, [isBackendConnected])
   }, [isBackendConnected])
 
 
-  // Cache progress WebSocket connection
+  // Cache progress WebSocket connection - always connected to monitor cache generation
   useEffect(() => {
   useEffect(() => {
     if (!isBackendConnected) return
     if (!isBackendConnected) return
 
 
-    // Check initial cache progress
-    const checkCacheProgress = () => {
-      fetch('/cache-progress')
-        .then((r) => r.json())
-        .then((data) => {
-          if (data.is_running) {
-            setCacheProgress(data)
-            connectCacheWebSocket()
-          } else if (data.stage === 'complete' || !data.is_running) {
-            setCacheProgress(null)
-          }
-        })
-        .catch(() => {})
-    }
+    let reconnectTimeout: ReturnType<typeof setTimeout> | null = null
 
 
     const connectCacheWebSocket = () => {
     const connectCacheWebSocket = () => {
       if (cacheWsRef.current) return
       if (cacheWsRef.current) return
@@ -451,19 +438,22 @@ export function Layout() {
           const message = JSON.parse(event.data)
           const message = JSON.parse(event.data)
           if (message.type === 'cache_progress') {
           if (message.type === 'cache_progress') {
             const data = message.data
             const data = message.data
-            if (!data.is_running && (data.stage === 'complete' || data.stage === 'error')) {
-              setCacheProgress(null)
-              ws.close()
-              cacheWsRef.current = null
-              // Show cache all prompt if not already shown
-              if (data.stage === 'complete') {
+            if (data.is_running) {
+              // Cache generation is running - show splash screen
+              setCacheProgress(data)
+            } else if (data.stage === 'complete') {
+              // Cache generation just completed
+              if (cacheProgress?.is_running) {
+                // Was running before, now complete - show cache all prompt
                 const promptShown = localStorage.getItem('cacheAllPromptShown')
                 const promptShown = localStorage.getItem('cacheAllPromptShown')
                 if (!promptShown) {
                 if (!promptShown) {
                   setTimeout(() => setShowCacheAllPrompt(true), 500)
                   setTimeout(() => setShowCacheAllPrompt(true), 500)
                 }
                 }
               }
               }
+              setCacheProgress(null)
             } else {
             } else {
-              setCacheProgress(data)
+              // Not running and not complete (idle state)
+              setCacheProgress(null)
             }
             }
           }
           }
         } catch {
         } catch {
@@ -473,37 +463,31 @@ export function Layout() {
 
 
       ws.onclose = () => {
       ws.onclose = () => {
         cacheWsRef.current = null
         cacheWsRef.current = null
+        // Reconnect after 3 seconds
+        if (isBackendConnected) {
+          reconnectTimeout = setTimeout(connectCacheWebSocket, 3000)
+        }
       }
       }
 
 
       ws.onerror = () => {
       ws.onerror = () => {
-        // Fallback to polling
-        const pollInterval = setInterval(() => {
-          fetch('/cache-progress')
-            .then((r) => r.json())
-            .then((data) => {
-              if (!data.is_running) {
-                setCacheProgress(null)
-                clearInterval(pollInterval)
-              } else {
-                setCacheProgress(data)
-              }
-            })
-            .catch(() => {})
-        }, 1000)
+        // Will trigger onclose
       }
       }
 
 
       cacheWsRef.current = ws
       cacheWsRef.current = ws
     }
     }
 
 
-    checkCacheProgress()
+    connectCacheWebSocket()
 
 
     return () => {
     return () => {
+      if (reconnectTimeout) {
+        clearTimeout(reconnectTimeout)
+      }
       if (cacheWsRef.current) {
       if (cacheWsRef.current) {
         cacheWsRef.current.close()
         cacheWsRef.current.close()
         cacheWsRef.current = null
         cacheWsRef.current = null
       }
       }
     }
     }
-  }, [isBackendConnected])
+  }, [isBackendConnected, cacheProgress?.is_running])
 
 
   // Calculate cache progress percentage
   // Calculate cache progress percentage
   const cachePercentage = cacheProgress?.total_files
   const cachePercentage = cacheProgress?.total_files