Fix: Handle Date objects safely in ThemeSwitcher
This commit is contained in:
parent
1daf44809a
commit
ea74666833
1 changed files with 22 additions and 17 deletions
|
|
@ -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) {
|
||||
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 {
|
||||
...p,
|
||||
...p.data,
|
||||
id: p.id || p.slug,
|
||||
slug: p.slug || p.id,
|
||||
content: p.body
|
||||
...postData,
|
||||
id: p.id || p.slug || postData.id,
|
||||
slug: p.slug || p.id || postData.slug,
|
||||
content: p.body || postData.content
|
||||
};
|
||||
}
|
||||
return p;
|
||||
});
|
||||
|
||||
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} />;
|
||||
|
|
|
|||
Loading…
Reference in a new issue