'use client'; import { useState } from 'react'; import { useSearchParams } from 'next/navigation'; import { Card } from '@/components/ui/Card'; import { Button } from '@/components/ui/Button'; import { Icons } from '@/components/ui/Icon'; import { useHistoryStore } from '@/lib/store/history-store'; import { CustomVideoPlayer } from './CustomVideoPlayer'; interface VideoPlayerProps { playUrl: string; videoId?: string; currentEpisode: number; onBack: () => void; } export function VideoPlayer({ playUrl, videoId, currentEpisode, onBack }: VideoPlayerProps) { const [videoError, setVideoError] = useState(''); const [useProxy, setUseProxy] = useState(false); const [shouldAutoPlay, setShouldAutoPlay] = useState(false); // Use reactive hook to subscribe to history updates // This ensures the component re-renders when history is hydrated from localStorage const viewingHistory = useHistoryStore(state => state.viewingHistory); const searchParams = useSearchParams(); const { addToHistory } = useHistoryStore(); // Get video metadata from URL params const source = searchParams.get('source') || ''; const title = searchParams.get('title') || '未知视频'; // Get saved progress for this video const getSavedProgress = () => { if (!videoId) return 0; // Directly check HistoryStore for progress // This is the single source of truth for playback resumption const historyItem = viewingHistory.find(item => // Loose match for videoId (string vs number) item.videoId.toString() === videoId?.toString() && item.source === source && item.episodeIndex === currentEpisode ); return historyItem ? historyItem.playbackPosition : 0; }; // Handle time updates and save progress const handleTimeUpdate = (currentTime: number, duration: number) => { if (!videoId || !playUrl || duration === 0) return; // Save progress every few seconds if (currentTime > 1) { addToHistory( videoId, title, playUrl, currentEpisode, source, currentTime, duration, undefined, [] ); } }; // Handle video errors const handleVideoError = (error: string) => { console.error('Video playback error:', error); // Auto-retry with proxy if not already using it if (!useProxy) { console.log('Attempting to retry with proxy...'); setUseProxy(true); setShouldAutoPlay(true); // Force autoplay after proxy retry setVideoError(''); return; } setVideoError(error); }; const finalPlayUrl = useProxy ? `/api/proxy?url=${encodeURIComponent(playUrl)}` : playUrl; if (!playUrl) { return (

暂无播放源

); } return ( {videoError ? (

播放失败

{videoError}

) : ( )}
); }