From 7e72d6c02d4347cd56afa53690a00c82a304caec Mon Sep 17 00:00:00 2001 From: fahed Date: Wed, 4 Feb 2026 11:49:25 +0300 Subject: [PATCH] fix: properly detect offline mode when using cached data --- src/App.js | 15 ++++----------- src/services/dataService.js | 8 ++++---- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/App.js b/src/App.js index c901271..9f5a790 100644 --- a/src/App.js +++ b/src/App.js @@ -41,28 +41,21 @@ function App() { setRefreshing(forceRefresh); const result = forceRefresh ? await refreshData() : await fetchData(); - setData(result); + setData(result.data); setError(null); - setIsOffline(false); + setIsOffline(result.fromCache); // Update cache info const status = getCacheStatus(); setCacheInfo(status); } catch (err) { - // Check if we got data from cache despite the error - const status = getCacheStatus(); - if (status.available && data.length > 0) { - setIsOffline(true); - setCacheInfo(status); - } else { - setError(err.message); - } + setError(err.message); console.error(err); } finally { setLoading(false); setRefreshing(false); } - }, [data.length]); + }, []); useEffect(() => { loadData(); diff --git a/src/services/dataService.js b/src/services/dataService.js index b32fc06..4caea1b 100644 --- a/src/services/dataService.js +++ b/src/services/dataService.js @@ -179,7 +179,7 @@ export async function fetchData() { const cached = loadFromCache(); if (cached) { console.warn('NocoDB not configured, using cached data'); - return cached.data; + return { data: cached.data, fromCache: true, cacheTimestamp: cached.timestamp }; } throw new Error('NocoDB not configured and no cached data available. Set REACT_APP_NOCODB_URL and REACT_APP_NOCODB_TOKEN in .env.local'); } @@ -191,7 +191,7 @@ export async function fetchData() { // Save to cache on success saveToCache(data); - return data; + return { data, fromCache: false }; } catch (err) { console.error('NocoDB fetch failed:', err.message); @@ -199,7 +199,7 @@ export async function fetchData() { const cached = loadFromCache(); if (cached) { console.warn(`Using cached data from ${new Date(cached.timestamp).toLocaleString()} (offline mode)`); - return cached.data; + return { data: cached.data, fromCache: true, cacheTimestamp: cached.timestamp }; } throw new Error(`Database unavailable and no cached data: ${err.message}`); @@ -214,7 +214,7 @@ export async function refreshData() { const data = await fetchFromNocoDB(); saveToCache(data); - return data; + return { data, fromCache: false }; } // ============================================