import { useParams } from 'react-router-dom'; import { useState } from 'react'; import { usePerson, usePersonTmdbFilmography } from '@/hooks/usePeople'; import { useLibrary } from '@/hooks/useEntries'; import { useLibraryStore } from '@/stores/useLibraryStore'; import { Skeleton } from '@/components/ui/skeleton'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Separator } from '@/components/ui/separator'; import { Button } from '@/components/ui/button'; import { EntryCard } from '@/components/entries/EntryCard'; import { getProfileUrl, formatDate } from '@/lib/media-utils'; import { ChevronLeft, ChevronRight } from 'lucide-react'; import type { TmdbEntry } from '@/types'; import type { TmdbFilmographyItem } from '@/api/people'; export function PersonDetailsPage() { const { id } = useParams<{ id: string }>(); const personId = parseInt(id!, 10); const { data: person, isLoading } = usePerson(personId); const { data: tmdbFilmography, isLoading: tmdbLoading } = usePersonTmdbFilmography(personId); useLibrary(); const libraryMap = useLibraryStore((s) => s.entries); const libraryLoaded = useLibraryStore((s) => s.isLoaded); if (isLoading || !person) { return (
); } return (
{person.name.split(' ').map((n: string) => n[0]).join('')}

{person.name}

{person.known_for_department && {person.known_for_department}} {person.birthday && (<>•Born: {formatDate(person.birthday)})} {person.deathday && (<>•Died: {formatDate(person.deathday)})} {person.place_of_birth && (<>•{person.place_of_birth})}
{person.biography && (

{person.biography}

)}
); } interface FilmographySectionProps { libraryMap: Map; libraryLoaded: boolean; tmdbFilmography?: { cast: TmdbFilmographyItem[]; crew: TmdbFilmographyItem[] }; tmdbLoading: boolean; } function FilmographySection({ libraryMap, libraryLoaded, tmdbFilmography, tmdbLoading }: FilmographySectionProps) { const [libraryPage, setLibraryPage] = useState(0); const [otherPage, setOtherPage] = useState(0); const PAGE_SIZE = 6; // 6 cols × 1 row const GRID = 'grid grid-cols-3 gap-3 sm:grid-cols-4 md:grid-cols-5 lg:grid-cols-6'; if (tmdbLoading || !libraryLoaded) { return (

Filmography

{Array.from({ length: 6 }).map((_, i) => ( ))}
); } const allTmdbCredits = [ ...(tmdbFilmography?.cast ?? []), ...(tmdbFilmography?.crew?.filter((c) => c.job === 'Director' || c.department === 'Directing') ?? []), ]; const seenTmdbIds = new Set(); const dedupedCredits = allTmdbCredits.filter((c) => { if (seenTmdbIds.has(c.id)) return false; seenTmdbIds.add(c.id); return true; }); const tmdbIdToLibraryEntry = new Map(); for (const entry of libraryMap.values()) { if (entry.entryIds?.tmdb_id != null) { tmdbIdToLibraryEntry.set(Number(entry.entryIds.tmdb_id), entry); } } // Sort by popularity descending const sorted = [...dedupedCredits].sort((a, b) => (b.popularity ?? 0) - (a.popularity ?? 0)); const libraryCredits = sorted.filter((c) => tmdbIdToLibraryEntry.has(c.id)); const otherCredits = sorted.filter((c) => !tmdbIdToLibraryEntry.has(c.id)); if (sorted.length === 0) { return (

Filmography

No filmography found.

); } const librarySlice = libraryCredits.slice(libraryPage * PAGE_SIZE, (libraryPage + 1) * PAGE_SIZE); const otherSlice = otherCredits.slice(otherPage * PAGE_SIZE, (otherPage + 1) * PAGE_SIZE); const libraryTotalPages = Math.ceil(libraryCredits.length / PAGE_SIZE); const otherTotalPages = Math.ceil(otherCredits.length / PAGE_SIZE); return (

Filmography

{libraryCredits.length > 0 && (

In Your Library ({libraryCredits.length})

{libraryTotalPages > 1 && (
{libraryPage + 1}/{libraryTotalPages}
)}
{librarySlice.map((credit) => { const entry = tmdbIdToLibraryEntry.get(credit.id)!; return (
{credit.character && (

{credit.character}

)}
); })}
)} {otherCredits.length > 0 && (

Not in Library ({otherCredits.length})

{otherTotalPages > 1 && (
{otherPage + 1}/{otherTotalPages}
)}
{otherSlice.map((credit) => { const tmdbEntry: TmdbEntry = { id: credit.id, title: credit.title, name: credit.name, poster_path: credit.poster_path, backdrop_path: null, release_date: credit.release_date, first_air_date: credit.first_air_date, media_type: credit.media_type, overview: '', vote_average: 0, vote_count: 0, genre_ids: [], popularity: credit.popularity ?? 0, }; return (
{credit.character && (

{credit.character}

)}
); })}
)}
); }