Fix: Handle Date objects safely in ThemeSwitcher

This commit is contained in:
marciobever 2026-05-03 17:04:53 +00:00
parent 1daf44809a
commit ea74666833

View file

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