import { Play } from "lucide-react"; import { useCallback } from "react"; import { useLocation } from "react-router"; import type { WatchTonightItem } from "@/hooks/queries/recommendations"; import ViewTransitionLink from "@/components/ViewTransitionLink"; import { Badge } from "@/components/ui/badge"; import { useWatchPlaybackController } from "@/playback/watchPlaybackContext"; import { parseWatchHref } from "@/pages/watchRouteHelpers"; const sourceLabels: Record = { continue_watching: "Continue", next_up: "Next Up", recommendation: "For You", }; interface WatchTonightCardProps { item: WatchTonightItem; onPlay: () => void; } export default function WatchTonightCard({ item, onPlay }: WatchTonightCardProps) { const location = useLocation(); const playbackController = useWatchPlaybackController(); const watchHref = `/watch/${item.content_id}`; const itemHref = `/item/${item.content_id}`; const source = item.watch_tonight_source ?? ""; const isInProgress = source === "continue_watching"; const isNextUp = source === "next_up"; const hasEpisodeMeta = item.season_number != null && item.episode_number != null; const heading = hasEpisodeMeta && item.series_title ? item.series_title : item.title; const progressPercent = isInProgress && (item.duration_seconds ?? 0) > 0 ? ((item.position_seconds ?? 0) / (item.duration_seconds ?? 1)) * 100 : 0; const episodeMeta = hasEpisodeMeta ? `S${item.season_number} E${item.episode_number}${item.title && item.series_title ? ` \u2022 ${item.title}` : ""}` : null; const subtitle = isNextUp ? "Next Episode" : isInProgress && (item.duration_seconds ?? 0) > 0 ? `${Math.max(0, Math.round(((item.duration_seconds ?? 0) - (item.position_seconds ?? 0)) / 60))} min left` : item.genres?.slice(0, 2).join(", ") || "\u00A0"; const handlePlayClick = useCallback( (event: React.MouseEvent) => { if ( event.defaultPrevented || event.button !== 0 || event.metaKey || event.altKey || event.ctrlKey || event.shiftKey ) { return; } const parsed = parseWatchHref(watchHref); if (!parsed) return; event.preventDefault(); onPlay(); playbackController.startPlayback({ contentId: parsed.contentId, fileId: parsed.fileId, libraryId: parsed.libraryId, restart: parsed.restart, returnHref: `${location.pathname}${location.search}`, }); }, [watchHref, location.pathname, location.search, playbackController, onPlay], ); const badgeLabel = sourceLabels[source]; return (
{/* Thumbnail */}
{item.backdrop_url ? ( {heading} ) : item.poster_url ? ( {heading} ) : (
No Image
)} {/* Play overlay */}
{/* Progress bar */} {isInProgress && progressPercent > 0 && (
)}
{/* Info */}
{badgeLabel && ( {badgeLabel} )}
{heading}
{episodeMeta && (
{episodeMeta}
)}
{subtitle}
); }