All files / src/components/channels MovieInfoDrawer.tsx

0% Statements 0/174
0% Branches 0/1
0% Functions 0/1
0% Lines 0/174

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import { useState, useEffect } from "react";
import { X, Play, Clapperboard, Loader2 } from "lucide-react";
import { Select } from "@/components/ui/select";
import type { Channel, OmdbData, WhatsonData } from "@/lib/types";
import { fetchOmdbData, fetchWhatsonData } from "@/lib/tauri";
import { RatingsRow } from "@/components/ui/ratings-row";
 
interface MovieInfoDrawerProps {
	movie: Channel;
	onClose: () => void;
	onPlay: (ch: Channel) => void;
	// Optional pre-fetched data from PlayerView to avoid double-fetch
	prefetchedOmdbData?: OmdbData | null;
	prefetchedWhatsonData?: WhatsonData | null;
}
 
export const MovieInfoDrawer = ({
	movie,
	onClose,
	onPlay,
	prefetchedOmdbData,
	prefetchedWhatsonData,
}: MovieInfoDrawerProps) => {
	const [visible, setVisible] = useState(false);
	const [selectedSourceIdx, setSelectedSourceIdx] = useState(0);
	const [omdbData, setOmdbData] = useState<OmdbData | null>(prefetchedOmdbData ?? null);
	const [omdbLoading, setOmdbLoading] = useState(prefetchedOmdbData === undefined);
	const [whatsonData, setWhatsonData] = useState<WhatsonData | null>(
		prefetchedWhatsonData ?? null
	);
 
	useEffect(() => {
		const id = requestAnimationFrame(() => setVisible(true));
		return () => cancelAnimationFrame(id);
	}, []);
 
	// Sync when prefetched Whatson data arrives after drawer opens
	useEffect(() => {
		if (prefetchedWhatsonData !== undefined) {
			setWhatsonData(prefetchedWhatsonData ?? null);
		}
	}, [prefetchedWhatsonData]);
 
	useEffect(() => {
		if (prefetchedOmdbData !== undefined) return;
		if (!movie) return;
		setOmdbLoading(true);
		setOmdbData(null);
		setWhatsonData(null);
		fetchOmdbData(movie.id, movie.name, "movie")
			.then((data) => {
				setOmdbData(data);
				// Also fetch Whatson if we got an imdb_id
				if (data?.imdbId) {
					const mediaType = movie.contentType === "series" ? "show" : "movie";
					fetchWhatsonData(data.imdbId, mediaType)
						.then(setWhatsonData)
						.catch(() => {});
				}
			})
			.catch(() => {})
			.finally(() => setOmdbLoading(false));
	}, [movie, prefetchedOmdbData]);
 
	const handleClose = () => {
		setVisible(false);
		setTimeout(onClose, 300);
	};
 
	const allSources = [...new Set([movie.url, ...movie.sources])];
	const hasSources = allSources.length > 1;
 
	const sourceOptions = allSources.map((_, idx) => ({
		value: idx,
		label: idx === 0 ? "Source 1 (default)" : `Source ${idx + 1}`,
	}));
 
	const handlePlay = () => {
		const url = allSources[selectedSourceIdx];
		onPlay(selectedSourceIdx === 0 ? movie : { ...movie, url });
		handleClose();
	};
 
	// Derived display values
	const posterSrc =
		whatsonData?.image ??
		(omdbData?.posterUrl && omdbData.posterUrl !== "N/A"
			? omdbData.posterUrl
			: (movie.logoUrl ?? null));
 
	const year = omdbData?.year && omdbData.year !== "N/A" ? omdbData.year : null;
	const rated =
		whatsonData?.certification ??
		(omdbData?.rated && omdbData.rated !== "N/A" ? omdbData.rated : null);
	const genre = omdbData?.genre && omdbData.genre !== "N/A" ? omdbData.genre : null;
	const runtime = omdbData?.runtime && omdbData.runtime !== "N/A" ? omdbData.runtime : null;
	const director = omdbData?.director && omdbData.director !== "N/A" ? omdbData.director : null;
	const actors = omdbData?.actors && omdbData.actors !== "N/A" ? omdbData.actors : null;
	const plot =
		(omdbData?.plot && omdbData.plot !== "N/A" ? omdbData.plot : null) ??
		whatsonData?.tagline ??
		null;
 
	const truncatedActors = actors && actors.length > 60 ? actors.slice(0, 57) + "..." : actors;
 
	const genreRuntime = [genre, runtime].filter(Boolean).join(" · ");
 
	return (
		<div className="fixed inset-0 z-50">
			<div
				className={`absolute inset-0 bg-black/60 backdrop-blur-sm transition-opacity duration-300 ${
					visible ? "opacity-100" : "opacity-0"
				}`}
				onClick={handleClose}
			/>
			<div
				className={`absolute bottom-0 left-0 right-0 bg-card rounded-t-2xl shadow-2xl flex flex-col transition-transform duration-300 ease-out max-h-[85vh] overflow-hidden ${
					visible ? "translate-y-0" : "translate-y-full"
				}`}
			>
				{/* Handle */}
				<div className="flex justify-center pt-3 pb-1 shrink-0">
					<div className="w-9 h-1 rounded-full bg-border" />
				</div>
 
				{/* Close button */}
				<div className="flex justify-end px-5 pt-1 shrink-0">
					<button
						onClick={handleClose}
						aria-label="Close"
						className="text-muted-foreground hover:text-foreground transition-colors"
					>
						<X className="h-4 w-4" />
					</button>
				</div>
 
				{/* Side-by-side: movie info (left ~65%) + controls (right ~35%) */}
				<div className="flex gap-4 px-5 pb-4 shrink-0">
					{/* Poster */}
					<div className="w-20 h-28 rounded-xl bg-secondary overflow-hidden shrink-0 flex items-center justify-center">
						{posterSrc ? (
							<img
								src={posterSrc}
								alt=""
								className="h-full w-full object-cover"
								loading="lazy"
							/>
						) : (
							<Clapperboard className="h-8 w-8 text-muted-foreground/30" />
						)}
					</div>
 
					{/* Movie info */}
					<div className="flex flex-col justify-center gap-1.5 flex-[2] min-w-0">
						{omdbLoading ? (
							<div className="flex items-center gap-2 text-muted-foreground">
								<Loader2 className="h-3.5 w-3.5 animate-spin" />
								<span className="text-xs">Loading info…</span>
							</div>
						) : (
							<>
								{/* Title + year + rated */}
								<div className="flex items-baseline gap-1.5 flex-wrap">
									<p className="text-base font-semibold leading-tight line-clamp-2">
										{movie.name}
									</p>
									{year && (
										<span className="text-xs text-muted-foreground shrink-0">
											({year})
										</span>
									)}
									{rated && (
										<span className="text-[11px] font-semibold bg-secondary text-muted-foreground px-1.5 py-0.5 rounded shrink-0">
											{rated}
										</span>
									)}
								</div>
 
								{/* Ratings row */}
								<RatingsRow omdbData={omdbData} whatsonData={whatsonData} />
 
								{/* Genre + runtime */}
								{genreRuntime && (
									<p className="text-xs text-muted-foreground">{genreRuntime}</p>
								)}
 
								{/* Director */}
								{director && (
									<p className="text-xs text-muted-foreground truncate">
										Dir: {director}
									</p>
								)}
 
								{/* Cast */}
								{truncatedActors && (
									<p className="text-xs text-muted-foreground">
										Cast: {truncatedActors}
									</p>
								)}
 
								{/* Fallback: no OMDB data */}
								{!omdbData && (
									<p className="text-xs text-muted-foreground">— · —</p>
								)}
							</>
						)}
					</div>
 
					{/* Controls */}
					<div className="flex flex-col justify-center gap-2.5 flex-[1] min-w-0 shrink-0">
						{hasSources && (
							<Select
								value={selectedSourceIdx}
								onChange={setSelectedSourceIdx}
								options={sourceOptions}
								aria-label="Select source"
							/>
						)}
						<button
							onClick={handlePlay}
							className="w-full flex items-center justify-center gap-2 bg-primary text-primary-foreground rounded-xl py-2.5 text-sm font-semibold hover:bg-primary/90 active:bg-primary/80 transition-colors"
						>
							<Play className="h-4 w-4 ml-0.5" />
							Play
						</button>
					</div>
				</div>
 
				{/* Plot — shown below the main row when available */}
				{plot && !omdbLoading && (
					<div className="px-5 pb-4 shrink-0">
						<div className="border-t border-border mb-3" />
						<p className="text-xs text-muted-foreground leading-relaxed">{plot}</p>
					</div>
				)}
 
				<div className="shrink-0 pb-2" />
			</div>
		</div>
	);
};