Skip to content

Commit

Permalink
Merge pull request #3073 from metabrainz/youtube-player-state
Browse files Browse the repository at this point in the history
Fix absent state in YoutubePlayer
  • Loading branch information
MonkeyDo authored Dec 13, 2024
2 parents 5719458 + c91d83a commit 5538b9b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
15 changes: 3 additions & 12 deletions frontend/js/src/common/brainzplayer/YoutubePlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ import {
import { DataSourceProps, DataSourceType } from "./BrainzPlayer";
import { dataSourcesInfo } from "../../settings/brainzplayer/BrainzPlayerSettings";

export type YoutubePlayerState = {
currentListen?: Listen;
};

export type YoutubePlayerProps = DataSourceProps & {
youtubeUser?: YoutubeUser;
refreshYoutubeToken: () => Promise<string>;
Expand All @@ -41,8 +37,7 @@ type ExtendedYoutubePlayer = {
getVideoData?: () => { video_id?: string; author: string; title: string };
} & YT.Player;

export default class YoutubePlayer
extends React.Component<YoutubePlayerProps, YoutubePlayerState>
export default class YoutubePlayer extends React.Component<YoutubePlayerProps>
implements DataSourceType {
static getVideoIDFromListen(listen: Listen | JSPFTrack): string | undefined {
// This may be either video ID or video link.
Expand Down Expand Up @@ -136,7 +131,7 @@ export default class YoutubePlayer
};

updateVideoInfo = (): void => {
let title;
let title = "";
let images: MediaImage[] = [];
const { onTrackInfoChange, onDurationChange } = this.props;
const videoData =
Expand All @@ -148,10 +143,6 @@ export default class YoutubePlayer
videoId = videoData.video_id as string;
images = YoutubePlayer.getThumbnailsFromVideoid(videoId);
videoUrl = YoutubePlayer.getURLFromVideoID(videoId);
} else {
// Fallback to track name from the listen we are playing
const { currentListen } = this.state;
title = getTrackName(currentListen);
}
onTrackInfoChange(title, videoUrl, undefined, undefined, images);
const duration = this.youtubePlayer?.getDuration();
Expand Down Expand Up @@ -184,7 +175,7 @@ export default class YoutubePlayer
// The player info is sometimes missing a title initially.
// We fallback to getting it with getVideoData method once the information is loaded in the player
if (!title || !videoId) {
setTimeout(this.updateVideoInfo.bind(this), 2000);
setTimeout(this.updateVideoInfo, 2000);
} else {
const images: MediaImage[] = YoutubePlayer.getThumbnailsFromVideoid(
videoId
Expand Down
7 changes: 2 additions & 5 deletions frontend/js/tests/common/brainzplayer/YoutubePlayer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import * as React from "react";
import { mount, ReactWrapper, shallow, ShallowWrapper } from "enzyme";
import { mount, shallow } from "enzyme";

import { act } from "react-dom/test-utils";
import YoutubePlayer, {
YoutubePlayerProps,
YoutubePlayerState,
} from "../../../src/common/brainzplayer/YoutubePlayer";
import YoutubePlayer from "../../../src/common/brainzplayer/YoutubePlayer";
import { DataSourceTypes } from "../../../src/common/brainzplayer/BrainzPlayer";
import APIService from "../../../src/utils/APIService";

Expand Down

0 comments on commit 5538b9b

Please sign in to comment.