All files / src/components/channels ChannelCard.tsx

100% Statements 44/44
98.33% Branches 59/60
100% Functions 16/16
100% Lines 40/40

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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382                                  6x     6x           6x                         596x                                                                           6x                                 497x   497x           8x             4x                                                                                                     2x                   6x 6x       6x                     16x 16x 16x       16x                 1x                                                                       1x                 6x                 101x 101x 101x   101x 101x 101x 101x   101x           101x     16x                                                                                               3x                 3x         2x   1x             6x                   612x 16x                 596x                             6x 6x  
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
 
import { memo } from "react";
import { Play, TvMinimal, Heart, Film } from "lucide-react";
import type { Channel, EpgProgram } from "@/lib/types";
import { EpgTimelineBar } from "./EpgTimelineBar";
import { useDownloads, aggregateForSeries } from "@/hooks/useDownloads";
import { useNow } from "@/hooks/useNow";
import { DownloadButton } from "@/components/downloads/DownloadButton";
import { DownloadProgressBar } from "@/components/downloads/DownloadProgressBar";
import { enqueueMovieDownload, stopDownload, removeDownload } from "@/lib/tauri";
import { cn, isFromInnerButton } from "@/lib/utils";
import { aggregateIconState, downloadIconState, isActiveDownload } from "@/lib/download-status";
import { ChannelLogo, useImageFailed } from "./ChannelLogo";
 
/** Width (px) of the left channel-info column in RowCard — must match spacer in ChannelList header. */
export const ROW_CARD_LEFT_WIDTH = 180;
 
/** Height (px) of the phone's channel row. The list's virtualizer uses it. */
export const COMPACT_ROW_HEIGHT = 64;
 
/**
 * The heart toggle each card variant shows. It always sits beside the play
 * control, never inside it, and a clickable row leaves its click alone.
 */
const FavoriteButton = ({
	isFavorite,
	onToggle,
	className,
	iconClassName,
	idleIconClassName = "text-muted-foreground",
}: {
	isFavorite: boolean;
	onToggle: () => void;
	className: string;
	iconClassName: string;
	idleIconClassName?: string;
}) => (
	<button
		type="button"
		onClick={onToggle}
		className={cn(
			"flex items-center justify-center shrink-0 focus-visible:outline-hidden focus-visible:ring-1 focus-visible:ring-ring",
			className
		)}
		aria-label={isFavorite ? "Remove from favorites" : "Add to favorites"}
	>
		<Heart
			className={cn(
				"transition-colors",
				iconClassName,
				isFavorite ? "fill-current text-red-500" : idleIconClassName
			)}
		/>
	</button>
);
 
interface ChannelCardProps {
	channel: Channel;
	onPlay: (channel: Channel) => void;
	/** "compact" is the phone's row, which shows what's on now instead of the timeline. */
	variant?: "row" | "compact" | "poster";
	onToggleFavorite?: (channel: Channel) => void;
	/** Programs in the EPG display window for this channel. */
	epgPrograms?: EpgProgram[];
	/**
	 * The guide is still loading, so a row with no programs yet doesn't say
	 * there's no guide.
	 */
	guidePending?: boolean;
	/** Dynamic window start (Unix seconds). Falls back to now − 1h. */
	windowStart?: number;
	/** Dynamic window end (Unix seconds). Falls back to now + 2h. */
	windowEnd?: number;
}
 
