From 591fce7e5cfc3c4d51fd3e832de41f897af08d96 Mon Sep 17 00:00:00 2001 From: Prayag Prajapati Date: Sat, 30 Nov 2024 13:47:03 +0530 Subject: [PATCH] fix: playback error if subtitle is not set --- src/components/buttons/playButton.tsx | 35 +++++++++++--- src/routes/_api/episode/$id.tsx | 10 ++-- src/utils/methods/getSubtitles.ts | 10 +++- src/utils/store/playback.ts | 62 +++++++++++++++---------- src/utils/types/subtitlePlaybackInfo.ts | 2 +- 5 files changed, 79 insertions(+), 40 deletions(-) diff --git a/src/components/buttons/playButton.tsx b/src/components/buttons/playButton.tsx index 3a35c43..85494ab 100644 --- a/src/components/buttons/playButton.tsx +++ b/src/components/buttons/playButton.tsx @@ -145,8 +145,11 @@ const PlayButton = ({ seasonId: item.SeasonId, // startItemId: item.Id, }); + if (result.data.Items?.map((i) => i.Id).indexOf(currentEpisodeId) === -1) { - throw new Error("Episode not found, index return -1"); + throw new Error( + `Episode having id ${currentEpisodeId} not found, index return -1`, + ); } index = result.data.Items?.map((i) => i.Id).indexOf(currentEpisodeId) ?? 0; mediaSource = await getMediaInfoApi(api).getPostedPlaybackInfo({ @@ -183,10 +186,23 @@ const PlayButton = ({ enableUserData: true, userId: userId, }); - if (result.data.Items?.map((i) => i.Id).indexOf(currentEpisodeId) === -1) { - throw new Error("Episode not found, index return -1"); + console.log(result.data); + if ( + currentEpisodeId && + result.data.Items?.map((i) => i.Id).indexOf(currentEpisodeId) === + -1 + ) { + throw new Error( + `Episode having id=${currentEpisodeId} not found, index return -1`, + ); + } + if (!currentEpisodeId) { + index = 0; + } else { + index = + result.data.Items?.map((i) => i.Id).indexOf(currentEpisodeId) ?? + 0; } - index = result.data.Items?.map((i) => i.Id).indexOf(currentEpisodeId) ?? 0; if ( result.data.Items?.[index]?.Id && result.data.Items?.[index].MediaSources?.[0]?.Id @@ -372,6 +388,7 @@ const PlayButton = ({ currentSubTrack, result?.mediaSource?.MediaSources?.[0]?.MediaStreams, ); + console.log(subtitle); // URL generation const urlOptions: URLSearchParams = { //@ts-ignore @@ -430,7 +447,7 @@ const PlayButton = ({ }); const handleClick = ( e: MouseEvent, - currentEpisodeId: string | undefined, + currentEpisodeId?: string | undefined, ) => { e.stopPropagation(); itemQuery.mutate(currentEpisodeId); @@ -442,7 +459,9 @@ const PlayButton = ({ color="primary" aria-label="Play" className={className} - onClick={(e) => handleClick(e, item.Id)} + onClick={(e) => + item.Type === "Episode" ? handleClick(e, item.Id) : handleClick(e) + } sx={sx} size={size} {...buttonProps} @@ -579,7 +598,9 @@ const PlayButton = ({