Better file fetching and status
This commit is contained in:
+13
-5
@@ -42,6 +42,7 @@ export default function Documents() {
|
|||||||
}
|
}
|
||||||
setCachedFiles(cached);
|
setCachedFiles(cached);
|
||||||
if (cached.size === docs.length) setSyncStatus('done');
|
if (cached.size === docs.length) setSyncStatus('done');
|
||||||
|
else setSyncStatus('idle');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Cache check failed", error);
|
console.error("Cache check failed", error);
|
||||||
}
|
}
|
||||||
@@ -57,6 +58,12 @@ export default function Documents() {
|
|||||||
|
|
||||||
checkCacheStatus();
|
checkCacheStatus();
|
||||||
|
|
||||||
|
const handleVisibility = () => {
|
||||||
|
if (document.visibilityState === 'visible') checkCacheStatus();
|
||||||
|
};
|
||||||
|
document.addEventListener('visibilitychange', handleVisibility);
|
||||||
|
window.addEventListener('focus', checkCacheStatus);
|
||||||
|
|
||||||
const conn = (navigator as any).connection;
|
const conn = (navigator as any).connection;
|
||||||
if (conn && (conn.type === 'wifi' || conn.type === 'ethernet') && !conn.saveData) {
|
if (conn && (conn.type === 'wifi' || conn.type === 'ethernet') && !conn.saveData) {
|
||||||
handleSyncAll();
|
handleSyncAll();
|
||||||
@@ -65,6 +72,8 @@ export default function Documents() {
|
|||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener('online', handleStatus);
|
window.removeEventListener('online', handleStatus);
|
||||||
window.removeEventListener('offline', handleStatus);
|
window.removeEventListener('offline', handleStatus);
|
||||||
|
document.removeEventListener('visibilitychange', handleVisibility);
|
||||||
|
window.removeEventListener('focus', checkCacheStatus);
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -73,9 +82,11 @@ export default function Documents() {
|
|||||||
const updatedCache = new Set(cachedFiles);
|
const updatedCache = new Set(cachedFiles);
|
||||||
|
|
||||||
for (const doc of docs) {
|
for (const doc of docs) {
|
||||||
|
if (!updatedCache.has(doc.file)) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(doc.file);
|
const response = await fetch(doc.file);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
|
await response.blob();
|
||||||
updatedCache.add(doc.file);
|
updatedCache.add(doc.file);
|
||||||
setCachedFiles(new Set(updatedCache));
|
setCachedFiles(new Set(updatedCache));
|
||||||
}
|
}
|
||||||
@@ -83,15 +94,12 @@ export default function Documents() {
|
|||||||
console.error("Kunde inte ladda ner:", doc.file);
|
console.error("Kunde inte ladda ner:", doc.file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setTimeout(async () => {
|
|
||||||
await checkCacheStatus();
|
await checkCacheStatus();
|
||||||
setSyncStatus('done');
|
setSyncStatus('done');
|
||||||
setShowNotification(true);
|
setShowNotification(true);
|
||||||
setTimeout(() => {
|
setTimeout(() => setShowNotification(false), 4000);
|
||||||
setShowNotification(false);
|
|
||||||
}, 4000);
|
|
||||||
}, 500);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!isHydrated) return <div className="flex justify-center py-20"><Loader2 className="animate-spin text-slate-teal" size={40} /></div>;
|
if (!isHydrated) return <div className="flex justify-center py-20"><Loader2 className="animate-spin text-slate-teal" size={40} /></div>;
|
||||||
|
|||||||
Reference in New Issue
Block a user