import { useState, useMemo, useEffect, useRef } from "react"; import { useParams, Link } from "react-router-dom"; import { useTmdbPreview } from "@/hooks/useEntries"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Skeleton } from "@/components/ui/skeleton"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import type { Credit } from "@/types"; type SortField = "name" | "gender" | "popularity"; type SortDir = "asc" | "desc"; type CastFilter = "all" | "cast" | "crew"; const PAGE_SIZE = 24; const SORT_LABELS: Record = { name: "Name", gender: "Gender", popularity: "Popularity", }; const FILTER_LABELS: Record = { all: "All", cast: "Cast", crew: "Crew", }; function sortCredits( credits: Credit[], field: SortField, dir: SortDir, ): Credit[] { return [...credits].sort((a, b) => { let cmp = 0; switch (field) { case "name": cmp = (a.people?.name ?? "").localeCompare(b.people?.name ?? ""); break; case "gender": cmp = (a.people?.gender ?? 0) - (b.people?.gender ?? 0); break; case "popularity": cmp = (b.people?.popularity ?? 0) - (a.people?.popularity ?? 0); break; } return dir === "asc" ? cmp : -cmp; }); } export function TmdbCastsPage() { const { type, tmdbId } = useParams<{ type: string; tmdbId: string }>(); const tmdbIdNum = parseInt(tmdbId!, 10); const mediaType = type as "movie" | "tv"; const { data: preview, isLoading } = useTmdbPreview(tmdbIdNum, mediaType); const allCredits = (preview?.credits ?? []) as unknown as Credit[]; const [sortField, setSortField] = useState("popularity"); const [sortDir, setSortDir] = useState("asc"); const [filter, setFilter] = useState("cast"); const [search, setSearch] = useState(""); const [visibleCount, setVisibleCount] = useState(PAGE_SIZE); const filtered = useMemo(() => { let result = allCredits; if (filter === "cast") result = result.filter((c) => c.cast_type === "cast"); else if (filter === "crew") result = result.filter((c) => c.cast_type === "crew"); if (search.trim()) { const q = search.trim().toLowerCase(); result = result.filter((c) => { const name = c.people?.name?.toLowerCase() ?? ""; const role = (c.cast_type === "cast" ? c.character : c.job || c.department )?.toLowerCase() ?? ""; return name.includes(q) || role.includes(q); }); } return result; }, [allCredits, filter, search]); const sorted = useMemo( () => sortCredits(filtered, sortField, sortDir), [filtered, sortField, sortDir], ); useEffect(() => { setVisibleCount(PAGE_SIZE); }, [filter, sortField, sortDir, search]); const visible = useMemo( () => sorted.slice(0, visibleCount), [sorted, visibleCount], ); const hasMore = visibleCount < sorted.length; const sentinelRef = useRef(null); useEffect(() => { if (!hasMore) return; const node = sentinelRef.current; if (!node) return; const observer = new IntersectionObserver( (entries) => { if (entries.some((e) => e.isIntersecting)) { setVisibleCount((c) => Math.min(c + PAGE_SIZE, sorted.length)); } }, { rootMargin: "400px 0px" }, ); observer.observe(node); return () => observer.disconnect(); }, [hasMore, sorted.length]); if (isLoading || !preview) { return (
{Array.from({ length: 18 }).map((_, i) => (
))}
); } const isTv = preview.is_tvseries; return (
{/* Breadcrumb */}
{preview.title} / Cast & Crew
{/* Controls */}
setSearch(e.target.value)} className="w-48" /> {sorted.length} {sorted.length === 1 ? "credit" : "credits"}
{/* Grid */} {sorted.length === 0 ? (

No credits found.

) : ( <>
{visible.map((credit) => ( ))}
{hasMore && (
{Array.from({ length: Math.min(PAGE_SIZE, sorted.length - visibleCount), }).map((_, i) => (
))}
)} )}
); } function TmdbCreditCard({ credit, isTv }: { credit: Credit; isTv: boolean }) { const profileUrl = credit.people?.profile_path ? `https://image.tmdb.org/t/p/w185${credit.people.profile_path}` : undefined; const personLink = credit.entry_id ? `/people/${credit.people_id}` : `/people/tmdb/${credit.people?.tmdb_id ?? credit.people_id}`; const roleLabel = credit.cast_type === "cast" ? credit.character : credit.job || credit.department; return ( {credit.people?.name ?.split(" ") .map((n) => n[0]) .join("")}

{credit.people?.name}

{roleLabel && (

{roleLabel}

)} {isTv && credit.episode_count != null && credit.episode_count > 0 && (

{credit.episode_count}{" "} {credit.episode_count === 1 ? "episode" : "episodes"}

)}
); }