Fix: Handle Date objects safely in ThemeSwitcher

This commit is contained in:
marciobever 2026-05-03 17:04:52 +00:00
parent a9d54b1cef
commit 1acb99a431

View file

@ -65,25 +65,30 @@ const themeMap: Record<string, any> = {
export default function ThemeSwitcher({ themeId, posts, currentArticle, ...props }: ThemeSwitcherProps) { export default function ThemeSwitcher({ themeId, posts, currentArticle, ...props }: ThemeSwitcherProps) {
// Normalize posts from Astro collection (data property) // Normalize posts from Astro collection (data property)
const normalizedPosts = posts?.map(p => { const normalizedPosts = posts?.map(p => {
if (p.data) { let postData = p.data ? { ...p, ...p.data } : { ...p };
// Safety for Astro Date objects failing in React
if (postData.date && typeof postData.date !== 'string') postData.date = String(postData.date);
if (postData.updatedDate && typeof postData.updatedDate !== 'string') postData.updatedDate = String(postData.updatedDate);
return { return {
...p, ...postData,
...p.data, id: p.id || p.slug || postData.id,
id: p.id || p.slug, slug: p.slug || p.id || postData.slug,
slug: p.slug || p.id, content: p.body || postData.content
content: p.body
}; };
}
return p;
}); });
const normalizedArticle = currentArticle?.data ? { let normalizedArticle = currentArticle?.data ? { ...currentArticle, ...currentArticle.data } : currentArticle ? { ...currentArticle } : null;
...currentArticle, if (normalizedArticle) {
...currentArticle.data, if (normalizedArticle.date && typeof normalizedArticle.date !== 'string') normalizedArticle.date = String(normalizedArticle.date);
id: currentArticle.id || currentArticle.slug, if (normalizedArticle.updatedDate && typeof normalizedArticle.updatedDate !== 'string') normalizedArticle.updatedDate = String(normalizedArticle.updatedDate);
slug: currentArticle.slug || currentArticle.id, normalizedArticle = {
content: currentArticle.body ...normalizedArticle,
} : currentArticle; id: currentArticle?.id || currentArticle?.slug || normalizedArticle.id,
slug: currentArticle?.slug || currentArticle?.id || normalizedArticle.slug,
content: currentArticle?.body || normalizedArticle.content
};
}
const SelectedTheme = themeMap[themeId] || CyberTheme; const SelectedTheme = themeMap[themeId] || CyberTheme;
return <SelectedTheme posts={normalizedPosts} currentArticle={normalizedArticle} {...props} />; return <SelectedTheme posts={normalizedPosts} currentArticle={normalizedArticle} {...props} />;