diff --git a/src/components/buttons/playButton.tsx b/src/components/buttons/playButton.tsx index 7cb2d2a..016afd6 100644 --- a/src/components/buttons/playButton.tsx +++ b/src/components/buttons/playButton.tsx @@ -629,6 +629,44 @@ const PlayButton = ({ e.stopPropagation(); itemQuery.mutate(currentEpisodeId); }; + const currentEpisode = useQuery({ + queryKey: ["playButton", "currentEpisode", item?.Id], + queryFn: async () => { + if (!api) { + throw new Error("API is not available"); + } + if (!userId) { + throw new Error("User ID is not available"); + } + if (!item.Id) { + throw new Error("Item ID is not available"); + } + let data: BaseItemDtoQueryResult | null = null; + const continueWatching = await getItemsApi(api).getResumeItems({ + userId: userId, + limit: 1, + mediaTypes: ["Video"], + parentId: item.Id, + enableUserData: true, + fields: [ItemFields.MediaStreams, ItemFields.MediaSources], + }); + const nextUp = await getTvShowsApi(api).getNextUp({ + userId: userId, + parentId: item.Id, + limit: 1, + }); + if ((continueWatching.data.Items?.length ?? 0) > 0) { + data = continueWatching.data; + } else if ((nextUp.data.Items?.length ?? 0) > 0) { + data = nextUp.data; + } else { + return null; + } + return data; + }, + // enabled: itemType === BaseItemKind.Series, + }); + if (iconOnly) { return ( //@ts-ignore @@ -636,11 +674,24 @@ const PlayButton = ({ color="primary" aria-label="Play" className={className} - onClick={(e) => - item.Type === "Episode" ? handleClick(e, item.Id) : handleClick(e) - } + onClick={(e) => { + if (item.Type === "Episode") { + handleClick(e, item.Id) + } else if (itemType === BaseItemKind.Series) { + handleClick(e, currentEpisode.data?.Items?.[0]?.Id) + } else { + handleClick(e) + } + }} sx={sx} size={size} + disabled={ + currentEpisode.isPending || + (itemType === BaseItemKind.Series && + (!currentEpisode.data || + !currentEpisode.data.Items || + currentEpisode.data.Items.length === 0)) + } {...buttonProps} >
{ - if (!api) { - throw new Error("API is not available"); - } - if (!userId) { - throw new Error("User ID is not available"); - } - if (!item.Id) { - throw new Error("Item ID is not available"); - } - let data: BaseItemDtoQueryResult | null = null; - const continueWatching = await getItemsApi(api).getResumeItems({ - userId: userId, - limit: 1, - mediaTypes: ["Video"], - parentId: item.Id, - enableUserData: true, - fields: [ItemFields.MediaStreams, ItemFields.MediaSources], - }); - const nextUp = await getTvShowsApi(api).getNextUp({ - userId: userId, - parentId: item.Id, - limit: 1, - }); - if ((continueWatching.data.Items?.length ?? 0) > 0) { - data = continueWatching.data; - } else if ((nextUp.data.Items?.length ?? 0) > 0) { - data = nextUp.data; - } else { - return null; - } - return data; - }, - // enabled: itemType === BaseItemKind.Series, - }); return (
handleClick(e, currentEpisode.data?.Items?.[0]?.Id)} + onClick={(e) => { + const episodeId = currentEpisode.data?.Items?.[0]?.Id; + if (episodeId) { + handleClick(e, episodeId); + } + }} startIcon={
- Watch S{currentEpisode.data?.Items?.[0].ParentIndexNumber ?? 1}E - {currentEpisode.data?.Items?.[0]?.IndexNumber ?? 1} - - (currentEpisode.data?.Items?.[0].UserData?.PlayedPercentage ?? - 100) && - (currentEpisode.data?.Items?.[0].UserData?.PlayedPercentage ?? - 0) > 0 - ? currentEpisode.data?.Items?.[0].UserData?.PlayedPercentage - : 0 - } - /> + {currentEpisode.isPending + ? "Loading..." + : !currentEpisode.data || + !currentEpisode.data.Items || + currentEpisode.data.Items.length === 0 + ? "No episodes to watch found" + : ( + <> + Watch S{currentEpisode.data.Items[0].ParentIndexNumber ?? 1}E + {currentEpisode.data.Items[0]?.IndexNumber ?? 1} + + (currentEpisode.data.Items[0].UserData?.PlayedPercentage ?? + 100) && + (currentEpisode.data.Items[0].UserData?.PlayedPercentage ?? + 0) > 0 + ? currentEpisode.data.Items[0].UserData?.PlayedPercentage + : 0 + } + /> + + ) + } - {(currentEpisode.data?.Items?.[0].UserData?.PlaybackPositionTicks ?? + {(currentEpisode.data?.Items?.[0]?.UserData?.PlaybackPositionTicks ?? 0) > 0 && ( {getRuntimeCompact( - (currentEpisode.data?.Items?.[0].RunTimeTicks ?? 0) - - (currentEpisode.data?.Items?.[0].UserData + (currentEpisode.data?.Items?.[0]?.RunTimeTicks ?? 0) - + (currentEpisode.data?.Items?.[0]?.UserData ?.PlaybackPositionTicks ?? 0), )}{" "} left