fix: properly detect offline mode when using cached data
This commit is contained in:
15
src/App.js
15
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();
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
// ============================================
|
||||
|
||||
Reference in New Issue
Block a user