import React, { useState, useEffect, useRef, useMemo } from "react"; import Box from "@mui/material/Box"; import Chip from "@mui/material/Chip"; import CircularProgress from "@mui/material/CircularProgress"; import Divider from "@mui/material/Divider"; import MenuItem from "@mui/material/MenuItem"; import Stack from "@mui/material/Stack"; import TextField from "@mui/material/TextField"; import Typography from "@mui/material/Typography"; import { green, red, yellow } from "@mui/material/colors"; import useParallax from "@/utils/hooks/useParallax"; import { AnimatePresence, motion, useScroll } from "motion/react"; import { Blurhash } from "react-blurhash"; import { BaseItemKind, ItemFields } from "@jellyfin/sdk/lib/generated-client"; import { getLibraryApi } from "@jellyfin/sdk/lib/utils/api/library-api"; import { getTvShowsApi } from "@jellyfin/sdk/lib/utils/api/tv-shows-api"; import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api"; import { useQuery } from "@tanstack/react-query"; import { endsAt, getRuntime, getRuntimeCompact, getRuntimeMusic, } from "@/utils/date/time"; import { Card } from "@/components/card/card"; import CardScroller from "@/components/cardScroller/cardScroller"; import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice"; import "./series.scss"; import heroBg from "@/assets/herobg.png"; import LikeButton from "@/components/buttons/likeButton"; import MarkPlayedButton from "@/components/buttons/markPlayedButton"; import PlayButton from "@/components/buttons/playButton"; import TrailerButton from "@/components/buttons/trailerButton"; import ShowMoreText from "@/components/showMoreText"; import { SeasonSelectorSkeleton } from "@/components/skeleton/seasonSelector"; import { getTypeIcon } from "@/components/utils/iconsCollection"; import { useBackdropStore } from "@/utils/store/backdrop"; import IconLink from "@/components/iconLink"; import EpisodeSkeleton from "@/components/skeleton/episode"; import getImageUrlsApi from "@/utils/methods/getImageUrlsApi"; import { useCentralStore } from "@/utils/store/central"; import { createFileRoute, useNavigate } from "@tanstack/react-router"; import { Link } from "@tanstack/react-router"; const getEpisodeDateString = (date: Date) => { const formatter = new Intl.DateTimeFormat(navigator.language ?? "en-US", { dateStyle: "full", }); return formatter.format(date).toString(); }; export const Route = createFileRoute("/_api/series/$id")({ component: SeriesTitlePage, }); function SeriesTitlePage() { const { id } = Route.useParams(); const api = Route.useRouteContext().api; const setBackdrop = useBackdropStore((s) => s.setBackdrop); const user = useCentralStore((s) => s.currentUser); const item = useQuery({ queryKey: ["item", id], queryFn: async () => { if (!api) return null; const result = await getUserLibraryApi(api).getItem({ userId: user?.Id, itemId: id, }); return result.data; }, enabled: !!user?.Id, networkMode: "always", refetchOnWindowFocus: true, }); const similarItems = useQuery({ queryKey: ["item", id, "similarItem"], queryFn: async () => { if (!api) return null; const result = await getLibraryApi(api).getSimilarShows({ userId: user?.Id, itemId: item.data?.Id ?? "", limit: 16, }); return result.data; }, enabled: item.isSuccess, }); const seasons = useQuery({ queryKey: ["item", id, "seasons"], queryFn: async () => { if (!api) return null; if (!item.data?.Id) return null; const result = await getTvShowsApi(api).getSeasons({ userId: user?.Id, seriesId: item.data.Id, isSpecialSeason: false, isMissing: false, }); return result.data; }, enabled: item.isSuccess, }); const [backdropImage, setBackdropImage] = useState(() => { const url = sessionStorage.getItem(`backdrop-${item.data?.Id}`); const key = sessionStorage.getItem(`backdrop-${item.data?.Id}-key`); return { url, key }; }); const [backdropImageLoaded, setBackdropImageLoaded] = useState(false); const [currentSeason, setCurrentSeason] = useState(() => { const result = sessionStorage.getItem(`season-${item.data?.Id}`); return result ?? 0; }); useEffect(() => { setCurrentSeason(sessionStorage.getItem(`season-${item.data?.Id}`) ?? 0); }, [item.data?.Id]); const currentSeasonItem = useQuery({ queryKey: ["item", id, "season", currentSeason], queryFn: async () => { if (!api) return null; const result = await getUserLibraryApi(api).getItem({ userId: user?.Id, itemId: seasons.data?.Items?.[Number(currentSeason)].Id ?? "", }); return result.data; }, enabled: seasons.isSuccess, networkMode: "always", refetchOnWindowFocus: true, }); const episodes = useQuery({ queryKey: ["item", id, "season", currentSeason, "episodes"], queryFn: async () => { if (!api) return null; const result = await getTvShowsApi(api).getEpisodes({ userId: user?.Id, seriesId: item.data?.Id ?? "", seasonId: seasons.data?.Items?.[Number(currentSeason)].Id, fields: [ItemFields.Overview], isMissing: false, }); return result.data; }, enabled: seasons.isSuccess, networkMode: "always", refetchOnWindowFocus: true, }); const specialFeatures = useQuery({ queryKey: ["item", id, "specialFeatures"], queryFn: async () => { if (!api) return null; const result = await getUserLibraryApi(api).getSpecialFeatures({ itemId: item.data?.Id ?? "", userId: user?.Id, }); return result.data; }, enabled: item.isSuccess, }); const directors = useMemo(() => { return item.data?.People?.filter((itm) => itm.Type === "Director") ?? []; }, [item.data?.Id]); const writers = useMemo(() => { return item.data?.People?.filter((itm) => itm.Type === "Writer") ?? []; }, [item.data?.Id]); const actors = useMemo(() => { return item.data?.People?.filter((itm) => itm.Type === "Actor") ?? []; }, [item.data?.Id]); const producers = useMemo(() => { return item.data?.People?.filter((itm) => itm.Type === "Producer") ?? []; }, [item.data?.Id]); useEffect(() => { if (api && currentSeasonItem.isSuccess && item.isSuccess) { if ( currentSeasonItem.isSuccess && (currentSeasonItem.data?.BackdropImageTags?.length ?? 0) > 0 ) { sessionStorage.setItem( `backdrop-${item.data?.Id}`, getImageUrlsApi(api).getItemImageUrlById( currentSeasonItem.data?.Id ?? "", "Backdrop", { tag: currentSeasonItem.data?.BackdropImageTags?.[0], }, ), ); sessionStorage.setItem( `backdrop-${item.data?.Id}-key`, currentSeasonItem.data?.BackdropImageTags?.[0] ?? "", ); setBackdropImage({ url: getImageUrlsApi(api).getItemImageUrlById( currentSeasonItem.data?.Id ?? "", "Backdrop", { tag: currentSeasonItem.data?.BackdropImageTags?.[0], }, ), key: currentSeasonItem.data?.BackdropImageTags?.[0], }); setBackdrop( getImageUrlsApi(api).getItemImageUrlById( currentSeasonItem.data?.Id ?? "", "Backdrop", { tag: currentSeasonItem.data?.BackdropImageTags?.[0], }, ), currentSeasonItem.data?.BackdropImageTags?.[0], ); setBackdropImageLoaded(false); } else if ( item.isSuccess && (item.data?.BackdropImageTags?.length ?? 0) > 0 ) { sessionStorage.setItem( `backdrop-${item.data?.Id}`, getImageUrlsApi(api).getItemImageUrlById( item.data?.Id ?? "", "Backdrop", { tag: item.data?.BackdropImageTags?.[0], }, ), ); sessionStorage.setItem( `backdrop-${item.data?.Id}-key`, item.data?.BackdropImageTags?.[0] ?? "", ); if (backdropImage.key !== item.data?.BackdropImageTags?.[0]) { setBackdropImageLoaded(false); // Reset Backdrop image load status if previous image is diff then new image } setBackdropImage({ url: getImageUrlsApi(api).getItemImageUrlById( item.data?.Id ?? "", "Backdrop", { tag: item.data?.BackdropImageTags?.[0], }, ), key: item.data?.BackdropImageTags?.[0], }); setBackdrop( getImageUrlsApi(api).getItemImageUrlById( item.data?.Id ?? "", "Backdrop", { tag: item.data?.BackdropImageTags?.[0], }, ), item.data?.BackdropImageTags?.[0], ); } } }, [currentSeasonItem.dataUpdatedAt]); const pageRef = useRef(null); const { scrollYProgress } = useScroll({ target: pageRef, offset: ["start start", "60vh start"], }); const parallax = useParallax(scrollYProgress, 50); const navigate = useNavigate(); if (item.isPending || similarItems.isPending) { return ( ); } if (item.isSuccess && item.data) { return (
{item.data.BackdropImageTags ? ( { console.info("Image Loaded"); setBackdropImageLoaded(true); }} animate={{ opacity: backdropImageLoaded ? 1 : 0, }} exit={{ opacity: 0, }} transition={{ duration: 0.2, }} style={{ y: parallax, }} /> ) : ( { e.currentTarget.style.opacity = "1"; }} style={{ y: parallax, }} /> )}
{item.data.ImageTags?.Primary ? (
{item.data.Name { e.currentTarget.style.opacity = "1"; }} className="item-hero-image" />
) : (
{getTypeIcon(item.data.Type ?? "Series")}
)}
{item.data.ImageTags?.Logo ? ( {item.data.Name { e.currentTarget.style.opacity = "1"; }} className="item-hero-logo" /> ) : ( {item.data.Name} )} {item.data.ProductionYear ?? ""} {item.data.Status === "Continuing" ? " - Present" : item.data.EndDate && ` - ${new Date(item.data.EndDate).getFullYear()}`} {seasons.data?.TotalRecordCount && ( {seasons.data.TotalRecordCount > 1 ? `${seasons.data.TotalRecordCount} Seasons` : `${seasons.data.TotalRecordCount} Season`} )} {item.data.OfficialRating && ( )} {item.data.CommunityRating && (
star
{Math.round(item.data.CommunityRating * 10) / 10}
)} {item.data.CriticRating && (
50 ? green[400] : red[400], }} > {item.data.CriticRating > 50 ? "thumb_up" : "thumb_down"}
{item.data.CriticRating}
)} {Boolean(item.data.RunTimeTicks) && ( {getRuntime(item.data.RunTimeTicks ?? 0)} )} {Boolean(item.data.RunTimeTicks) && ( {endsAt( (item.data.RunTimeTicks ?? 0) - (item.data.UserData?.PlaybackPositionTicks ?? 0), )} )} {item.data.Genres?.slice(0, 5).join(" / ")}
{item.data.RemoteTrailers && ( )}
{(item.data.Taglines?.length ?? 0) > 0 && ( {item.data.Taglines?.[0]} )}
{item.data.ExternalUrls?.map( (url) => url.Url && url.Name && ( ), )}
{actors?.length > 0 && (
Actors
{actors.map((actor) => ( {actor.PrimaryImageTag ? ( {actor.Name ) : (
{getTypeIcon("Person")}
)}
{actor.Name} {actor.Role}
))}
)} {writers.length > 0 && (
Writers
{writers.map((actor) => ( {actor.PrimaryImageTag ? ( {actor.Name ) : (
{getTypeIcon("Person")}
)}
{actor.Name} {actor.Role}
))}
)} {directors.length > 0 && (
Directors
{directors.map((actor) => ( {actor.PrimaryImageTag ? ( {actor.Name ) : (
{getTypeIcon("Person")}
)}
{actor.Name} {actor.Role}
))}
)} {producers.length > 0 && (
Producers
{producers.map((actor) => ( {actor.PrimaryImageTag ? ( {actor.Name ) : (
{getTypeIcon("Person")}
)}
{actor.Name} {actor.Role}
))}
)}
{seasons.isPending ? ( ) : (
{seasons.data?.Items?.[Number(currentSeason)]?.Name} ) : ( episodes.data?.TotalRecordCount ) } />
{currentSeasonItem.isSuccess && ( )} {currentSeasonItem.isSuccess && ( )} { setCurrentSeason(e.target.value); sessionStorage.setItem( `season-${item.data?.Id}`, e.target.value, ); }} select SelectProps={{ MenuProps: { disableScrollLock: true, }, }} size="small" > {seasons.data?.Items?.map((season, index) => { return ( {season.Name} ); })}
{episodes.isPending ? ( ) : ( episodes.data?.Items?.map((episode) => { return ( navigate({ to: "/episode/$id", params: { id: episode.Id ?? "" }, }) } initial={{ transform: "translateY(10px)", opacity: 0, }} whileInView={{ opacity: 1, transform: "translateY(0px)", }} viewport={{ once: true, }} transition={{ duration: 0.2, ease: "backInOut", }} style={{ width: "100%", }} className="item-detail-episode" > {episode.IndexNumberEnd ? `${episode.IndexNumber} / ${episode.IndexNumberEnd}` : (episode.IndexNumber ?? 0)}
tv_gen
{episode.Name { e.currentTarget.style.opacity = "1"; }} /> {(episode.UserData?.PlaybackPositionTicks ?? 0) > 0 && (
)}
{episode.Name}
{episode.PremiereDate && ( )} {getRuntimeCompact(episode.RunTimeTicks ?? 0)}
{episode.Overview}
); }) )}
)} {specialFeatures.isSuccess && (specialFeatures.data?.length ?? 0) > 0 && ( {specialFeatures.data?.map((special) => { return ( {}} /> ); })} )} {(similarItems.data?.TotalRecordCount ?? 0) > 0 && ( {similarItems.data?.Items?.map((similar) => { return ( ); })} )} ); } if (item.isError || similarItems.isError) { return ; } } export default SeriesTitlePage;