From b7e23db750072cef1d2d1ef2e6360d4ac7af99d0 Mon Sep 17 00:00:00 2001 From: MerlinScheurer Date: Wed, 5 Mar 2025 18:34:57 +0100 Subject: [PATCH] Fix EmbeddableVideoPlayer scrollIntoView v4 --- .../src/components/EmbeddableVideoPlayer.tsx | 22 ++++++++----------- frontend/src/pages/ChannelVideo.tsx | 7 +++--- frontend/src/pages/Home.tsx | 4 +--- frontend/src/pages/Playlist.tsx | 12 ++-------- frontend/src/pages/Search.tsx | 7 +++--- 5 files changed, 20 insertions(+), 32 deletions(-) diff --git a/frontend/src/components/EmbeddableVideoPlayer.tsx b/frontend/src/components/EmbeddableVideoPlayer.tsx index 813953dd..d3be212d 100644 --- a/frontend/src/components/EmbeddableVideoPlayer.tsx +++ b/frontend/src/components/EmbeddableVideoPlayer.tsx @@ -1,4 +1,4 @@ -import { useEffect, useLayoutEffect, useRef, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { VideoResponseType } from '../pages/Video'; import VideoPlayer from './VideoPlayer'; import loadVideoById from '../api/loader/loadVideoById'; @@ -21,12 +21,11 @@ type Playlist = { type PlaylistList = Playlist[]; type EmbeddableVideoPlayerProps = { - videoId: string; + videoId: string | null; }; const EmbeddableVideoPlayer = ({ videoId }: EmbeddableVideoPlayerProps) => { const inlinePlayerRef = useRef(null); - const prevVideoId = useRef(null); const { appSettingsConfig } = useAppSettingsStore(); const [, setSearchParams] = useSearchParams(); @@ -38,6 +37,12 @@ const EmbeddableVideoPlayer = ({ videoId }: EmbeddableVideoPlayerProps) => { useEffect(() => { (async () => { + if (!videoId) { + return; + } + + inlinePlayerRef.current?.scrollIntoView(); + if (refresh || videoId !== videoResponse?.youtube_id) { const videoResponse = await loadVideoById(videoId); @@ -72,16 +77,7 @@ const EmbeddableVideoPlayer = ({ videoId }: EmbeddableVideoPlayerProps) => { // eslint-disable-next-line react-hooks/exhaustive-deps }, [videoId, refresh]); - useLayoutEffect(() => { - if (videoId !== prevVideoId.current) { - setTimeout(() => { - inlinePlayerRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' }); - }, 0); - } - prevVideoId.current = videoId; // Update the previous video ID - }, [videoId]); - - if (videoResponse === undefined) { + if (videoResponse === undefined || videoId === null) { return
; } diff --git a/frontend/src/pages/ChannelVideo.tsx b/frontend/src/pages/ChannelVideo.tsx index 39141e99..3a22aee2 100644 --- a/frontend/src/pages/ChannelVideo.tsx +++ b/frontend/src/pages/ChannelVideo.tsx @@ -47,7 +47,6 @@ const ChannelVideo = ({ videoType }: ChannelVideoProps) => { const pagination = videoResponse?.paginate; const hasVideos = videoResponse?.data?.length !== 0; - const showEmbeddedVideo = videoId !== null; const view = userConfig.view_style_home; const isGridView = view === ViewStyles.grid; @@ -81,7 +80,6 @@ const ChannelVideo = ({ videoType }: ChannelVideoProps) => { channelId, pagination?.current_page, videoType, - showEmbeddedVideo, ]); if (!channel) { @@ -152,10 +150,13 @@ const ChannelVideo = ({ videoType }: ChannelVideoProps) => {
+
- {showEmbeddedVideo && } + + +
{!hasVideos && ( diff --git a/frontend/src/pages/Home.tsx b/frontend/src/pages/Home.tsx index 14e94bca..70a6fb70 100644 --- a/frontend/src/pages/Home.tsx +++ b/frontend/src/pages/Home.tsx @@ -119,7 +119,6 @@ const Home = () => { const continueVideos = continueVideoResponse?.data; const hasVideos = videoResponse?.data?.length !== 0; - const showEmbeddedVideo = videoId !== null; const isGridView = userConfig.view_style_home === ViewStyles.grid; const gridView = isGridView ? `boxed-${userConfig.grid_items}` : ''; @@ -153,7 +152,6 @@ const Home = () => { userConfig.hide_watched, currentPage, pagination?.current_page, - showEmbeddedVideo, ]); return ( @@ -161,7 +159,7 @@ const Home = () => { TubeArchivist - {showEmbeddedVideo && } +
{continueVideos && continueVideos.length > 0 && ( diff --git a/frontend/src/pages/Playlist.tsx b/frontend/src/pages/Playlist.tsx index b82dfecb..31719d9c 100644 --- a/frontend/src/pages/Playlist.tsx +++ b/frontend/src/pages/Playlist.tsx @@ -56,7 +56,6 @@ const Playlist = () => { const palylistEntries = playlistResponse?.playlist_entries; const videoArchivedCount = Number(palylistEntries?.filter(video => video.downloaded).length); const videoInPlaylistCount = pagination?.total_hits; - const showEmbeddedVideo = videoId !== null; const view = userConfig.view_style_home; const gridItems = userConfig.grid_items; @@ -87,14 +86,7 @@ const Playlist = () => { setRefresh(false); })(); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [ - playlistId, - userConfig.hide_watched, - refresh, - currentPage, - pagination?.current_page, - showEmbeddedVideo, - ]); + }, [playlistId, userConfig.hide_watched, refresh, currentPage, pagination?.current_page]); if (!playlistId || !playlist) { return `Playlist ${playlistId} not found!`; @@ -301,7 +293,7 @@ const Playlist = () => { />
- {showEmbeddedVideo && } +
diff --git a/frontend/src/pages/Search.tsx b/frontend/src/pages/Search.tsx index b391ee18..eb1d13b5 100644 --- a/frontend/src/pages/Search.tsx +++ b/frontend/src/pages/Search.tsx @@ -56,7 +56,6 @@ const Search = () => { const playlistList = searchResults?.results.playlist_results; const fulltextList = searchResults?.results.fulltext_results; const queryType = searchResults?.queryType; - const showEmbeddedVideo = videoId !== null; const hasSearchQuery = searchTerm.length > 0; const hasVideos = Number(videoList?.length) > 0; @@ -90,7 +89,7 @@ const Search = () => { } else { setSearchResults(EmptySearchResponse); } - }, [debouncedSearchTerm, refresh, showEmbeddedVideo]); + }, [debouncedSearchTerm, refresh]); const fetchResults = async (searchQuery: string) => { const searchResults = await loadSearch(searchQuery); @@ -102,7 +101,9 @@ const Search = () => { return ( <> TubeArchivist - {showEmbeddedVideo && } + + +

Search your Archive