const RowCardView = ({
	channel,
	onPlay,
	onToggleFavorite,
	epgPrograms,
	guidePending,
	windowStart,
	windowEnd,
}: {
	channel: Channel;
	onPlay: (ch: Channel) => void;
	onToggleFavorite?: (ch: Channel) => void;
	epgPrograms?: EpgProgram[];
	guidePending?: boolean;
	windowStart?: number;
	windowEnd?: number;
}) => {
	const now = useNow();
 
	return (
		// The channel button on the left plays it, and sits beside the heart
		// rather than around it, so each control is reached on its own. A click
		// elsewhere on the row, such as on the timeline, plays the channel too.
		<div
			onClick={(e) => {
				if (!isFromInnerButton(e)) onPlay(channel);
			}}
			className="group flex items-center w-full rounded-lg hover:bg-accent/60 transition-colors cursor-pointer"
		>
			{/* Left: channel info, fixed width (must match ROW_CARD_LEFT_WIDTH) */}
			<button
				type="button"
				onClick={() => onPlay(channel)}
				className="flex items-center gap-2 px-2 py-1.5 shrink-0 min-w-0 text-left rounded-lg focus-visible:outline-hidden focus-visible:ring-1 focus-visible:ring-ring"
				style={{ width: `${ROW_CARD_LEFT_WIDTH}px` }}
			>
				<ChannelLogo
					url={channel.logoUrl}
					className="h-6 w-6 rounded"
					fallback={<TvMinimal className="text-muted-foreground h-3 w-3" />}
				/>
 
				<div className="min-w-0 flex-1">
					<p className="text-xs leading-tight truncate">{channel.name}</p>
					{channel.groupTitle && (
						<p className="text-[10px] text-muted-foreground/70 truncate mt-0.5">
							{channel.groupTitle}
						</p>
					)}
				</div>
			</button>
 
			{/* Middle: EPG timeline — flex-1, takes the bulk of row width */}
			<div className="flex-1 min-w-0 py-1.5">
				{epgPrograms && epgPrograms.length > 0 ? (
					<EpgTimelineBar
						programmes={epgPrograms}
						now={now}
						windowStart={windowStart}
						windowEnd={windowEnd}
						height="h-9"
					/>
				) : (
					<div className="h-9 rounded-md border border-border/15 flex items-center justify-center bg-secondary/10">
						{!guidePending && (
							<span className="text-[8px] text-muted-foreground/25 select-none">
								no epg
							</span>
						)}
					</div>
				)}
			</div>
 
			{/* Right: LIVE badge + favourite */}
			<div className="flex items-center gap-1 px-2 shrink-0">
				<span className="flex items-center gap-0.5 text-[9px] font-semibold text-red-400 whitespace-nowrap">
					<span className="h-1.5 w-1.5 rounded-full bg-red-400 animate-pulse" />
					LIVE
				</span>
 
				{onToggleFavorite && (
					<FavoriteButton
						isFavorite={channel.isFavorite}
						onToggle={() => onToggleFavorite(channel)}
						className="h-6 w-6 rounded hover:bg-accent"
						iconClassName="h-3 w-3"
					/>
				)}
			</div>
		</div>
	);
};
 
const RowCard = memo(RowCardView);
RowCard.displayName = "RowCard";
 
/** The phone's channel row. A phone is too narrow for the timeline, so it
 *  shows the programme on now and how far into it the channel is. */
const CompactRowCard = ({
	channel,
	onPlay,
	onToggleFavorite,
	epgPrograms,
}: {
	channel: Channel;
	onPlay: (ch: Channel) => void;
	onToggleFavorite?: (ch: Channel) => void;
	epgPrograms?: EpgProgram[];
}) => {
	const now = useNow();
	const current = epgPrograms?.find((p) => p.startTime <= now && now < p.endTime);
	const progress = current
		? ((now - current.startTime) / (current.endTime - current.startTime)) * 100
		: 0;
 
	return (
		// The play and favorite buttons sit side by side rather than one inside
		// the other, so VoiceOver reads and activates each one on its own.
		<div
			className="flex items-center w-full px-2 rounded-lg active:bg-accent/60 transition-colors"
			style={{ height: `${COMPACT_ROW_HEIGHT}px` }}
		>
			<button
				type="button"
				onClick={() => onPlay(channel)}
				className="flex items-center gap-3 flex-1 min-w-0 h-full text-left rounded-lg focus-visible:outline-hidden focus-visible:ring-1 focus-visible:ring-ring"
			>
				<ChannelLogo
					url={channel.logoUrl}
					className="h-10 w-10 rounded-md"
					fallback={<TvMinimal className="text-muted-foreground h-4 w-4" />}
				/>
 
				<div className="min-w-0 flex-1">
					<p className="text-sm font-medium leading-tight truncate">{channel.name}</p>
					{current ? (
						<>
							<p className="text-xs text-muted-foreground truncate mt-0.5">
								{current.title}
							</p>
							<div className="h-0.5 mt-1.5 rounded-full bg-secondary overflow-hidden">
								<div
									className="h-full bg-red-400/80"
									style={{ width: `${progress.toFixed(1)}%` }}
								/>
							</div>
						</>
					) : (
						channel.groupTitle && (
							<p className="text-xs text-muted-foreground/70 truncate mt-0.5">
								{channel.groupTitle}
							</p>
						)
					)}
				</div>
			</button>
 
			{onToggleFavorite && (
				<FavoriteButton
					isFavorite={channel.isFavorite}
					onToggle={() => onToggleFavorite(channel)}
					className="h-11 w-11 -mr-2 rounded-full"
					iconClassName="h-4 w-4"
				/>
			)}
		</div>
	);
};
 
