Skip to content

Commit

Permalink
Use window reload to fix yt player on first video bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ras1 committed Sep 2, 2024
1 parent b07e99c commit 62be474
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
11 changes: 4 additions & 7 deletions src/components/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@ export default function Player(props) {
function onError() {
const internalPlayer = props.playerRef.current?.getInternalPlayer()
const videoUrl = internalPlayer.getVideoUrl()
if (!videoUrl) return

const videoId = getVideoId(videoUrl)
const currentVideoIndex = internalPlayer.getPlaylistIndex()

const currentVideoIndex = internalPlayer.getPlaylistIndex()
// use JS destructuring syntax to exclude the `description` property from the rest of the object, it can be too long
const { description, ...destructuredVideo } = props.videos[currentVideoIndex]
const errorMessage = `BROKEN VIDEO: ${videoId}, videoUrl: ${videoUrl}, video: ${JSON.stringify(destructuredVideo)}`

const errorMessage = `BROKEN VIDEO: ${videoId}, index: ${currentVideoIndex}, videoUrl: ${videoUrl}, video: ${JSON.stringify(destructuredVideo)}`
console.log(errorMessage)
Honeybadger.notify(errorMessage);

if (currentVideoIndex === 0) {
console.log("Recovering from broken first video...")
props.onErrorRecovery()
if (currentVideoIndex === 0 || !videoUrl) {
window.location.reload()
} else {
internalPlayer.nextVideo()
}
Expand Down
9 changes: 2 additions & 7 deletions src/hooks/UseVideoDataFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@ const initialFetchResult = {
export default function useVideoDataFetcher(selectedPlaylistIds) {
const [playlistIds, setPlaylistIds] = useState(selectedPlaylistIds)
const [fetchResult, setFetchResult] = useState(initialFetchResult)
const [refetchTrigger, setRefetchTrigger] = useState(false)

// Effect hook for fetching data when playlist IDs change
useEffect(() => {
fetchData(playlistIds, setFetchResult)
}, [playlistIds, refetchTrigger])
}, [playlistIds])

function triggerRefetch() {
setRefetchTrigger(!refetchTrigger)
}

return [fetchResult, setPlaylistIds, triggerRefetch]
return [fetchResult, setPlaylistIds]
}

async function fetchData(playlistIds, setFetchResult) {
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/UseVideoPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import useVideoDataFetcher from '../hooks/UseVideoDataFetcher'

export default function useVideoPlayer(selectedPlaylistIds) {
const [currentVideos, setCurrentVideos] = useState([])
const [videoFetchResult, setVideoFetchPlaylistIds, triggerRefetch] = useVideoDataFetcher(selectedPlaylistIds)
const [videoFetchResult, setVideoFetchPlaylistIds] = useVideoDataFetcher(selectedPlaylistIds)

useEffect(shuffleVideos, [videoFetchResult])

Expand All @@ -27,7 +27,6 @@ export default function useVideoPlayer(selectedPlaylistIds) {
return {
currentVideos,
setVideoFetchPlaylistIds,
triggerRefetch,
isLoaded: videoFetchResult.isLoaded
}
}
3 changes: 1 addition & 2 deletions src/pages/ShufflePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function ShufflePlayer() {
const [selectedPlaylistIds, setSelectedPlaylistIds] = useLocalStorage(AppConstants.SelectedPlaylistIdsKey, [])
const [playlists, setPlaylists] = usePlaylistDataFetcher(selectedPlaylistIds)

const { currentVideos, setVideoFetchPlaylistIds, triggerRefetch, isLoaded } = useVideoPlayer(selectedPlaylistIds)
const { currentVideos, setVideoFetchPlaylistIds, isLoaded } = useVideoPlayer(selectedPlaylistIds)

if (!isLoaded) {
return <LoadingPlaceholder />
Expand All @@ -32,7 +32,6 @@ export default function ShufflePlayer() {
hideVideo={hideVideo}
playerRef={playerRef}
setCurrentVideo={setCurrentVideo}
onErrorRecovery={triggerRefetch}
/>

<CurrentVideoInfo currentVideo={currentVideo} />
Expand Down

0 comments on commit 62be474

Please sign in to comment.