Dynamic file sizes

This commit is contained in:
2026-03-18 15:39:40 +01:00 Verified
parent 2814a287fb
commit 4da454819d
10 changed files with 35 additions and 18 deletions
-3
View File
@@ -2,9 +2,6 @@
# Ignore the heavy PDFs during build # Ignore the heavy PDFs during build
public/files/* public/files/*
# (Optional) Keep a placeholder file so the directory structure isn't completely lost locally
!public/files/.gitkeep
# Dependency directories # Dependency directories
node_modules node_modules
npm-debug.log npm-debug.log
+6 -12
View File
@@ -3,38 +3,32 @@
{ {
"title": "Turistkarta", "title": "Turistkarta",
"icon": "Map", "icon": "Map",
"file": "/files/Turistkarta - Kullaberg.pdf", "file": "/files/Turistkarta - Kullaberg.pdf"
"size": "2.51 MB"
}, },
{ {
"title": "Orienteringskarta", "title": "Orienteringskarta",
"icon": "MapPlus", "icon": "MapPlus",
"file": "/files/Orienteringskarta - Kullaberg.pdf", "file": "/files/Orienteringskarta - Kullaberg.pdf"
"size": "3.74 MB"
}, },
{ {
"title": "Badplatser att besöka", "title": "Badplatser att besöka",
"icon": "WavesLadder", "icon": "WavesLadder",
"file": "/files/Badplatser på Kullaberg.pdf", "file": "/files/Badplatser på Kullaberg.pdf"
"size": "1.88 MB"
}, },
{ {
"title": "Vandringsrutter", "title": "Vandringsrutter",
"icon": "MapPinned", "icon": "MapPinned",
"file": "/files/Vandringsrutter.pdf", "file": "/files/Vandringsrutter.pdf"
"size": "4.42 MB"
}, },
{ {
"title": "Underlag för guidediplomering", "title": "Underlag för guidediplomering",
"icon": "BookCopy", "icon": "BookCopy",
"file": "/files/Underlag för guidediplomering.pdf", "file": "/files/Underlag för guidediplomering.pdf"
"size": "3.18 MB"
}, },
{ {
"title": "Destinationskunskap Kullahalvön", "title": "Destinationskunskap Kullahalvön",
"icon": "BookCopy", "icon": "BookCopy",
"file": "/files/Destinationskunskap.pdf", "file": "/files/Destinationskunskap.pdf"
"size": "1.97 MB"
} }
], ],
"links": [ "links": [
+29 -3
View File
@@ -18,6 +18,20 @@ const IconMap: Record<string, any> = {
"FileText": FileText "FileText": FileText
}; };
const fetchFileSize = async (url: string) => {
try {
const res = await fetch(url, { method: 'HEAD' });
const bytes = res.headers.get('content-length');
if (bytes) {
const mb = (parseInt(bytes) / (1024 * 1024)).toFixed(2);
return `${mb} MB`;
}
} catch (error) {
console.error("Kunde inte hämta filstorlek för", url);
}
return "Okänd";
};
export default function Documents() { export default function Documents() {
const [isHydrated, setIsHydrated] = useState(false); const [isHydrated, setIsHydrated] = useState(false);
const [isOffline, setIsOffline] = useState(false); const [isOffline, setIsOffline] = useState(false);
@@ -48,10 +62,22 @@ export default function Documents() {
if (navigator.onLine) { if (navigator.onLine) {
const res = await readJsonFile('documents.json'); const res = await readJsonFile('documents.json');
if (res.success && res.data) { if (res.success && res.data) {
setDocs(res.data.docs || []); const docsWithSizes = await Promise.all(
(res.data.docs || []).map(async (doc: DocumentItem) => {
if (!doc.size || doc.size === "") {
const calculatedSize = await fetchFileSize(doc.file);
return { ...doc, size: calculatedSize };
}
return doc;
})
);
setDocs(docsWithSizes);
setLinks(res.data.links || []); setLinks(res.data.links || []);
localStorage.setItem('kullaberg_documents_cache', JSON.stringify(res.data)); const dataToCache = { ...res.data, docs: docsWithSizes };
checkCacheStatus(res.data.docs || []); localStorage.setItem('kullaberg_documents_cache', JSON.stringify(dataToCache));
checkCacheStatus(docsWithSizes);
} }
} else { } else {
const cachedData = localStorage.getItem('kullaberg_documents_cache'); const cachedData = localStorage.getItem('kullaberg_documents_cache');
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.