const PosterCard = ({
	channel,
	onPlay,
	onToggleFavorite,
}: {
	channel: Channel;
	onPlay: (ch: Channel) => void;
	onToggleFavorite?: (ch: Channel) => void;
}) => {
	const hasSources = channel.sources.length > 0;
	const [imgError, markImgFailed] = useImageFailed(channel.logoUrl);
	const showFallback = !channel.logoUrl || imgError;
 
	const { byChannel, bySeries } = useDownloads();
	const isSeries = channel.contentType === "series";
	const dl = byChannel.get(channel.id);
	const seriesEps = isSeries ? (bySeries.get(channel.id) ?? []) : [];
 
	const iconState = isSeries
		? // totalEpisodes is unknown at the card level (resolved in the drawer),
			// so pass 0 → aggregateForSeries never returns "complete" here.
			aggregateIconState(aggregateForSeries(seriesEps, 0))
		: downloadIconState(dl);
 
	return (
		<div className="group flex flex-col text-left relative">
			<button
				onClick={() => onPlay(channel)}
				className="focus-visible:outline-hidden focus-visible:ring-1 focus-visible:ring-ring rounded-lg w-full"
			>
				<div className="relative w-full aspect-2/1 rounded-lg overflow-hidden mb-1.5 border border-border/40">
					{!showFallback ? (
						<img
							src={channel.logoUrl}
							alt=""
							className="h-full w-full object-cover transition-transform duration-200 group-hover:scale-105"
							loading="lazy"
							onError={markImgFailed}
						/>
					) : (
						<div className="h-full w-full bg-linear-to-br from-secondary via-secondary/80 to-secondary/50 flex flex-col items-center justify-center gap-1">
							<Film className="h-5 w-5 text-muted-foreground/25" />
							<span className="text-[9px] font-medium text-muted-foreground/30 uppercase tracking-wider">
								No poster
							</span>
						</div>
					)}
					<div className="absolute inset-0 bg-black/0 group-hover:bg-black/50 transition-colors flex items-center justify-center">
						<div className="h-9 w-9 rounded-full bg-primary flex items-center justify-center opacity-0 group-hover:opacity-100 scale-90 group-hover:scale-100 transition-all duration-150">
							<Play className="h-4 w-4 text-white ml-0.5" />
						</div>
					</div>
					{hasSources && (
						<div className="absolute top-1.5 right-1.5 bg-black/70 text-white text-[9px] font-semibold px-1.5 py-0.5 rounded-full">
							{channel.sources.length + 1}
						</div>
					)}
				</div>
				<p className="text-xs leading-snug line-clamp-2 text-foreground/85 group-hover:text-foreground transition-colors px-0.5">
					{channel.name}
				</p>
			</button>
			{/* Siblings of the play button, since screen readers can't reach a
			    control or a progress bar nested inside a button on its own. The
			    extra pixel keeps each one inside the poster's border. */}
			{!isSeries && dl && isActiveDownload(dl) && (
				<div className="pointer-events-none absolute inset-x-0 top-0 aspect-2/1 flex items-end p-px rounded-lg overflow-hidden">
					<div className="w-full px-1.5 pb-1 pt-3 bg-linear-to-t from-black/90 to-transparent">
						<DownloadProgressBar download={dl} />
					</div>
				</div>
			)}
			{onToggleFavorite && (
				<FavoriteButton
					isFavorite={channel.isFavorite}
					onToggle={() => onToggleFavorite(channel)}
					className="absolute top-[5px] left-[5px] z-10 h-7 w-7 rounded-full bg-black/50 hover:bg-black/70 transition-colors cursor-pointer"
					iconClassName="h-3.5 w-3.5"
					idleIconClassName="text-white"
				/>
			)}
			<DownloadButton
				state={iconState}
				onStart={() => {
					if (!isSeries) enqueueMovieDownload(channel.id);
					// Whole-series enqueue is driven from the series detail drawer
					// (it needs the resolved episode list); no-op from the card.
				}}
				onStop={() => {
					if (!isSeries && dl) stopDownload(dl.id);
				}}
				onRemove={() => (!isSeries && dl ? removeDownload(dl.id) : undefined)}
				className="absolute top-1 left-9 z-10 h-7 w-7 justify-center rounded-full bg-black/50 hover:bg-black/70"
			/>
		</div>
	);
};
 
const ChannelCardView = ({
	channel,
	onPlay,
	variant = "row",
	onToggleFavorite,
	epgPrograms,
	guidePending,
	windowStart,
	windowEnd,
}: ChannelCardProps) => {
	if (variant === "compact") {
		return (
			<CompactRowCard
				channel={channel}
				onPlay={onPlay}
				onToggleFavorite={onToggleFavorite}
				epgPrograms={epgPrograms}
			/>
		);
	}
	return variant === "poster" ? (
		<PosterCard channel={channel} onPlay={onPlay} onToggleFavorite={onToggleFavorite} />
	) : (
		<RowCard
			channel={channel}
			onPlay={onPlay}
			onToggleFavorite={onToggleFavorite}
			epgPrograms={epgPrograms}
			guidePending={guidePending}
			windowStart={windowStart}
			windowEnd={windowEnd}
		/>
	);
};
 
export const ChannelCard = memo(ChannelCardView);
ChannelCard.displayName = "ChannelCard";