import { useEffect, useRef, type CSSProperties } from "react"; import { useTranslation } from "react-i18next"; import { useInfiniteQuery, useQuery } from "@tanstack/react-query"; import { ArrowLeft } from "lucide-react"; import { api, type PlexCard } from "../lib/api"; import { notify } from "../lib/notifications"; import { useDebounced } from "../lib/useDebounced"; import { useHistorySubview } from "../lib/history"; // The Plex module's content area: browse/search the mirrored library as poster cards, drill from a // show into seasons/episodes. Library scope / watch-state / sort live in the left PlexSidebar; the // search term comes from the shared top Header search box (q). A show drill-down rides history // (useHistorySubview) so browser Back returns to the grid. Playback lands in P2 — a card announces // for now. Rendered by App for page==="plex" (its own nav module). type Props = { q: string; library: string; show: string; sort: string }; const PAGE = 40; function dur(n?: number | null): string { if (!n) return ""; const h = Math.floor(n / 3600); const m = Math.floor((n % 3600) / 60); return h ? `${h}h ${m}m` : `${m}m`; } export default function PlexBrowse({ q, library, show, sort }: Props) { const { t } = useTranslation(); const dq = useDebounced(q.trim(), 350); const sub = useHistorySubview(null); // open show ratingKey (null = grid) const browseQ = useInfiniteQuery({ queryKey: ["plex-browse", library, dq, sort, show], enabled: !!library && sub.view === null, initialPageParam: 0, queryFn: ({ pageParam }) => api.plexBrowse({ library, q: dq || undefined, sort, show, offset: pageParam as number, limit: PAGE }), getNextPageParam: (last) => { const seen = last.offset + last.items.length; return seen < last.total ? seen : undefined; }, }); const items = browseQ.data?.pages.flatMap((p) => p.items) ?? []; const total = browseQ.data?.pages[0]?.total ?? 0; // Infinite scroll: auto-load the next page when the sentinel scrolls into view. const sentinel = useRef(null); const { hasNextPage, isFetchingNextPage, fetchNextPage } = browseQ; useEffect(() => { const el = sentinel.current; if (!el) return; const io = new IntersectionObserver( (entries) => { if (entries[0].isIntersecting && hasNextPage && !isFetchingNextPage) fetchNextPage(); }, { rootMargin: "600px" }, ); io.observe(el); return () => io.disconnect(); }, [hasNextPage, isFetchingNextPage, fetchNextPage]); function onPlay(card: PlexCard) { notify({ level: "info", message: t("plex.playerSoon", { title: card.title }) }); } function onCard(card: PlexCard) { if (card.type === "show") sub.open(card.id); else onPlay(card); } if (sub.view) { return ; } return (

{browseQ.isLoading ? " " : dq ? t("plex.searchCount", { count: total }) : t("plex.count", { count: total })}

{browseQ.isLoading ? (

{t("plex.loading")}

) : items.length === 0 ? (

{dq ? t("plex.noMatches") : t("plex.empty")}

) : (
{items.map((c) => ( onCard(c)} /> ))}
)}
{isFetchingNextPage &&

{t("plex.loading")}

}
); } const PLAYABLE_TINT: Record = { direct: "bg-emerald-500", remux: "bg-amber-500", transcode: "bg-orange-600", }; function PlexPosterCard({ card, onClick }: { card: PlexCard; onClick: () => void }) { const { t } = useTranslation(); const inProgress = (card.position_seconds ?? 0) > 0 && card.status !== "watched"; const pct = inProgress && card.duration_seconds ? Math.min(100, Math.round(((card.position_seconds ?? 0) / card.duration_seconds) * 100)) : 0; return ( ); } function PlexShowView({ showId, onBack, onPlay, }: { showId: string; onBack: () => void; onPlay: (c: PlexCard) => void; }) { const { t } = useTranslation(); const q = useQuery({ queryKey: ["plex-show", showId], queryFn: () => api.plexShow(showId) }); const d = q.data; return (
{q.isLoading || !d ? (

{t("plex.loading")}

) : ( <>

{d.show.title}

{d.show.year &&

{d.show.year}

} {d.show.summary && (

{d.show.summary}

)}
{d.seasons.map((se) => (

{se.title}

{se.episodes.map((ep) => { const inProgress = (ep.position_seconds ?? 0) > 0 && ep.status !== "watched"; return ( ); })}
))} )}
); }