Optimized: Clean ThemeSwitcher with safe dates
This commit is contained in:
parent
c3adb90c63
commit
77e31e97c0
1 changed files with 58 additions and 85 deletions
|
|
@ -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 {
|
import Theme from './PopularTheme';
|
||||||
themeId: string;
|
|
||||||
posts: any[];
|
const formatDateSafe = (value: any) => {
|
||||||
activeView?: 'home' | 'article';
|
if (!value) return '00:00:00';
|
||||||
currentArticle?: any;
|
|
||||||
children?: React.ReactNode;
|
if (value instanceof Date) {
|
||||||
|
return value.toLocaleDateString('pt-BR');
|
||||||
}
|
}
|
||||||
|
|
||||||
const themeMap: Record<string, any> = {
|
if (typeof value === 'string') {
|
||||||
'cyber': CyberTheme,
|
return value;
|
||||||
'startup': StartupTheme,
|
}
|
||||||
'linear': LinearTheme,
|
|
||||||
'magazine': MagazineTheme,
|
try {
|
||||||
'neon': NeonTheme,
|
const parsed = new Date(value);
|
||||||
'popular': PopularTheme,
|
if (!isNaN(parsed.getTime())) {
|
||||||
'darkchef': DarkChefTheme,
|
return parsed.toLocaleDateString('pt-BR');
|
||||||
'editorial': EditorialTheme,
|
}
|
||||||
'gourmet': GourmetTheme,
|
} catch {}
|
||||||
'healthy': HealthyTheme,
|
|
||||||
'cards': CardsTheme,
|
return String(value);
|
||||||
'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,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function ThemeSwitcher({ themeId, posts, currentArticle, ...props }: ThemeSwitcherProps) {
|
export default function ThemeSwitcher({ themeId, posts = [], currentArticle, ...props }: any) {
|
||||||
// Normalize posts from Astro collection (data property)
|
const normalizedPosts = (posts || []).map((p: any, i: number) => {
|
||||||
const normalizedPosts = posts?.map(p => {
|
const data = 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 {
|
||||||
...postData,
|
...p,
|
||||||
id: p.id || p.slug || postData.id,
|
...data,
|
||||||
slug: p.slug || p.id || postData.slug,
|
id: p?.id || p?.slug || data?.slug || String(i),
|
||||||
content: p.body || postData.content
|
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;
|
const data = currentArticle?.data || {};
|
||||||
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;
|
const normalizedArticle = currentArticle
|
||||||
return <SelectedTheme posts={normalizedPosts} currentArticle={normalizedArticle} {...props} />;
|
? {
|
||||||
|
...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}
|
||||||
|
/>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue