'use client'; import { Film, MessageCircleHeart, MountainSnow, Star, Swords, Tv, VenetianMask, } from 'lucide-react'; import { Suspense, useEffect, useState } from 'react'; // 客户端收藏 API import { getAllFavorites } from '@/lib/db.client'; import CapsuleSwitch from '@/components/CapsuleSwitch'; import CollectionCard from '@/components/CollectionCard'; import ContinueWatching from '@/components/ContinueWatching'; import DemoCard from '@/components/DemoCard'; import PageLayout from '@/components/PageLayout'; import ScrollableRow from '@/components/ScrollableRow'; import VideoCard from '@/components/VideoCard'; interface DoubanItem { title: string; poster: string; } interface DoubanResponse { code: number; message: string; list: DoubanItem[]; } // 合集数据 const collections = [ { icon: Film, title: '热门电影', href: '/douban?type=movie&tag=热门&title=热门电影', }, { icon: Tv, title: '热门剧集', href: '/douban?type=tv&tag=热门&title=热门剧集', }, { icon: Star, title: '豆瓣 Top250', href: '/douban?type=movie&tag=top250&title=豆瓣 Top250', }, { icon: Swords, title: '美剧', href: '/douban?type=tv&tag=美剧' }, { icon: MessageCircleHeart, title: '韩剧', href: '/douban?type=tv&tag=韩剧' }, { icon: MountainSnow, title: '日剧', href: '/douban?type=tv&tag=日剧' }, { icon: VenetianMask, title: '日漫', href: '/douban?type=tv&tag=日本动画' }, ]; function HomeClient() { const [activeTab, setActiveTab] = useState('home'); const [hotMovies, setHotMovies] = useState([]); const [hotTvShows, setHotTvShows] = useState([]); const [loading, setLoading] = useState(true); // 收藏夹数据 type FavoriteItem = { id: string; source: string; title: string; poster: string; episodes: number; source_name: string; }; const [favoriteItems, setFavoriteItems] = useState([]); useEffect(() => { const fetchDoubanData = async () => { try { setLoading(true); // 并行获取热门电影和热门剧集 const [moviesResponse, tvShowsResponse] = await Promise.all([ fetch('/api/douban?type=movie&tag=热门'), fetch('/api/douban?type=tv&tag=热门'), ]); if (moviesResponse.ok) { const moviesData: DoubanResponse = await moviesResponse.json(); setHotMovies(moviesData.list); } if (tvShowsResponse.ok) { const tvShowsData: DoubanResponse = await tvShowsResponse.json(); setHotTvShows(tvShowsData.list); } } finally { setLoading(false); } }; fetchDoubanData(); }, []); // 当切换到收藏夹时加载收藏数据 useEffect(() => { if (activeTab !== 'favorites') return; (async () => { const all = await getAllFavorites(); // 根据保存时间排序(从近到远) const sorted = Object.entries(all) .sort(([, a], [, b]) => b.save_time - a.save_time) .map(([key, fav]) => { const plusIndex = key.indexOf('+'); const source = key.slice(0, plusIndex); const id = key.slice(plusIndex + 1); return { id, source, title: fav.title, poster: fav.cover, episodes: fav.total_episodes, source_name: fav.source_name, } as FavoriteItem; }); setFavoriteItems(sorted); })(); }, [activeTab]); return (
{/* 顶部 Tab 切换 */}
{activeTab === 'favorites' ? ( // 收藏夹视图

我的收藏

{favoriteItems.map((item) => (
))} {favoriteItems.length === 0 && (
暂无收藏内容
)}
) : ( // 首页视图 <> {/* 推荐 */}

推荐

{collections.map((collection) => (
))}
{/* 继续观看 */} {/* 热门电影 */}

热门电影

{loading ? // 加载状态显示灰色占位数据 Array.from({ length: 8 }).map((_, index) => (
)) : // 显示真实数据 hotMovies.map((movie, index) => (
))}
{/* 热门剧集 */}

热门剧集

{loading ? // 加载状态显示灰色占位数据 Array.from({ length: 8 }).map((_, index) => (
)) : // 显示真实数据 hotTvShows.map((show, index) => (
))}
)}
); } export default function Home() { return ( ); }