/* eslint-disable @typescript-eslint/no-explicit-any */ 'use client'; import { AlertTriangle,Star, X } from 'lucide-react'; import { useEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import { clearAllFavorites, getAllFavorites, getAllPlayRecords, subscribeToDataUpdates, } from '@/lib/db.client'; import VideoCard from '@/components/VideoCard'; interface FavoriteItem { id: string; source: string; title: string; year: string; poster: string; episodes?: number; source_name?: string; currentEpisode?: number; search_title?: string; origin?: 'vod' | 'live'; } interface FavoritesPanelProps { isOpen: boolean; onClose: () => void; } export const FavoritesPanel: React.FC = ({ isOpen, onClose, }) => { const [favoriteItems, setFavoriteItems] = useState([]); const [loading, setLoading] = useState(false); const [showConfirmDialog, setShowConfirmDialog] = useState(false); // 加载收藏数据 const loadFavorites = async () => { setLoading(true); try { const allFavorites = await getAllFavorites(); const allPlayRecords = await getAllPlayRecords(); // 根据保存时间排序(从近到远) const sorted = Object.entries(allFavorites) .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); // 查找对应的播放记录,获取当前集数 const playRecord = allPlayRecords[key]; const currentEpisode = playRecord?.index; return { id, source, title: fav.title, year: fav.year, poster: fav.cover, episodes: fav.total_episodes, source_name: fav.source_name, currentEpisode, search_title: fav?.search_title, origin: fav?.origin, } as FavoriteItem; }); setFavoriteItems(sorted); } catch (error) { console.error('加载收藏失败:', error); } finally { setLoading(false); } }; // 清空所有收藏 const handleClearAll = async () => { try { await clearAllFavorites(); setFavoriteItems([]); setShowConfirmDialog(false); } catch (error) { console.error('清空收藏失败:', error); } }; // 打开面板时加载收藏 useEffect(() => { if (isOpen) { loadFavorites(); } }, [isOpen]); // 监听收藏变化,实时移除已取消收藏的项目 useEffect(() => { const unsubscribe = subscribeToDataUpdates('favoritesUpdated', async (newFavorites: Record) => { if (isOpen) { // 获取最新的收藏列表的键 const currentKeys = Object.keys(newFavorites); // 过滤掉已经不在收藏中的项目 setFavoriteItems((prevItems) => prevItems.filter((item) => { const key = `${item.source}+${item.id}`; return currentKeys.includes(key); }) ); } }); return () => { unsubscribe(); }; }, [isOpen]); return ( <> {/* 背景遮罩 */}
{/* 收藏面板 */}
{/* 标题栏 */}

我的收藏

{favoriteItems.length > 0 && ( {favoriteItems.length} 项 )}
{favoriteItems.length > 0 && ( )}
{/* 收藏列表 */}
{loading ? (
) : favoriteItems.length === 0 ? (

暂无收藏内容

) : (
{favoriteItems.map((item) => (
1 ? 'tv' : ''} />
))}
)}
{/* 确认对话框 */} {showConfirmDialog && createPortal(
setShowConfirmDialog(false)} >
e.stopPropagation()} >
{/* 图标和标题 */}

清空收藏

确定要清空所有收藏吗?此操作不可恢复。

{/* 按钮组 */}
, document.body )} ); };