feat: show current episode in PlayButton
This commit is contained in:
3 files changed
+146
-18
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
import React, { memo, type MouseEventHandler } from "react"; // Import memo
|
import React, { memo, type MouseEvent } from "react"; // Import memo
|
||||||
|
|
||||||
import Button, { type ButtonProps } from "@mui/material/Button";
|
import Button, { type ButtonProps } from "@mui/material/Button";
|
||||||
import Fab from "@mui/material/Fab";
|
import Fab from "@mui/material/Fab";
|
||||||
@@ -20,7 +20,7 @@ import {
|
|||||||
import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api";
|
import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api";
|
||||||
import { getPlaylistsApi } from "@jellyfin/sdk/lib/utils/api/playlists-api";
|
import { getPlaylistsApi } from "@jellyfin/sdk/lib/utils/api/playlists-api";
|
||||||
import { getTvShowsApi } from "@jellyfin/sdk/lib/utils/api/tv-shows-api";
|
import { getTvShowsApi } from "@jellyfin/sdk/lib/utils/api/tv-shows-api";
|
||||||
import { useMutation } from "@tanstack/react-query";
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
import { useNavigate } from "@tanstack/react-router";
|
import { useNavigate } from "@tanstack/react-router";
|
||||||
import { useSnackbar } from "notistack";
|
import { useSnackbar } from "notistack";
|
||||||
|
|
||||||
@@ -37,6 +37,7 @@ import { usePhotosPlayback } from "@/utils/store/photosPlayback";
|
|||||||
import { getMediaInfoApi } from "@jellyfin/sdk/lib/utils/api/media-info-api";
|
import { getMediaInfoApi } from "@jellyfin/sdk/lib/utils/api/media-info-api";
|
||||||
import { getMediaSegmentsApi } from "@jellyfin/sdk/lib/utils/api/media-segments-api";
|
import { getMediaSegmentsApi } from "@jellyfin/sdk/lib/utils/api/media-segments-api";
|
||||||
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
|
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
|
||||||
|
import { LoadingButton } from "@mui/lab";
|
||||||
import type { SxProps } from "@mui/material";
|
import type { SxProps } from "@mui/material";
|
||||||
import type { AxiosResponse } from "axios";
|
import type { AxiosResponse } from "axios";
|
||||||
|
|
||||||
@@ -109,11 +110,12 @@ const PlayButton = ({
|
|||||||
|
|
||||||
const itemQuery = useMutation({
|
const itemQuery = useMutation({
|
||||||
mutationKey: ["playButton", item?.Id, userId],
|
mutationKey: ["playButton", item?.Id, userId],
|
||||||
mutationFn: async () => {
|
mutationFn: async (currentEpisodeId?: string) => {
|
||||||
setPlaybackDataLoading(true);
|
setPlaybackDataLoading(true);
|
||||||
let result: undefined | AxiosResponse<BaseItemDtoQueryResult, any>;
|
let result: undefined | AxiosResponse<BaseItemDtoQueryResult, any>;
|
||||||
let mediaSource: undefined | AxiosResponse<PlaybackInfoResponse, any>;
|
let mediaSource: undefined | AxiosResponse<PlaybackInfoResponse, any>;
|
||||||
let introInfo: undefined | MediaSegmentDtoQueryResult;
|
let introInfo: undefined | MediaSegmentDtoQueryResult;
|
||||||
|
let index = 0;
|
||||||
if (!api) {
|
if (!api) {
|
||||||
throw new Error("API is not available");
|
throw new Error("API is not available");
|
||||||
}
|
}
|
||||||
@@ -143,7 +145,7 @@ const PlayButton = ({
|
|||||||
seasonId: item.SeasonId,
|
seasonId: item.SeasonId,
|
||||||
// startItemId: item.Id,
|
// startItemId: item.Id,
|
||||||
});
|
});
|
||||||
const index = result.data.Items?.map((i) => i.Id).indexOf(item.Id) ?? 0;
|
index = result.data.Items?.map((i) => i.Id).indexOf(item.Id) ?? 0;
|
||||||
mediaSource = await getMediaInfoApi(api).getPostedPlaybackInfo({
|
mediaSource = await getMediaInfoApi(api).getPostedPlaybackInfo({
|
||||||
audioStreamIndex: currentAudioTrack,
|
audioStreamIndex: currentAudioTrack,
|
||||||
subtitleStreamIndex:
|
subtitleStreamIndex:
|
||||||
@@ -178,26 +180,32 @@ const PlayButton = ({
|
|||||||
enableUserData: true,
|
enableUserData: true,
|
||||||
userId: userId,
|
userId: userId,
|
||||||
});
|
});
|
||||||
|
index =
|
||||||
|
result.data.Items?.map((i) => i.Id).indexOf(
|
||||||
|
currentEpisodeId ?? "",
|
||||||
|
) ?? 0;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
result.data.Items?.[0]?.Id &&
|
result.data.Items?.[index]?.Id &&
|
||||||
result.data.Items?.[0].MediaSources?.[0]?.Id
|
result.data.Items?.[index].MediaSources?.[0]?.Id
|
||||||
) {
|
) {
|
||||||
mediaSource = await getMediaInfoApi(api).getPostedPlaybackInfo({
|
mediaSource = await getMediaInfoApi(api).getPostedPlaybackInfo({
|
||||||
audioStreamIndex: currentAudioTrack,
|
audioStreamIndex: currentAudioTrack,
|
||||||
subtitleStreamIndex:
|
subtitleStreamIndex:
|
||||||
currentSubTrack === "nosub" ? -1 : currentSubTrack,
|
currentSubTrack === "nosub" ? -1 : currentSubTrack,
|
||||||
itemId: result.data.Items?.[0].Id,
|
itemId: result.data.Items?.[index].Id ?? "",
|
||||||
startTimeTicks:
|
startTimeTicks:
|
||||||
result.data.Items?.[0].UserData?.PlaybackPositionTicks,
|
result.data.Items?.[0].UserData?.PlaybackPositionTicks,
|
||||||
userId: userId,
|
userId: userId,
|
||||||
mediaSourceId: result.data.Items?.[0].MediaSources?.[0].Id,
|
mediaSourceId:
|
||||||
|
result.data.Items?.[index].MediaSources?.[0].Id ?? "",
|
||||||
playbackInfoDto: {
|
playbackInfoDto: {
|
||||||
DeviceProfile: playbackProfile,
|
DeviceProfile: playbackProfile,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
introInfo = (
|
introInfo = (
|
||||||
await getMediaSegmentsApi(api).getItemSegments({
|
await getMediaSegmentsApi(api).getItemSegments({
|
||||||
itemId: result.data.Items?.[0].Id,
|
itemId: result.data.Items?.[index].Id ?? "",
|
||||||
})
|
})
|
||||||
)?.data;
|
)?.data;
|
||||||
}
|
}
|
||||||
@@ -289,7 +297,12 @@ const PlayButton = ({
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return { item: result?.data, mediaSource: mediaSource?.data, introInfo };
|
return {
|
||||||
|
item: result?.data,
|
||||||
|
mediaSource: mediaSource?.data,
|
||||||
|
introInfo,
|
||||||
|
episodeIndex: index,
|
||||||
|
};
|
||||||
},
|
},
|
||||||
onSuccess: (result: PlayResult | null) => {
|
onSuccess: (result: PlayResult | null) => {
|
||||||
if (!api) {
|
if (!api) {
|
||||||
@@ -344,7 +357,7 @@ const PlayButton = ({
|
|||||||
console.error("No media source found");
|
console.error("No media source found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const episodeIndex = item.IndexNumber ? item.IndexNumber - 1 : 0;
|
const episodeIndex = result.episodeIndex;
|
||||||
|
|
||||||
// Creates a queue containing all Episodes for a particular season(series) or collection of movies
|
// Creates a queue containing all Episodes for a particular season(series) or collection of movies
|
||||||
const queue = result?.item?.Items;
|
const queue = result?.item?.Items;
|
||||||
@@ -384,6 +397,9 @@ const PlayButton = ({
|
|||||||
playItemValue = result?.item?.Items?.[0];
|
playItemValue = result?.item?.Items?.[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const startPosition =
|
||||||
|
result?.item?.Items?.[episodeIndex].UserData?.PlaybackPositionTicks;
|
||||||
|
|
||||||
playItem(
|
playItem(
|
||||||
itemName,
|
itemName,
|
||||||
episodeTitle,
|
episodeTitle,
|
||||||
@@ -392,8 +408,8 @@ const PlayButton = ({
|
|||||||
result?.mediaSource?.MediaSources?.[0].Container ?? "mkv",
|
result?.mediaSource?.MediaSources?.[0].Container ?? "mkv",
|
||||||
playbackUrl,
|
playbackUrl,
|
||||||
userId ?? "",
|
userId ?? "",
|
||||||
item.UserData?.PlaybackPositionTicks,
|
startPosition,
|
||||||
item.RunTimeTicks,
|
result?.item?.Items?.[episodeIndex].RunTimeTicks,
|
||||||
playItemValue ?? item,
|
playItemValue ?? item,
|
||||||
queue,
|
queue,
|
||||||
episodeIndex,
|
episodeIndex,
|
||||||
@@ -415,9 +431,12 @@ const PlayButton = ({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const handleClick: MouseEventHandler<HTMLButtonElement> = (e) => {
|
const handleClick = (
|
||||||
|
e: MouseEvent<HTMLAnchorElement | MouseEvent>,
|
||||||
|
currentEpisodeId: string | undefined,
|
||||||
|
) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
itemQuery.mutate();
|
itemQuery.mutate(currentEpisodeId);
|
||||||
};
|
};
|
||||||
if (iconOnly) {
|
if (iconOnly) {
|
||||||
return (
|
return (
|
||||||
@@ -442,6 +461,114 @@ const PlayButton = ({
|
|||||||
</Fab>
|
</Fab>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (itemType === BaseItemKind.Series) {
|
||||||
|
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 | undefined;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
enabled: itemType === BaseItemKind.Series,
|
||||||
|
});
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="play-button"
|
||||||
|
style={{
|
||||||
|
width: "auto",
|
||||||
|
position: "relative",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<LoadingButton
|
||||||
|
loading={currentEpisode.isPending}
|
||||||
|
className={className ?? "play-button"}
|
||||||
|
variant="contained"
|
||||||
|
onClick={(e) => handleClick(e, currentEpisode.data?.Items?.[0]?.Id)}
|
||||||
|
startIcon={
|
||||||
|
<div
|
||||||
|
className="material-symbols-rounded fill"
|
||||||
|
style={{
|
||||||
|
zIndex: 1,
|
||||||
|
fontSize: "2em",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
play_arrow
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
{...buttonProps}
|
||||||
|
sx={{
|
||||||
|
position: "relative",
|
||||||
|
overflow: "hidden",
|
||||||
|
}}
|
||||||
|
//@ts-ignore - white color is a custom color in the theme which mui's types don't know about
|
||||||
|
color="white"
|
||||||
|
size={size}
|
||||||
|
>
|
||||||
|
Watch S{currentEpisode.data?.Items?.[0].ParentIndexNumber}E
|
||||||
|
{currentEpisode.data?.Items?.[0]?.IndexNumber ?? 1}
|
||||||
|
<MemoizedLinearProgress
|
||||||
|
//@ts-ignore
|
||||||
|
value={
|
||||||
|
100 >
|
||||||
|
(currentEpisode.data?.Items?.[0].UserData?.PlayedPercentage ??
|
||||||
|
100) &&
|
||||||
|
(currentEpisode.data?.Items?.[0].UserData?.PlayedPercentage ??
|
||||||
|
0) > 0
|
||||||
|
? currentEpisode.data?.Items?.[0].UserData?.PlayedPercentage
|
||||||
|
: 0
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</LoadingButton>
|
||||||
|
{(currentEpisode.data?.Items?.[0].UserData?.PlaybackPositionTicks ??
|
||||||
|
0) > 0 && (
|
||||||
|
<Typography
|
||||||
|
sx={{
|
||||||
|
opacity: 0.8,
|
||||||
|
position: "absolute",
|
||||||
|
bottom: "-1.8em",
|
||||||
|
left: "50%",
|
||||||
|
transform: "translate(-50%)",
|
||||||
|
}}
|
||||||
|
variant="caption"
|
||||||
|
>
|
||||||
|
{getRuntimeCompact(
|
||||||
|
(currentEpisode.data?.Items?.[0].RunTimeTicks ?? 0) -
|
||||||
|
(currentEpisode.data?.Items?.[0].UserData
|
||||||
|
?.PlaybackPositionTicks ?? 0),
|
||||||
|
)}{" "}
|
||||||
|
left
|
||||||
|
</Typography>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className="play-button"
|
className="play-button"
|
||||||
@@ -505,8 +632,8 @@ const PlayButton = ({
|
|||||||
variant="caption"
|
variant="caption"
|
||||||
>
|
>
|
||||||
{getRuntimeCompact(
|
{getRuntimeCompact(
|
||||||
item.RunTimeTicks ??
|
(item.RunTimeTicks ?? 0) -
|
||||||
0 - (item.UserData?.PlaybackPositionTicks ?? 0),
|
(item.UserData?.PlaybackPositionTicks ?? 0),
|
||||||
)}{" "}
|
)}{" "}
|
||||||
left
|
left
|
||||||
</Typography>
|
</Typography>
|
||||||
|
|||||||
@@ -607,7 +607,7 @@ function SeriesTitlePage() {
|
|||||||
<PlayButton
|
<PlayButton
|
||||||
itemId={item.data.Id}
|
itemId={item.data.Id}
|
||||||
item={item.data}
|
item={item.data}
|
||||||
itemType={item.data.Type}
|
itemType={BaseItemKind.Series}
|
||||||
itemUserData={item.data.UserData}
|
itemUserData={item.data.UserData}
|
||||||
currentAudioTrack={0}
|
currentAudioTrack={0}
|
||||||
currentVideoTrack={0}
|
currentVideoTrack={0}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ interface PlayResult {
|
|||||||
item: BaseItemDtoQueryResult | undefined;
|
item: BaseItemDtoQueryResult | undefined;
|
||||||
mediaSource: PlaybackInfoResponse | undefined;
|
mediaSource: PlaybackInfoResponse | undefined;
|
||||||
introInfo: MediaSegmentDtoQueryResult | undefined;
|
introInfo: MediaSegmentDtoQueryResult | undefined;
|
||||||
|
episodeIndex: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default PlayResult;
|
export default PlayResult;
|
||||||
Reference in new issue
Block a user