This commit is contained in:
Tomas Dvorak
2025-11-11 10:29:30 +01:00
parent d5b4faea61
commit 8762bde4bf
139 changed files with 7240 additions and 2870 deletions
+31 -1
View File
@@ -2,15 +2,45 @@ import api, { API_URL } from './api';
import { getToken } from '../utils/auth';
const normalizeArticle = (raw: any): Article => {
if (!raw) return raw;
if (!raw) return raw as Article;
const id = raw.id ?? raw.ID ?? raw.article_id ?? raw.articleId;
const category = raw.category ?? raw.Category;
const author = raw.author ?? raw.Author;
// Normalize attachments: backend may send a JSON string or an array
let attachments: Array<{ name: string; url: string; mime_type?: string; size?: number }> | undefined = undefined;
const aRaw = raw.attachments ?? raw.Attachments;
try {
if (Array.isArray(aRaw)) {
attachments = aRaw.map((it: any) => {
if (typeof it === 'string') {
const name = it.split('/').pop() || 'soubor';
return { name, url: it };
}
return { name: it?.name || (String(it?.url || '').split('/').pop() || 'soubor'), url: it?.url || '', mime_type: it?.mime_type || it?.type, size: it?.size };
});
} else if (typeof aRaw === 'string' && aRaw.trim() !== '') {
const parsed = JSON.parse(aRaw);
if (Array.isArray(parsed)) {
attachments = parsed.map((it: any) => {
if (typeof it === 'string') {
const name = it.split('/').pop() || 'soubor';
return { name, url: it };
}
return { name: it?.name || (String(it?.url || '').split('/').pop() || 'soubor'), url: it?.url || '', mime_type: it?.mime_type || it?.type, size: it?.size };
});
}
}
} catch {
// ignore malformed attachments
}
return {
...(raw as Article),
id,
category,
author,
...(attachments ? { attachments } : {}),
} as Article;
};