Better new file handeling and offline notifications.
This commit is contained in:
+26
-14
@@ -8,6 +8,7 @@ import { readJsonFile } from '../actions/jsonEditor';
|
||||
import { DocumentCard, DocumentItem, ExternalLinkCard, LinkItem } from '../components/ui/Cards';
|
||||
import { OfflineBadge } from '../components/ui/OfflineBadge';
|
||||
import { PageHeader } from '../components/ui/PageHeader';
|
||||
import { getLocalFileMeta } from '../actions/files';
|
||||
|
||||
const IconMap: Record<string, any> = {
|
||||
"Map": Map,
|
||||
@@ -18,18 +19,25 @@ const IconMap: Record<string, any> = {
|
||||
"FileText": FileText
|
||||
};
|
||||
|
||||
const fetchFileSize = async (url: string) => {
|
||||
const fetchFileMeta = async (url: string) => {
|
||||
try {
|
||||
const res = await fetch(url, { method: 'HEAD' });
|
||||
const res = await fetch(url, { method: 'HEAD', cache: 'no-store' });
|
||||
const bytes = res.headers.get('content-length');
|
||||
const lastModified = res.headers.get('last-modified');
|
||||
const etag = res.headers.get('etag');
|
||||
|
||||
let sizeStr = "Okänd";
|
||||
if (bytes) {
|
||||
const mb = (parseInt(bytes) / (1024 * 1024)).toFixed(2);
|
||||
return `${mb} MB`;
|
||||
sizeStr = `${mb} MB`;
|
||||
}
|
||||
const version = etag ? etag.replace(/"/g, '') :
|
||||
(lastModified ? new Date(lastModified).getTime().toString() : 'v1');
|
||||
|
||||
return { size: sizeStr, version };
|
||||
} catch (error) {
|
||||
console.error("Kunde inte hämta filstorlek för", url);
|
||||
return { size: "Okänd", version: "v1" };
|
||||
}
|
||||
return "Okänd";
|
||||
};
|
||||
|
||||
export default function Documents() {
|
||||
@@ -62,22 +70,26 @@ export default function Documents() {
|
||||
if (navigator.onLine) {
|
||||
const res = await readJsonFile('documents.json');
|
||||
if (res.success && res.data) {
|
||||
const docsWithSizes = await Promise.all(
|
||||
const docsWithMeta = 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;
|
||||
const meta = await getLocalFileMeta(doc.file);
|
||||
const versionedUrl = `${doc.file}?v=${meta.version}`;
|
||||
|
||||
return {
|
||||
...doc,
|
||||
size: (!doc.size || doc.size === "Okänd") ? meta.size : doc.size,
|
||||
originalFile: doc.file,
|
||||
file: versionedUrl
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
setDocs(docsWithSizes);
|
||||
setDocs(docsWithMeta);
|
||||
setLinks(res.data.links || []);
|
||||
const dataToCache = { ...res.data, docs: docsWithSizes };
|
||||
const dataToCache = { ...res.data, docs: docsWithMeta };
|
||||
localStorage.setItem('kullaberg_documents_cache', JSON.stringify(dataToCache));
|
||||
|
||||
checkCacheStatus(docsWithSizes);
|
||||
checkCacheStatus(docsWithMeta);
|
||||
}
|
||||
} else {
|
||||
const cachedData = localStorage.getItem('kullaberg_documents_cache');
|
||||
|
||||
Reference in New Issue
Block a user