From 1b5f591a7ab2c09d0edd47fa524e534b0912fc70 Mon Sep 17 00:00:00 2001 From: kuekhaoyang Date: Sat, 22 Nov 2025 15:53:05 +0800 Subject: [PATCH] feat: Integrate video playback progress with history store and ensure seeking on late initialization. --- components/player/VideoPlayer.tsx | 26 ++++++++++++++----- .../hooks/desktop/usePlaybackControls.ts | 13 +++++++++- .../hooks/mobile/useMobilePlaybackControls.ts | 11 ++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/components/player/VideoPlayer.tsx b/components/player/VideoPlayer.tsx index 322e374..3ba4aa2 100644 --- a/components/player/VideoPlayer.tsx +++ b/components/player/VideoPlayer.tsx @@ -17,18 +17,30 @@ interface VideoPlayerProps { export function VideoPlayer({ playUrl, videoId, currentEpisode, onBack }: VideoPlayerProps) { const [videoError, setVideoError] = useState(''); + // 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; - const savedTime = localStorage.getItem(`video_progress_${videoId}_${currentEpisode}`); - return savedTime ? parseFloat(savedTime) : 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 @@ -74,7 +86,7 @@ export function VideoPlayer({ playUrl, videoId, currentEpisode, onBack }: VideoP {videoError ? (
-
播放失败

{videoError}

- -