From b163b60837c68fd8181afde7a1c903883dca7a0f Mon Sep 17 00:00:00 2001 From: Roland Date: Fri, 28 Oct 2022 08:12:25 -0600 Subject: [PATCH] Only scrolling to top when changing videos. Clearing old video when changing videos. --- app/src/components/Videos.js | 6 ++++-- app/src/hooks/customHooks.js | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/src/components/Videos.js b/app/src/components/Videos.js index 92c9d21b..10789f63 100644 --- a/app/src/components/Videos.js +++ b/app/src/components/Videos.js @@ -1,4 +1,4 @@ -import React, {useState} from 'react'; +import React, {useEffect, useState} from 'react'; import {Link, Route, Routes, useParams} from "react-router-dom"; import { defaultSearchOrder, @@ -22,7 +22,9 @@ import {deleteVideos} from "../api"; export function VideoWrapper() { const {videoId} = useParams(); const {videoFile, prevFile, nextFile, setFavorite} = useVideo(videoId); - scrollToTop(); + + // Scroll to the top when videoId changes. + useEffect(scrollToTop, [videoId]); return diff --git a/app/src/hooks/customHooks.js b/app/src/hooks/customHooks.js index 664e78dd..b50492e7 100644 --- a/app/src/hooks/customHooks.js +++ b/app/src/hooks/customHooks.js @@ -266,6 +266,12 @@ export const useVideo = (videoId) => { const [nextFile, setNextFile] = useState(null); const fetchVideo = async () => { + if (videoId && videoFile && videoFile.video.id !== videoId) { + // Video is changing. Clear the old video before fetching. + setVideoFile(null); + setPrevFile(null); + setNextFile(null); + } try { const [v, p, n] = await getVideo(videoId); setVideoFile(v);