Repository Public
1"use client";2import { useEffect, useRef, useState } from "react";3import { Check, ChevronRight, Film, Play, Plus, Star, Tv } from "lucide-react";4import Link from "next/link";5import { api, cn, countryName, currentProg, duration, flag, STATUS_LABEL, RIGHTS_LABEL, categoryLabel, type Channel, type Prog, type Status, type Title } from "@/lib/client";6import { usePlayer } from "./player";78export function StatusDot({ status, className }: { status: Status; className?: string }) {9 return <span role="img" aria-label={STATUS_LABEL[status]} title={STATUS_LABEL[status]} className={cn("inline-block h-2.5 w-2.5 shrink-0 rounded-full", status === "up" ? "bg-up" : status === "down" ? "bg-down" : "bg-faint", className)} />;10}11export function StatusBadge({ status }: { status: Status }) {12 return <span className={cn("inline-flex items-center gap-1.5 rounded-full px-2.5 py-1 text-xs font-medium", status === "up" ? "bg-up/15 text-up" : status === "down" ? "bg-down/15 text-down" : "bg-white/10 text-dim")}><StatusDot status={status} />{STATUS_LABEL[status]}</span>;13}1415/** Channel logo, loaded lazily straight from its public URL; falls back to initials when missing or broken. */16export function Logo({ channel, className }: { channel: Pick<Channel, "name" | "logo">; className?: string }) {17 const [bad, setBad] = useState(false);18 useEffect(() => setBad(false), [channel.logo]);19 const initials = channel.name.replace(/[^\p{L}\p{N} ]/gu, "").split(/\s+/).filter(Boolean).slice(0, 2).map((w) => w[0]).join("").toUpperCase() || "TV";20 return (21 <span className={cn("tile relative grid place-items-center overflow-hidden", className)}>22 {channel.logo && !bad23 ? <img src={channel.logo} alt="" loading="lazy" decoding="async" referrerPolicy="no-referrer" onError={() => setBad(true)} className="logo-img absolute inset-0 m-auto h-[68%] w-[72%] object-contain" />24 : <span className="text-[clamp(0.8rem,35cqh,1.6rem)] font-bold tracking-tight text-white/45" aria-hidden>{initials}</span>}25 </span>26 );27}2829export function useFavToggle(channel: Channel, onChange?: (fav: boolean) => void) {30 const { onFavChange } = usePlayer();31 const [fav, setFav] = useState(!!channel.fav);32 useEffect(() => setFav(!!channel.fav), [channel.fav]);33 const toggle = async () => {34 const next = !fav; setFav(next); onFavChange(channel.id, next); onChange?.(next);35 try { await api("/favorites", { body: { channelId: channel.id, on: next } }); } catch { setFav(!next); onFavChange(channel.id, !next); onChange?.(!next); }36 };37 return { fav, toggle };38}3940export function ChannelCard({ channel, queue, prog, className, onFav }: { channel: Channel; queue?: Channel[]; prog?: Prog[]; className?: string; onFav?: (fav: boolean) => void }) {41 const { play, channel: playing } = usePlayer();42 const { fav, toggle } = useFavToggle(channel, onFav);43 const { now } = currentProg(prog);44 const active = playing?.id === channel.id;45 return (46 <div className={cn("group relative", className)}>47 <button type="button" onClick={() => play(channel, queue)} data-nav className="block w-full rounded-[0.9rem] text-left transition-transform duration-200 active:scale-[0.97] focus-visible:scale-[1.03] md:hover:-translate-y-0.5" aria-label={`${channel.name} — ${STATUS_LABEL[channel.status]}`}>48 <Logo channel={channel} className={cn("aspect-video w-full rounded-[0.9rem] ring-1 ring-line [container-type:size]", active && "ring-2 ring-white", channel.status === "down" && "opacity-45")} />49 <span className="mt-2 flex items-center gap-1.5 px-0.5"><StatusDot status={channel.status} className="h-1.5 w-1.5" /><span className="truncate text-[13.5px] font-medium">{channel.name}</span></span>50 <span className="block truncate px-0.5 text-xs text-dim">{now ? now.title : <>{flag(channel.country)} {channel.categories[0] ? categoryLabel(channel.categories[0]) : countryName(channel.country)}</>}</span>51 </button>52 <button type="button" onClick={toggle} aria-label={fav ? `Retirer ${channel.name} des favoris` : `Ajouter ${channel.name} aux favoris`} aria-pressed={fav}53 className={cn("absolute right-1 top-1 grid h-9 w-9 place-items-center rounded-full bg-black/50 backdrop-blur-md transition", fav ? "opacity-100" : "opacity-100 md:opacity-0 md:focus-visible:opacity-100 md:group-hover:opacity-100")}>54 <Star strokeWidth={1.8} className={cn("h-[1.05rem] w-[1.05rem]", fav ? "fill-amber-300 text-amber-300" : "text-white/85")} />55 </button>56 </div>57 );58}5960export function Row({ title, items, href, programmes }: { title: string; items: Channel[]; href?: string | null; programmes?: Record<string, Prog[]> }) {61 if (!items.length) return null;62 return (63 <section className="fadein">64 <div className="mb-3 flex items-end justify-between px-4 lg:px-8">65 <h2 className="text-[1.0625rem] font-semibold lg:text-xl">{title}</h2>66 {href && <Link href={href} data-nav className="-mr-2 flex h-9 items-center gap-0.5 rounded-full px-2 text-[13px] font-medium text-faint hover:text-ink">Tout voir<ChevronRight className="h-4 w-4" /></Link>}67 </div>68 <div className="no-scrollbar flex snap-x scroll-px-4 gap-3 overflow-x-auto scroll-smooth px-4 lg:scroll-px-8 pb-3 pt-1 lg:gap-4 lg:px-8" data-nav-row>69 {items.map((c) => <ChannelCard key={c.id} channel={c} queue={items} prog={programmes?.[c.id]} className="w-40 shrink-0 snap-start sm:w-48 lg:w-56" />)}70 </div>71 </section>72 );73}7475/** Poster image straight from its public origin (Wikimedia Commons, Internet Archive, TMDB) or from the authenticated thumbnail route; a typographic tile otherwise. */76export function Poster({ title, className, wide }: { title: Pick<Title, "title" | "poster" | "backdrop" | "kind" | "year">; className?: string; wide?: boolean }) {77 const src = wide ? title.backdrop ?? title.poster : title.poster;78 const [bad, setBad] = useState(false);79 useEffect(() => setBad(false), [src]);80 return (81 <span className={cn("poster relative block overflow-hidden", className)}>82 {src && !bad83 ? <img src={src} alt="" loading="lazy" decoding="async" referrerPolicy="no-referrer" onError={() => setBad(true)} className="absolute inset-0 h-full w-full object-cover" />84 : <span className="absolute inset-0 flex flex-col items-center justify-center gap-2 p-3 text-center">{title.kind === "series" ? <Tv className="h-7 w-7 text-white/25" /> : <Film className="h-7 w-7 text-white/25" />}<span className="line-clamp-3 text-sm font-semibold leading-tight text-white/70">{title.title}</span>{title.year && <span className="text-xs text-white/35">{title.year}</span>}</span>}85 </span>86 );87}8889export function useTitleFav(t: Pick<Title, "id" | "fav">, onChange?: (fav: boolean) => void) {90 const [fav, setFav] = useState(!!t.fav);91 useEffect(() => setFav(!!t.fav), [t.fav, t.id]);92 const toggle = async () => { const next = !fav; setFav(next); onChange?.(next); try { await api("/title-favs", { body: { titleId: t.id, on: next } }); } catch { setFav(!next); onChange?.(!next); } };93 return { fav, toggle };94}9596export function TitleCard({ title, className, wide }: { title: Title; className?: string; wide?: boolean }) {97 const p = title.progress; const pct = p && p.duration ? Math.min(100, (p.position / p.duration) * 100) : 0;98 const meta = [title.year, title.kind === "series" ? `${title.episodes} épisode${title.episodes > 1 ? "s" : ""}` : duration(title.runtime)].filter(Boolean).join(" · ");99 return (100 <Link href={`/titre/${title.id}`} data-nav className={cn("card-press group block rounded-2xl", className)} aria-label={`${title.title}${title.year ? ` (${title.year})` : ""}`}>101 <span className="relative block">102 <Poster title={title} wide={wide} className={cn("w-full rounded-[0.9rem] ring-1 ring-line", wide ? "aspect-video" : "aspect-[2/3]")} />103 {title.library && <span className="chip absolute left-2 top-2 bg-black/60 backdrop-blur">Ma bibliothèque</span>}104 {p && <span className="absolute inset-x-2 bottom-2 block"><span className="mb-1 block text-[11px] font-semibold text-white drop-shadow">{p.label ?? "Reprendre"}</span><span className="block h-1 overflow-hidden rounded-full bg-white/25"><span className="grad block h-full" style={{ width: `${pct}%` }} /></span></span>}105 </span>106 <span className="mt-2 block truncate px-0.5 text-[13.5px] font-medium">{title.title}</span>107 <span className="block truncate px-0.5 text-xs text-dim">{meta || RIGHTS_LABEL[title.rights]}</span>108 </Link>109 );110}111112export function TitleRow({ title, items, href, wide }: { title: string; items: Title[]; href?: string | null; wide?: boolean }) {113 if (!items.length) return null;114 return (115 <section className="fadein">116 <div className="mb-1 flex items-end justify-between px-4 lg:px-8">117 <h2 className="text-[1.0625rem] font-semibold lg:text-xl">{title}</h2>118 {href && <Link href={href} data-nav className="-mr-2 flex h-9 items-center gap-0.5 rounded-full px-2 text-[13px] font-medium text-faint hover:text-ink">Tout voir<ChevronRight className="h-4 w-4" /></Link>}119 </div>120 <div className="no-scrollbar flex snap-x scroll-px-4 gap-3 overflow-x-auto scroll-smooth px-4 pb-4 pt-2 lg:gap-4 lg:px-8 lg:scroll-px-8" data-nav-row>121 {items.map((t) => <TitleCard key={t.id} title={t} wide={wide} className={cn("shrink-0 snap-start", wide ? "w-60 sm:w-72" : "w-[7.5rem] sm:w-36 lg:w-44")} />)}122 </div>123 </section>124 );125}126127export function FavButton({ title, className, onChange }: { title: Pick<Title, "id" | "fav">; className?: string; onChange?: (fav: boolean) => void }) {128 const { fav, toggle } = useTitleFav(title, onChange);129 return <button type="button" onClick={toggle} aria-pressed={fav} aria-label={fav ? "Dans ma liste" : "Ma liste"} className={cn("inline-flex h-12 min-w-12 items-center justify-center gap-2 rounded-full bg-white/10 px-3.5 text-sm font-semibold backdrop-blur hover:bg-white/15 sm:px-5", className)}>{fav ? <Check className="h-5 w-5 text-up" /> : <Plus className="h-5 w-5" />}<span className="hidden sm:inline">{fav ? "Dans ma liste" : "Ma liste"}</span></button>;130}131export function PlayButton({ onClick, label = "Regarder", className, autoFocus }: { onClick: () => void; label?: string; className?: string; autoFocus?: boolean }) {132 return <button type="button" data-nav autoFocus={autoFocus} onClick={onClick} className={cn("inline-flex h-12 items-center gap-2 rounded-full bg-white px-6 text-[15px] font-semibold text-black transition hover:bg-white/90 active:scale-[0.97]", className)}><Play className="h-5 w-5 fill-current" />{label}</button>;133}134135export function Empty({ title, children }: { title: string; children?: React.ReactNode }) {136 return <div className="mx-auto max-w-md px-6 py-16 text-center"><h3 className="text-lg font-semibold">{title}</h3>{children && <div className="mt-2 text-sm leading-relaxed text-dim">{children}</div>}</div>;137}138export function PageHeader({ title, children, sub }: { title: string; sub?: React.ReactNode; children?: React.ReactNode }) {139 return <header className="flex flex-wrap items-end justify-between gap-3 px-4 pb-4 pt-5 lg:px-8 lg:pt-8"><div><h1 className="text-[1.7rem] font-bold leading-tight lg:text-[2rem]">{title}</h1>{sub && <p className="mt-1 text-sm text-dim">{sub}</p>}</div>{children}</header>;140}141142/** Calls `onVisible` when the sentinel scrolls into view (infinite lists). */143export function Sentinel({ onVisible, disabled }: { onVisible: () => void; disabled?: boolean }) {144 const ref = useRef<HTMLDivElement>(null);145 const cb = useRef(onVisible); cb.current = onVisible;146 useEffect(() => {147 if (disabled || !ref.current) return;148 const io = new IntersectionObserver((e) => { if (e[0].isIntersecting) cb.current(); }, { rootMargin: "600px" });149 io.observe(ref.current); return () => io.disconnect();150 }, [disabled]);151 return <div ref={ref} className="h-px" />;152}153154export { inputCls, btnCls } from "./ui-static";155