Optimized: Clean ThemeSwitcher with safe dates

This commit is contained in:
marciobever 2026-05-04 10:59:51 +00:00
parent c3adb90c63
commit 77e31e97c0

View file

@ -1,95 +1,68 @@
import CyberTheme from './CyberTheme';
import StartupTheme from './StartupTheme';
import LinearTheme from './LinearTheme';
import MagazineTheme from './MagazineTheme';
import NeonTheme from './NeonTheme';
import PopularTheme from './PopularTheme';
import DarkChefTheme from './DarkChefTheme';
import EditorialTheme from './EditorialTheme';
import GourmetTheme from './GourmetTheme';
import HealthyTheme from './HealthyTheme';
import CardsTheme from './CardsTheme';
import CorporateTheme from './CorporateTheme';
import CryptoTheme from './CryptoTheme';
import SubstackTheme from './SubstackTheme';
import TradingTheme from './TradingTheme';
import HypeTheme from './HypeTheme';
import LuxeTheme from './LuxeTheme';
import MinimalTheme from './MinimalTheme';
import NexusTheme from './NexusTheme';
import TerraTheme from './TerraTheme';
import AuthorityTheme from './AuthorityTheme';
import MetricTheme from './MetricTheme';
import SeoMinimalTheme from './SeoMinimalTheme';
import SeoNexusTheme from './SeoNexusTheme';
import PulseTheme from './PulseTheme';
import SeoPortalTheme from './SeoTheme';
interface ThemeSwitcherProps {
themeId: string;
posts: any[];
activeView?: 'home' | 'article';
currentArticle?: any;
children?: React.ReactNode;
}
import Theme from './PopularTheme';
const themeMap: Record<string, any> = {
'cyber': CyberTheme,
'startup': StartupTheme,
'linear': LinearTheme,
'magazine': MagazineTheme,
'neon': NeonTheme,
'popular': PopularTheme,
'darkchef': DarkChefTheme,
'editorial': EditorialTheme,
'gourmet': GourmetTheme,
'healthy': HealthyTheme,
'cards': CardsTheme,
'corporate': CorporateTheme,
'crypto': CryptoTheme,
'substack': SubstackTheme,
'trading': TradingTheme,
'hype': HypeTheme,
'luxe': LuxeTheme,
'minimal': MinimalTheme,
'nexus': NexusTheme,
'terra': TerraTheme,
'authority': AuthorityTheme,
'metric': MetricTheme,
'seo-minimal': SeoMinimalTheme,
'seo-nexus': SeoNexusTheme,
'pulse': PulseTheme,
'seo-portal': SeoPortalTheme,
const formatDateSafe = (value: any) => {
if (!value) return '00:00:00';
if (value instanceof Date) {
return value.toLocaleDateString('pt-BR');
}
if (typeof value === 'string') {
return value;
}
try {
const parsed = new Date(value);
if (!isNaN(parsed.getTime())) {
return parsed.toLocaleDateString('pt-BR');
}
} catch {}
return String(value);
};
export default function ThemeSwitcher({ themeId, posts, currentArticle, ...props }: ThemeSwitcherProps) {
// Normalize posts from Astro collection (data property)
const normalizedPosts = posts?.map(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);
export default function ThemeSwitcher({ themeId, posts = [], currentArticle, ...props }: any) {
const normalizedPosts = (posts || []).map((p: any, i: number) => {
const data = p?.data || {};
return {
...postData,
id: p.id || p.slug || postData.id,
slug: p.slug || p.id || postData.slug,
content: p.body || postData.content
...p,
...data,
id: p?.id || p?.slug || data?.slug || String(i),
slug: p?.slug || data?.slug || p?.id || String(i),
title: data?.title || p?.title || 'Sem título',
excerpt: data?.excerpt || data?.description || p?.excerpt || '',
date: formatDateSafe(data?.date || data?.pubDate || p?.date || p?.pubDate),
author: data?.author || p?.author || 'AI_NODE',
sev: data?.sev || p?.sev || (i % 2 === 0 ? 'CRITICAL' : 'HIGH'),
content: p?.body || p?.content || ''
};
});
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 data = currentArticle?.data || {};
const SelectedTheme = themeMap[themeId] || CyberTheme;
return <SelectedTheme posts={normalizedPosts} currentArticle={normalizedArticle} {...props} />;
const normalizedArticle = currentArticle
? {
...currentArticle,
...data,
id: currentArticle?.id || currentArticle?.slug || data?.slug || 'article',
slug: currentArticle?.slug || data?.slug || currentArticle?.id || 'article',
title: data?.title || currentArticle?.title || 'Sem título',
excerpt: data?.excerpt || data?.description || currentArticle?.excerpt || '',
date: formatDateSafe(data?.date || data?.pubDate || currentArticle?.date || currentArticle?.pubDate),
author: data?.author || currentArticle?.author || 'AI_NODE',
sev: data?.sev || currentArticle?.sev || 'LOW',
content: currentArticle?.body || currentArticle?.content || ''
}
: undefined;
return (
<Theme
themeId={themeId}
posts={normalizedPosts}
currentArticle={normalizedArticle}
{...props}
/>
);
}