/** @format */
import { useState, useEffect } from "react";
import PropTypes from "prop-types";
import Box from "@mui/material/Box";
import CircularProgress from "@mui/material/CircularProgress";
import Typography from "@mui/material/Typography";
import Stack from "@mui/material/Stack";
import Chip from "@mui/material/Chip";
import Divider from "@mui/material/Divider";
import LinearProgress from "@mui/material/LinearProgress";
import Tabs from "@mui/material/Tabs";
import Tab from "@mui/material/Tab";
import Grid2 from "@mui/material/Unstable_Grid2";
import Button from "@mui/material/Button";
import IconButton from "@mui/material/IconButton";
import Paper from "@mui/material/Paper";
import TableContainer from "@mui/material/TableContainer";
import Table from "@mui/material/Table";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import TableBody from "@mui/material/TableBody";
import TableCell from "@mui/material/TableCell";
import TextField from "@mui/material/TextField";
import MenuItem from "@mui/material/MenuItem";
import Link from "@mui/material/Link";
import { green, pink, yellow } from "@mui/material/colors";
import { AnimatePresence, motion } from "framer-motion";
import { Blurhash } from "react-blurhash";
import { useDispatch, useSelector } from "react-redux";
import { useParams, useNavigate, Link as RouterLink } from "react-router-dom";
import {
BaseItemKind,
ItemFields,
MediaStreamType,
} from "@jellyfin/sdk/lib/generated-client";
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
import { getLibraryApi } from "@jellyfin/sdk/lib/utils/api/library-api";
import { getTvShowsApi } from "@jellyfin/sdk/lib/utils/api/tv-shows-api";
import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api";
import { getPlaystateApi } from "@jellyfin/sdk/lib/utils/api/playstate-api";
import { getVideosApi } from "@jellyfin/sdk/lib/utils/api/videos-api";
import { useQuery } from "@tanstack/react-query";
import { MdiPlayOutline } from "../../components/icons/mdiPlayOutline";
import { MdiStar } from "../../components/icons/mdiStar";
import { MdiCheck } from "../../components/icons/mdiCheck";
import { MdiHeartOutline } from "../../components/icons/mdiHeartOutline";
import { MdiHeart } from "../../components/icons/mdiHeart";
import { MdiClockOutline } from "../../components/icons/mdiClockOutline";
import { getRuntimeMusic, getRuntimeFull, endsAt } from "../../utils/date/time";
import { TypeIconCollectionCard } from "../../components/utils/iconsCollection";
import { Card } from "../../components/card/card";
import { EpisodeCard } from "../../components/card/episodeCard";
import { CardScroller } from "../../components/cardScroller/cardScroller";
import "./item.module.scss";
import { EpisodeCardsSkeleton } from "../../components/skeleton/episodeCards";
import { ErrorNotice } from "../../components/notices/errorNotice/errorNotice";
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
{value === index && {children}}
);
}
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.number.isRequired,
value: PropTypes.number.isRequired,
};
function a11yProps(index) {
return {
id: `full-width-tab-${index}`,
"aria-controls": `full-width-tabpanel-${index}`,
};
}
const ItemDetail = () => {
const { id } = useParams();
const dispatch = useDispatch();
const navigate = useNavigate();
const [primageImageLoaded, setPrimaryImageLoaded] = useState(false);
const [backdropImageLoaded, setBackdropImageLoaded] = useState(false);
const user = useQuery({
queryKey: ["user"],
queryFn: async () => {
let usr = await getUserApi(window.api).getCurrentUser();
return usr.data;
},
networkMode: "always",
});
const item = useQuery({
queryKey: ["item", id],
queryFn: async () => {
const result = await getUserLibraryApi(window.api).getItem({
userId: user.data.Id,
itemId: id,
});
return result.data;
},
enabled: !!user.data,
networkMode: "always",
refetchOnWindowFocus: true,
});
const similarItems = useQuery({
queryKey: ["item", id, "similarItem"],
queryFn: async () => {
let result;
if (item.data.Type == "Movie") {
result = await getLibraryApi(window.api).getSimilarMovies({
userId: user.data.Id,
itemId: item.data.Id,
});
} else if (item.data.Type == "Series") {
result = await getLibraryApi(window.api).getSimilarShows({
userId: user.data.Id,
itemId: item.data.Id,
});
} else if (item.data.Type == "MusicAlbum") {
result = await getLibraryApi(window.api).getSimilarAlbums({
userId: user.data.Id,
itemId: item.data.Id,
});
} else if (item.data.Type == "MusicArtist") {
result = await getLibraryApi(window.api).getSimilarArtists({
userId: user.data.Id,
itemId: item.data.Id,
});
} else {
result = await getLibraryApi(window.api).getSimilarItems({
userId: user.data.Id,
itemId: item.data.Id,
});
}
return result.data;
},
enabled: item.isSuccess,
networkMode: "always",
refetchOnWindowFocus: true,
});
const seasons = useQuery({
queryKey: ["item", id, "seasons"],
queryFn: async () => {
const result = await getTvShowsApi(window.api).getSeasons({
seriesId: item.data.Id,
});
return result.data;
},
enabled: item.isSuccess && item.data.Type == "Series",
networkMode: "always",
refetchOnWindowFocus: true,
});
const nextUpEpisode = useQuery({
queryKey: ["item", id, "nextUp"],
queryFn: async () => {
const result = await getTvShowsApi(window.api).getNextUp({
userId: user.data.Id,
limit: 1,
parentId: item.data.Id,
disableFirstEpisode: true,
});
return result.data;
},
enabled: item.isSuccess && item.data.Type == "Series",
networkMode: "always",
refetchOnWindowFocus: true,
});
const [currentSeason, setCurrentSeason] = useState(0);
const episodes = useQuery({
queryKey: ["item", id, `season ${currentSeason + 1}`, "episodes"],
queryFn: async () => {
const result = await getItemsApi(window.api).getItems({
userId: user.data.Id,
parentId: seasons.data.Items[currentSeason].Id,
fields: [ItemFields.SeasonUserData, "Overview"],
});
return result.data;
},
enabled: seasons.isSuccess,
networkMode: "always",
refetchOnWindowFocus: true,
});
const personMovies = useQuery({
queryKey: ["item", id, "personMovies"],
queryFn: async () => {
const result = await getItemsApi(window.api).getItems({
userId: user.data.Id,
personIds: [id],
includeItemTypes: [BaseItemKind.Movie],
recursive: true,
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
sortOrder: ["Descending"],
});
return result.data;
},
enabled: item.isSuccess && item.data.Type == BaseItemKind.Person,
networkMode: "always",
});
const personShows = useQuery({
queryKey: ["item", id, "personShows"],
queryFn: async () => {
const result = await getItemsApi(window.api).getItems({
userId: user.data.Id,
personIds: [id],
includeItemTypes: [BaseItemKind.Series],
recursive: true,
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
sortOrder: ["Descending"],
});
return result.data;
},
enabled: item.isSuccess && item.data.Type == BaseItemKind.Person,
networkMode: "always",
});
const personBooks = useQuery({
queryKey: ["item", id, "personBooks"],
queryFn: async () => {
const result = await getItemsApi(window.api).getItems({
userId: user.data.Id,
personIds: [id],
includeItemTypes: [BaseItemKind.Book],
recursive: true,
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
sortOrder: ["Descending"],
});
return result.data;
},
enabled: item.isSuccess && item.data.Type == BaseItemKind.Person,
networkMode: "always",
});
const personPhotos = useQuery({
queryKey: ["item", id, "personPhotos"],
queryFn: async () => {
const result = await getItemsApi(window.api).getItems({
userId: user.data.Id,
personIds: [id],
includeItemTypes: [BaseItemKind.Photo],
recursive: true,
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
});
return result.data;
},
enabled: item.isSuccess && item.data.Type == BaseItemKind.Person,
networkMode: "always",
});
const personEpisodes = useQuery({
queryKey: ["item", id, "personEpisodes"],
queryFn: async () => {
const result = await getItemsApi(window.api).getItems({
userId: user.data.Id,
personIds: [id],
includeItemTypes: [BaseItemKind.Episode],
recursive: true,
fields: ["SeasonUserData", "Overview"],
sortBy: ["PremiereDate", "ProductionYear", "SortName"],
});
return result.data;
},
enabled: item.isSuccess && item.data.Type == BaseItemKind.Person,
networkMode: "always",
});
const musicTracks = useQuery({
queryKey: ["item", id, "musicTracks"],
queryFn: async () => {
const result = await getItemsApi(window.api).getItems({
userId: user.data.Id,
parentId: item.data.Id,
});
return result.data;
},
enabled: item.isSuccess && item.data.Type == BaseItemKind.MusicAlbum,
networkMode: "always",
});
const handleMarkPlayedOrunPlayed = async () => {
let result;
if (!item.data.UserData.Played) {
result = await getPlaystateApi(window.api).markPlayedItem({
userId: user.data.Id,
itemId: item.data.Id,
});
} else if (item.data.UserData.Played) {
result = await getPlaystateApi(window.api).markUnplayedItem({
userId: user.data.Id,
itemId: item.data.Id,
});
}
item.refetch();
};
const handleLiking = async () => {
let result;
if (item.data.UserData.IsFavorite) {
result = await getUserLibraryApi(window.api).unmarkFavoriteItem({
userId: user.data.Id,
itemId: item.data.Id,
});
} else if (!item.data.UserData.IsFavorite) {
result = await getUserLibraryApi(window.api).markFavoriteItem({
userId: user.data.Id,
itemId: item.data.Id,
});
}
item.refetch();
};
const handleLikingTrack = async (track) => {
let result;
if (track.UserData.IsFavorite) {
result = await getUserLibraryApi(window.api).unmarkFavoriteItem({
userId: user.data.Id,
itemId: track.Id,
});
} else if (!track.UserData.IsFavorite) {
result = await getUserLibraryApi(window.api).markFavoriteItem({
userId: user.data.Id,
itemId: track.Id,
});
}
item.refetch();
musicTracks.refetch();
};
const [activePersonTab, setActivePersonTab] = useState(0);
const personTabs = ["Movies", "TV Shows", "Books", "Photos", "Episodes"];
useEffect(() => {
if (
personMovies.isSuccess &&
personShows.isSuccess &&
personBooks.isSuccess &&
personPhotos.isSuccess &&
personEpisodes.isSuccess
) {
if (personMovies.data.Items.length != 0) {
setActivePersonTab(0);
} else if (personShows.data.Items.length != 0) {
setActivePersonTab(1);
} else if (personBooks.data.Items.length != 0) {
setActivePersonTab(2);
} else if (personPhotos.data.Items.length != 0) {
setActivePersonTab(3);
} else if (personEpisodes.data.Items.length != 0) {
setActivePersonTab(4);
}
}
}, [
personMovies.isLoading,
personShows.isLoading,
personBooks.isLoading,
personPhotos.isLoading,
personEpisodes.isLoading,
]);
const [videoTracks, setVideoTracks] = useState([]);
const [audioTracks, setAudioTracks] = useState([]);
const [subtitleTracks, setSubtitleTracks] = useState([]);
const [currentVideoTrack, setCurrentVideoTrack] = useState();
const [currentAudioTrack, setCurrentAudioTrack] = useState();
const [currentSubTrack, setCurrentSubTrack] = useState();
useEffect(() => {
if (item.isSuccess && !!item.data.MediaStreams) {
let videos = [];
let audios = [];
let subs = [];
for (let track of item.data.MediaStreams) {
switch (track.Type) {
case MediaStreamType.Video:
videos.push(track);
break;
case MediaStreamType.Audio:
audios.push(track);
break;
case MediaStreamType.Subtitle:
subs.push(track);
break;
default:
break;
}
}
setVideoTracks(videos);
setAudioTracks(audios);
setSubtitleTracks(subs);
setCurrentVideoTrack(videos[0].DisplayTitle);
setCurrentAudioTrack(audios[0].DisplayTitle);
setCurrentSubTrack(subs[0].DisplayTitle);
}
}, [item.isSuccess]);
if (item.isLoading || similarItems.isLoading) {
return (
);
}
if (item.isSuccess && similarItems.isSuccess) {
return (
<>
{item.data.BackdropImageTags.length != 0 && (
)}
{!!item.data.ParentBackdropImageTags &&
item.data.ParentBackdropImageTags.length != 0 && (
)}
{item.data.Type != BaseItemKind.MusicAlbum
? item.data.BackdropImageTags.length !=
0 && (
setBackdropImageLoaded(
true,
)
}
/>
)
: !!item.data.ParentBackdropItemId && (
setBackdropImageLoaded(
true,
)
}
/>
)}
{item.data.BackdropImageTags.length != 0 && (
)}
{TypeIconCollectionCard[item.data.Type]}
}
sx={{
transition: "opacity 150ms",
opacity: item.data.UserData
.Played
? 1
: 0,
top: 5,
right: 5,
}}
/>
{!!item.data.ImageTags.Primary && (
setPrimaryImageLoaded(
true,
)
}
/>
)}
{!!item.data.ImageBlurHashes
.Primary && (
)}
{
TypeIconCollectionCard[
item.data.Type
]
}
{!!item.data.ImageTags
.Logo ? (
) : (
item.data.Name
)}
{item.data.OriginalTitle}
{!!item.data.AlbumArtists && (
,
}
>
{item.data.AlbumArtists.map(
(
artist,
aindex,
) => {
return (
{
artist.Name
}
);
},
)}
)}
}
sx={{
alignItems: "center",
width: "100%",
}}
>
{item.data.Type != "MusicArtist" &&
item.data.ProductionYear && (
{
item.data
.ProductionYear
}
)}
{item.data.Type !=
"MusicArtist" && (
)}
{item.data.Type !=
"MusicArtist" && (
{!!item.data
.CommunityRating ? (
<>
{Math.round(
item
.data
.CommunityRating *
10,
) / 10}
>
) : (
No Community
Rating
)}
)}
{!!item.data.RunTimeTicks && (
{getRuntimeFull(
item.data
.RunTimeTicks,
)}
)}
{!!item.data.RunTimeTicks && (
{endsAt(
item.data
.RunTimeTicks,
)}
)}
{item.data.Type != "Person" &&
!item.data.Type.includes(
"Music",
) && (
<>
}
sx={{
position:
"relative",
overflow:
"hidden",
}}
disabled
>
{!!item.data
.UserData
.PlayedPercentage &&
item.data.UserData
.PlayedPercentage >
0
? "Resume"
: "Play"}
{!!item.data
.UserData
.PlayedPercentage && (
)}
>
)}
{item.data.UserData.IsFavorite ? (
) : (
)}
{item.data.Taglines[0]}
{item.data.Genres != 0 && (
Genre
{item.data.Genres.map(
(
genre,
index,
) => {
return (
);
},
)}
)}
{item.data.ExternalUrls.length != 0 && (
Links
,
}
>
{item.data.ExternalUrls.map(
(
item,
index,
) => {
return (
{
item.Name
}
);
},
)}
)}
{item.data.Type ==
BaseItemKind.MusicAlbum && (
Artists
,
}
flexWrap="wrap"
>
{item.data.Artists.map(
(
mitem,
mindex,
) => {
return (
{
mitem
}
);
},
)}
)}
{!!item.data.MediaStreams &&
item.data.MediaStreams.length !=
0 && (
<>
{videoTracks != [] && (
Video
{
setCurrentVideoTrack(
e
.target
.value,
);
}}
>
{videoTracks.map(
(
stream,
sindex,
) => {
if (
stream.Type ==
BaseItemKind.Video
)
return (
);
},
)}
)}
{audioTracks != [] && (
Audio
{
setCurrentAudioTrack(
e
.target
.value,
);
}}
>
{audioTracks.map(
(
stream,
sindex,
) => {
return (
);
},
)}
)}
{subtitleTracks !=
[] && (
Subtitle
{
setCurrentSubTrack(
e
.target
.value,
);
}}
>
{subtitleTracks.map(
(
stream,
sindex,
) => {
return (
);
},
)}
)}
>
)}
{!!item.data.Overview && (
Overview
{item.data.Overview}
)}
{item.data.Type == BaseItemKind.MusicAlbum && (
#
Name
{musicTracks.isSuccess &&
musicTracks.data.Items.map(
(mitem, mindex) => {
return (
{
mitem.IndexNumber
}
handleLikingTrack(
mitem,
)
}
>
{mitem
.UserData
.IsFavorite ? (
) : (
)}
{
mitem.Name
}
{getRuntimeMusic(
mitem.RunTimeTicks,
)}
);
},
)}
)}
{item.data.Type == BaseItemKind.Person &&
personShows.isSuccess &&
personMovies.isSuccess &&
personBooks.isSuccess &&
personPhotos.isSuccess &&
personEpisodes.isSuccess && (
setActivePersonTab(
newVal,
)
}
aria-label="person-tabs"
>
{personTabs.map(
(pitem, index) => {
return (
);
},
)}
{personTabs.map((pitem, index) => {
return (
{pitem == "Movies" && (
{personMovies.data.Items.map(
(
mitem,
mindex,
) => {
return (
{
navigate(
`/item/${mitem.Id}`,
);
}}
>
);
},
)}
)}
{pitem == "TV Shows" && (
{personShows.data.Items.map(
(
mitem,
mindex,
) => {
return (
{
navigate(
`/item/${mitem.Id}`,
);
}}
>
);
},
)}
)}
{pitem == "Books" && (
{personBooks.data.Items.map(
(
mitem,
mindex,
) => {
return (
{
navigate(
`/item/${mitem.Id}`,
);
}}
>
);
},
)}
)}
{pitem == "Photos" && (
{personPhotos.data.Items.map(
(
mitem,
mindex,
) => {
return (
{
navigate(
`/item/${mitem.Id}`,
);
}}
>
);
},
)}
)}
{pitem == "Episodes" && (
{personEpisodes.data.Items.map(
(
mitem,
mindex,
) => {
return (
);
},
)}
)}
);
})}
)}
{nextUpEpisode.isSuccess &&
nextUpEpisode.data.TotalRecordCount != 0 && (
Next Up
{nextUpEpisode.data.Items.map(
(mitem, mindex) => {
return (
);
},
)}
)}
{seasons.isSuccess && (
Episodes
) : (
episodes.data
.TotalRecordCount
)
}
sx={{ ml: 2 }}
/>
{
setCurrentSeason(
e.target.value,
);
}}
size="small"
>
{seasons.data.Items.map(
(season, index) => {
return (
);
},
)}
{episodes.isLoading ? (
) : (
episodes.data.Items.map(
(mitem, mindex) => {
return (
);
},
)
)}
)}
{item.data.People.length != 0 && (
{item.data.People.map((person, index) => {
return (
);
})}
)}
{similarItems.data.Items.length != 0 && (
{similarItems.data.Items.map(
(simItem, index) => {
return (
{
navigate(
`/item/${simItem.Id}`,
);
}}
/>
);
},
)}
)}
>
);
}
if (item.isError || similarItems.isError) {
return ;
}
};
export default ItemDetail;