import HeroSlider from '@/components/HeroSlider'; import MovieRow from '@/components/MovieRow'; import { getHomepageData, getMovieDetail, HomeSection, Subject } from '@/lib/api'; import { getDracinHome, getDracinDetail, DramaboxItem } from '@/lib/dramabox'; export default async function Home() { console.log("Fetching homepage data..."); let data; let dracinData: DramaboxItem[] = []; try { // Fetch both datasets in parallel const [homeData, dracinRes] = await Promise.all([ getHomepageData(), getDracinHome(1, 10) // Fetch top 10 Dracin items ]); data = homeData; dracinData = dracinRes; } catch (e) { console.error("Error generating homepage:", e); return (

Error loading data

{e instanceof Error ? e.message : String(e)}

); } // Use operatingList if available, otherwise fallback to homeList let contentList = data.operatingList || data.homeList; if (!contentList) { contentList = []; } // Map Dracin Data to Subject format if (dracinData.length > 0) { const dracinSection: HomeSection = { type: 'SUBJECTS_MOVIE', // Reuse existing type for rendering title: 'Latest Dracin', // Title for the new row subjects: dracinData.map(d => ({ subjectId: d.id.toString(), subjectType: 3, // Custom type title: d.name, description: d.introduction, releaseDate: "", genre: d.tags.join(", "), cover: { url: d.cover, width: 300, height: 450 }, image: { url: d.cover, width: 300, height: 450 }, countryName: "China", imdbRatingValue: "N/A", detailPath: "", isDracin: true })) }; // Insert Dracin section after the first movie section (usually index 1) if (contentList.length > 1) { contentList.splice(1, 0, dracinSection); } else { contentList.push(dracinSection); } } if (contentList.length === 0) { return
No Content Available
; } // Find Banner section const bannerSection = contentList.find((s) => s.type === 'BANNER'); // Get all items for the slider let bannerItems = bannerSection?.banner?.items || bannerSection?.subjects || []; // Enrich banner items with descriptions if missing if (bannerItems.length > 0) { bannerItems = await Promise.all(bannerItems.map(async (item: any) => { const hasDesc = item.description && item.description !== "No description available."; const hasSubjectDesc = item.subject?.description && item.subject.description !== "No description available."; if (hasDesc || hasSubjectDesc) return item; const id = item.subjectId || item.id || item.subject?.subjectId; if (!id) return item; try { // Try fetching as a movie first (most common and covers the long numeric IDs we saw) try { const detail = await getMovieDetail(id); if (detail && detail.subject && detail.subject.description) { return { ...item, description: detail.subject.description }; } } catch (movieError) { // Ignore error and try Dracin } // If movie fetch failed or didn't return a description, try Dracin const { drama } = await getDracinDetail(id); if (drama && drama.introduction) { return { ...item, description: drama.introduction }; } } catch (e) { // Log as warning to differentiate from critical errors // console.warn(`Failed to enrich banner item ${id}:`, e instanceof Error ? e.message : e); } return item; })); } return (
{/* Hero Section */} {bannerItems.length > 0 && } {/* Movie Rows */}
{contentList.map((section, index) => { if (section.type === 'SUBJECTS_MOVIE' && section.subjects && section.subjects.length > 0) { const isDracinSection = section.title === 'Latest Dracin'; return ( ); } return null; })}
); }