mirror of
https://github.com/p-stream/p-stream.git
synced 2026-04-21 00:02:07 +00:00
499 lines
18 KiB
TypeScript
499 lines
18 KiB
TypeScript
import classNames from "classnames";
|
|
import { ReactNode, useCallback, useEffect, useRef, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { useAsync } from "react-use";
|
|
|
|
import { getMetaFromId } from "@/backend/metadata/getmeta";
|
|
import { MWMediaType, MWSeasonMeta } from "@/backend/metadata/types/mw";
|
|
import { Icon, Icons } from "@/components/Icon";
|
|
import { ProgressRing } from "@/components/layout/ProgressRing";
|
|
import { OverlayAnchor } from "@/components/overlays/OverlayAnchor";
|
|
import { Overlay } from "@/components/overlays/OverlayDisplay";
|
|
import { OverlayPage } from "@/components/overlays/OverlayPage";
|
|
import { OverlayRouter } from "@/components/overlays/OverlayRouter";
|
|
import { usePlayerMeta } from "@/components/player/hooks/usePlayerMeta";
|
|
import { VideoPlayerButton } from "@/components/player/internals/Button";
|
|
import { Menu } from "@/components/player/internals/ContextMenu";
|
|
import { useOverlayRouter } from "@/hooks/useOverlayRouter";
|
|
import { PlayerMeta } from "@/stores/player/slices/source";
|
|
import { usePlayerStore } from "@/stores/player/store";
|
|
import { useProgressStore } from "@/stores/progress";
|
|
|
|
import { hasAired } from "../utils/aired";
|
|
|
|
function CenteredText(props: { children: React.ReactNode }) {
|
|
return (
|
|
<div className="h-full w-full flex justify-center items-center p-8 text-center">
|
|
{props.children}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function useSeasonData(mediaId: string, seasonId: string) {
|
|
const [seasons, setSeason] = useState<MWSeasonMeta[] | null>(null);
|
|
|
|
const state = useAsync(async () => {
|
|
const data = await getMetaFromId(MWMediaType.SERIES, mediaId, seasonId);
|
|
if (data?.meta.type !== MWMediaType.SERIES) return null;
|
|
setSeason(data.meta.seasons);
|
|
return {
|
|
season: data.meta.seasonData,
|
|
fullData: data,
|
|
};
|
|
}, [mediaId, seasonId]);
|
|
|
|
return [state, seasons] as const;
|
|
}
|
|
|
|
function SeasonsView({
|
|
selectedSeason,
|
|
setSeason,
|
|
}: {
|
|
selectedSeason: string;
|
|
setSeason: (id: string) => void;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const meta = usePlayerStore((s) => s.meta);
|
|
const [loadingState, seasons] = useSeasonData(
|
|
meta?.tmdbId ?? "",
|
|
selectedSeason,
|
|
);
|
|
|
|
let content: ReactNode = null;
|
|
if (seasons) {
|
|
content = (
|
|
<Menu.Section className="pb-6">
|
|
{seasons?.map((season) => {
|
|
return (
|
|
<Menu.ChevronLink
|
|
key={season.id}
|
|
onClick={() => setSeason(season.id)}
|
|
>
|
|
{season.title}
|
|
</Menu.ChevronLink>
|
|
);
|
|
})}
|
|
</Menu.Section>
|
|
);
|
|
} else if (loadingState.error)
|
|
content = (
|
|
<CenteredText>{t("player.menus.episodes.loadingError")}</CenteredText>
|
|
);
|
|
else if (loadingState.loading)
|
|
content = (
|
|
<CenteredText>{t("player.menus.episodes.loadingList")}</CenteredText>
|
|
);
|
|
|
|
return (
|
|
<Menu.CardWithScrollable>
|
|
<Menu.Title>
|
|
{meta?.title ?? t("player.menus.episodes.loadingTitle")}
|
|
</Menu.Title>
|
|
{content}
|
|
</Menu.CardWithScrollable>
|
|
);
|
|
}
|
|
|
|
function EpisodesView({
|
|
id,
|
|
selectedSeason,
|
|
goBack,
|
|
onChange,
|
|
}: {
|
|
id: string;
|
|
selectedSeason: string;
|
|
goBack?: () => void;
|
|
onChange?: (meta: PlayerMeta) => void;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const router = useOverlayRouter(id);
|
|
const { setPlayerMeta } = usePlayerMeta();
|
|
const meta = usePlayerStore((s) => s.meta);
|
|
const [loadingState] = useSeasonData(meta?.tmdbId ?? "", selectedSeason);
|
|
const progress = useProgressStore();
|
|
const carouselRef = useRef<HTMLDivElement>(null);
|
|
const activeEpisodeRef = useRef<HTMLDivElement>(null);
|
|
|
|
const playEpisode = useCallback(
|
|
(episodeId: string) => {
|
|
if (loadingState.value) {
|
|
const newData = setPlayerMeta(loadingState.value.fullData, episodeId);
|
|
if (newData) onChange?.(newData);
|
|
}
|
|
// prevent router clear here, otherwise its done double
|
|
// player already switches route after meta change
|
|
router.close(true);
|
|
},
|
|
[setPlayerMeta, loadingState, router, onChange],
|
|
);
|
|
|
|
const handleScroll = (direction: "left" | "right") => {
|
|
if (!carouselRef.current) return;
|
|
|
|
const cardWidth = 256; // w-64 in pixels
|
|
const cardSpacing = 16; // space-x-4 in pixels
|
|
const scrollAmount = (cardWidth + cardSpacing) * 2;
|
|
|
|
const newScrollPosition =
|
|
carouselRef.current.scrollLeft +
|
|
(direction === "left" ? -scrollAmount : scrollAmount);
|
|
|
|
carouselRef.current.scrollTo({
|
|
left: newScrollPosition,
|
|
behavior: "smooth",
|
|
});
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (activeEpisodeRef.current) {
|
|
// horizontal scroll
|
|
if (window.innerWidth >= 1024 && carouselRef.current) {
|
|
const containerLeft = carouselRef.current.getBoundingClientRect().left;
|
|
const containerWidth = carouselRef.current.clientWidth;
|
|
const elementLeft =
|
|
activeEpisodeRef.current.getBoundingClientRect().left;
|
|
const elementWidth = activeEpisodeRef.current.clientWidth;
|
|
|
|
// Calculate center
|
|
const scrollPosition =
|
|
elementLeft - containerLeft - containerWidth / 2 + elementWidth / 2;
|
|
|
|
carouselRef.current.scrollLeft += scrollPosition;
|
|
} else {
|
|
// vertical scroll
|
|
activeEpisodeRef.current.scrollIntoView({
|
|
behavior: "smooth",
|
|
block: "center",
|
|
});
|
|
}
|
|
}
|
|
}, [loadingState.value]);
|
|
|
|
if (!meta?.tmdbId) return null;
|
|
|
|
let content: ReactNode = null;
|
|
if (loadingState.error)
|
|
content = (
|
|
<CenteredText>{t("player.menus.episodes.loadingError")}</CenteredText>
|
|
);
|
|
else if (loadingState.loading)
|
|
content = (
|
|
<CenteredText>{t("player.menus.episodes.loadingList")}</CenteredText>
|
|
);
|
|
else if (loadingState.value) {
|
|
content = (
|
|
<div className="relative">
|
|
{/* Horizontal scroll buttons */}
|
|
<div className="absolute left-0 top-1/2 transform -translate-y-1/2 z-10 px-4 hidden lg:block">
|
|
<button
|
|
type="button"
|
|
className="p-2 bg-black/80 hover:bg-video-context-hoverColor transition-colors rounded-full border border-video-context-border backdrop-blur-sm"
|
|
onClick={() => handleScroll("left")}
|
|
>
|
|
<Icon icon={Icons.CHEVRON_LEFT} className="text-white/80" />
|
|
</button>
|
|
</div>
|
|
|
|
<div
|
|
ref={carouselRef}
|
|
className="flex flex-col lg:flex-row lg:overflow-x-auto space-y-3 sm:space-y-4 lg:space-y-0 lg:space-x-4 pb-4 pt-2 lg:px-12 scrollbar-hide"
|
|
style={{
|
|
scrollbarWidth: "none",
|
|
msOverflowStyle: "none",
|
|
}}
|
|
>
|
|
{loadingState.value.season.episodes.length === 0 ? (
|
|
<div className="flex-shrink-0 w-full flex justify-center items-center p-4">
|
|
<p>{t("player.menus.episodes.emptyState")}</p>
|
|
</div>
|
|
) : (
|
|
loadingState.value.season.episodes.map((ep) => {
|
|
const episodeProgress =
|
|
progress.items[meta?.tmdbId]?.episodes?.[ep.id];
|
|
const percentage = episodeProgress
|
|
? (episodeProgress.progress.watched /
|
|
episodeProgress.progress.duration) *
|
|
100
|
|
: 0;
|
|
|
|
const isAired = hasAired(ep.air_date);
|
|
const isActive = ep.id === meta?.episode?.tmdbId;
|
|
|
|
return (
|
|
<div key={ep.id} ref={isActive ? activeEpisodeRef : null}>
|
|
{/* Extra small screens - Simple vertical list with no thumbnails */}
|
|
<div className="block sm:hidden w-full px-3">
|
|
<Menu.Link
|
|
onClick={() => playEpisode(ep.id)}
|
|
active={isActive}
|
|
clickable={isAired}
|
|
rightSide={
|
|
episodeProgress && (
|
|
<ProgressRing
|
|
className="h-[18px] w-[18px] text-white"
|
|
percentage={percentage > 90 ? 100 : percentage}
|
|
/>
|
|
)
|
|
}
|
|
>
|
|
<Menu.LinkTitle>
|
|
<div
|
|
className={classNames(
|
|
"text-left flex items-center space-x-1 text-video-context-type-main",
|
|
isAired || isActive ? "" : "text-opacity-25",
|
|
)}
|
|
>
|
|
<span className="p-0.5 px-2 rounded inline bg-video-context-hoverColor bg-opacity-50">
|
|
E{ep.number}
|
|
</span>
|
|
<span className="line-clamp-1 break-all">
|
|
{ep.title}
|
|
</span>
|
|
</div>
|
|
</Menu.LinkTitle>
|
|
</Menu.Link>
|
|
</div>
|
|
|
|
{/* Small screens - Vertical list with thumbnails to the left */}
|
|
<div
|
|
onClick={() => playEpisode(ep.id)}
|
|
className={classNames(
|
|
"hidden sm:flex lg:hidden w-full rounded-lg overflow-hidden transition-all duration-200 relative cursor-pointer",
|
|
isActive
|
|
? "bg-video-context-hoverColor/50"
|
|
: "hover:bg-video-context-hoverColor/50",
|
|
!isAired ? "opacity-50" : "",
|
|
)}
|
|
>
|
|
{/* Thumbnail */}
|
|
<div className="relative aspect-video max-h-[110px] w-1/3 flex-shrink-0 bg-video-context-hoverColor">
|
|
{ep.still_path ? (
|
|
<img
|
|
src={`https://image.tmdb.org/t/p/w300${ep.still_path}`}
|
|
alt={ep.title}
|
|
className="w-full h-full object-cover"
|
|
/>
|
|
) : (
|
|
<div className="w-full h-full flex items-center justify-center bg-black bg-opacity-50">
|
|
<Icon
|
|
icon={Icons.FILM}
|
|
className="text-video-context-type-main opacity-50 text-3xl"
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
{/* Episode Number Badge */}
|
|
<div className="absolute top-2 left-2 flex items-center space-x-2">
|
|
<span className="p-0.5 px-2 rounded inline bg-video-context-hoverColor bg-opacity-80 text-video-context-type-main text-sm">
|
|
E{ep.number}
|
|
</span>
|
|
{!isAired && (
|
|
<span className="text-video-context-type-main/70 text-sm">
|
|
{ep.air_date
|
|
? `(Airs - ${new Date(ep.air_date).toLocaleDateString()})`
|
|
: "(Unreleased)"}
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Content */}
|
|
<div className="p-3 flex-1">
|
|
<h3 className="font-bold text-white line-clamp-1">
|
|
{ep.title}
|
|
</h3>
|
|
{ep.overview && (
|
|
<p className="text-sm text-white/80 mt-1.5 line-clamp-2">
|
|
{ep.overview}
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
{/* Progress indicator */}
|
|
{percentage > 0 && (
|
|
<div className="absolute bottom-0 left-0 right-0 h-1 bg-progress-background/25">
|
|
<div
|
|
className="h-full bg-progress-filled"
|
|
style={{
|
|
width: `${percentage > 98 ? 100 : percentage}%`,
|
|
}}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* Large screens - Horizontal cards with thumbnails above title */}
|
|
<div
|
|
onClick={() => playEpisode(ep.id)}
|
|
className={classNames(
|
|
"hidden lg:block flex-shrink-0 w-64 rounded-lg overflow-hidden transition-all duration-200 relative cursor-pointer",
|
|
isActive
|
|
? "bg-video-context-hoverColor/50"
|
|
: "hover:bg-video-context-hoverColor/50",
|
|
!isAired ? "opacity-50" : "hover:scale-95",
|
|
)}
|
|
>
|
|
{/* Thumbnail */}
|
|
<div className="relative aspect-video w-full bg-video-context-hoverColor">
|
|
{ep.still_path ? (
|
|
<img
|
|
src={`https://image.tmdb.org/t/p/w300${ep.still_path}`}
|
|
alt={ep.title}
|
|
className="w-full h-full object-cover"
|
|
/>
|
|
) : (
|
|
<div className="w-full h-full flex items-center justify-center bg-black bg-opacity-50">
|
|
<Icon
|
|
icon={Icons.FILM}
|
|
className="text-video-context-type-main opacity-50 text-3xl"
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
{/* Episode Number Badge */}
|
|
<div className="absolute top-2 left-2 flex items-center space-x-2">
|
|
<span className="p-0.5 px-2 rounded inline bg-video-context-hoverColor bg-opacity-80 text-video-context-type-main text-sm">
|
|
E{ep.number}
|
|
</span>
|
|
{!isAired && (
|
|
<span className="text-video-context-type-main/70 text-sm">
|
|
{ep.air_date
|
|
? `(Airs - ${new Date(ep.air_date).toLocaleDateString()})`
|
|
: "(Unreleased)"}
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Content */}
|
|
<div className="p-3">
|
|
<h3 className="font-bold text-white line-clamp-1">
|
|
{ep.title}
|
|
</h3>
|
|
{ep.overview && (
|
|
<p className="text-sm text-white/80 mt-1.5 line-clamp-2">
|
|
{ep.overview}
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
{/* Progress indicator */}
|
|
{percentage > 0 && (
|
|
<div className="absolute bottom-0 left-0 right-0 h-1 bg-progress-background/25">
|
|
<div
|
|
className="h-full bg-progress-filled"
|
|
style={{
|
|
width: `${percentage > 98 ? 100 : percentage}%`,
|
|
}}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
})
|
|
)}
|
|
</div>
|
|
|
|
{/* Right scroll button */}
|
|
<div className="absolute right-0 top-1/2 transform -translate-y-1/2 z-10 px-4 hidden lg:block">
|
|
<button
|
|
type="button"
|
|
className="p-2 bg-black/80 hover:bg-video-context-hoverColor transition-colors rounded-full border border-video-context-border backdrop-blur-sm"
|
|
onClick={() => handleScroll("right")}
|
|
>
|
|
<Icon icon={Icons.CHEVRON_RIGHT} className="text-white/80" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Menu.CardWithScrollable>
|
|
<Menu.BackLink onClick={goBack} side="right">
|
|
{loadingState?.value?.season.title ||
|
|
t("player.menus.episodes.loadingTitle")}
|
|
</Menu.BackLink>
|
|
{content}
|
|
</Menu.CardWithScrollable>
|
|
);
|
|
}
|
|
|
|
function EpisodesOverlay({
|
|
id,
|
|
onChange,
|
|
}: {
|
|
id: string;
|
|
onChange?: (meta: PlayerMeta) => void;
|
|
}) {
|
|
const router = useOverlayRouter(id);
|
|
const meta = usePlayerStore((s) => s.meta);
|
|
const [selectedSeason, setSelectedSeason] = useState("");
|
|
|
|
const lastActiveState = useRef(false);
|
|
useEffect(() => {
|
|
if (lastActiveState.current === router.isRouterActive) return;
|
|
lastActiveState.current = router.isRouterActive;
|
|
setSelectedSeason(meta?.season?.tmdbId ?? "");
|
|
}, [meta, selectedSeason, setSelectedSeason, router.isRouterActive]);
|
|
|
|
const setSeason = useCallback(
|
|
(seasonId: string) => {
|
|
setSelectedSeason(seasonId);
|
|
router.navigate("/episodes");
|
|
},
|
|
[router],
|
|
);
|
|
|
|
return (
|
|
<Overlay id={id}>
|
|
<OverlayRouter id={id}>
|
|
<OverlayPage id={id} path="/" width={343} height={431}>
|
|
<SeasonsView setSeason={setSeason} selectedSeason={selectedSeason} />
|
|
</OverlayPage>
|
|
<OverlayPage id={id} path="/episodes" width={0} height={360} fullWidth>
|
|
{selectedSeason.length > 0 ? (
|
|
<EpisodesView
|
|
selectedSeason={selectedSeason}
|
|
id={id}
|
|
goBack={() => router.navigate("/")}
|
|
onChange={onChange}
|
|
/>
|
|
) : null}
|
|
</OverlayPage>
|
|
</OverlayRouter>
|
|
</Overlay>
|
|
);
|
|
}
|
|
|
|
interface EpisodesProps {
|
|
onChange?: (meta: PlayerMeta) => void;
|
|
}
|
|
|
|
export function EpisodesRouter(props: EpisodesProps) {
|
|
return <EpisodesOverlay onChange={props.onChange} id="episodes" />;
|
|
}
|
|
|
|
export function Episodes() {
|
|
const { t } = useTranslation();
|
|
const router = useOverlayRouter("episodes");
|
|
const setHasOpenOverlay = usePlayerStore((s) => s.setHasOpenOverlay);
|
|
const type = usePlayerStore((s) => s.meta?.type);
|
|
|
|
useEffect(() => {
|
|
setHasOpenOverlay(router.isRouterActive);
|
|
}, [setHasOpenOverlay, router.isRouterActive]);
|
|
if (type !== "show") return null;
|
|
|
|
return (
|
|
<OverlayAnchor id={router.id}>
|
|
<VideoPlayerButton
|
|
onClick={() => router.open("/episodes")}
|
|
icon={Icons.EPISODES}
|
|
>
|
|
{t("player.menus.episodes.button")}
|
|
</VideoPlayerButton>
|
|
</OverlayAnchor>
|
|
);
|
|
}
|