diff --git a/react_frontend/src/api/people.ts b/react_frontend/src/api/people.ts index 982f34b..d9bbf19 100644 --- a/react_frontend/src/api/people.ts +++ b/react_frontend/src/api/people.ts @@ -35,9 +35,21 @@ export interface TmdbPersonPreview { filmography: TmdbFilmographyResponse; } +export interface PaginatedPeopleResponse { + data: People[]; + total: number; + page: number; + totalPages: number; +} + export const peopleApi = { getAll: () => api.get('/people').then((r) => r.data), + getAllPaginated: (page: number, limit: number, search?: string) => + api + .get('/people', { params: { page, limit, ...(search ? { search } : {}) } }) + .then((r) => r.data), + getById: (id: number) => api.get(`/people/${id}`).then((r) => r.data), diff --git a/react_frontend/src/components/entries/CastList.tsx b/react_frontend/src/components/entries/CastList.tsx index ca8b29d..fbff339 100644 --- a/react_frontend/src/components/entries/CastList.tsx +++ b/react_frontend/src/components/entries/CastList.tsx @@ -33,9 +33,14 @@ function CastCard({ credit }: { credit: Credit }) { ? getProfileUrl(credit.people.profile_path) : undefined; + // For local DB credits, link to /people/:id. For TMDB preview credits (no entry_id), link via tmdb_id. + const personLink = credit.entry_id + ? `/people/${credit.people_id}` + : `/people/tmdb/${credit.people?.tmdb_id ?? credit.people_id}`; + return ( diff --git a/react_frontend/src/hooks/usePeople.ts b/react_frontend/src/hooks/usePeople.ts index b1b0c9e..f88bf59 100644 --- a/react_frontend/src/hooks/usePeople.ts +++ b/react_frontend/src/hooks/usePeople.ts @@ -1,4 +1,4 @@ -import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; +import { useQuery, useMutation, useQueryClient, useInfiniteQuery } from '@tanstack/react-query'; import { toast } from 'sonner'; import { peopleApi } from '@/api/people'; @@ -73,3 +73,17 @@ export function useSearchLocalPeople(query: string) { enabled: query.length > 0, }); } + +const LOCAL_PEOPLE_PAGE_SIZE = 48; + +export function useLocalPeopleInfinite(search: string, enabled: boolean) { + return useInfiniteQuery({ + queryKey: ['people', 'paginated', search], + queryFn: ({ pageParam = 1 }) => + peopleApi.getAllPaginated(pageParam, LOCAL_PEOPLE_PAGE_SIZE, search || undefined), + getNextPageParam: (lastPage) => + lastPage.page < lastPage.totalPages ? lastPage.page + 1 : undefined, + initialPageParam: 1, + enabled, + }); +} diff --git a/react_frontend/src/pages/PeoplePage.tsx b/react_frontend/src/pages/PeoplePage.tsx index 9743eba..29618b8 100644 --- a/react_frontend/src/pages/PeoplePage.tsx +++ b/react_frontend/src/pages/PeoplePage.tsx @@ -1,7 +1,7 @@ import { useState, useCallback, useRef, useEffect, useMemo } from 'react'; import { Link } from 'react-router-dom'; import { usePopularPeople, useTmdbSearchPeople } from '@/hooks/useTmdb'; -import { useSearchLocalPeople } from '@/hooks/usePeople'; +import { useLocalPeopleInfinite } from '@/hooks/usePeople'; import { Input } from '@/components/ui/input'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Skeleton } from '@/components/ui/skeleton'; @@ -12,14 +12,12 @@ import { Search, Heart } from 'lucide-react'; import { getProfileUrl } from '@/lib/media-utils'; type PeopleFilter = 'all' | 'favorite'; -const LOCAL_PAGE_SIZE = 48; export function PeoplePage() { const [search, setSearch] = useState(''); const [debouncedSearch, setDebouncedSearch] = useState(''); const [filter, setFilter] = useState('all'); const [localOnly, setLocalOnly] = useState(false); - const [localVisibleCount, setLocalVisibleCount] = useState(LOCAL_PAGE_SIZE); // Debounce search input useEffect(() => { @@ -27,46 +25,40 @@ export function PeoplePage() { return () => clearTimeout(timer); }, [search]); - // Reset visible count when filter/search changes - useEffect(() => { - setLocalVisibleCount(LOCAL_PAGE_SIZE); - }, [debouncedSearch, filter, localOnly]); - const isSearching = debouncedSearch.length > 0; const popularQuery = usePopularPeople(); const tmdbSearchQuery = useTmdbSearchPeople(localOnly ? '' : debouncedSearch); - const { data: localSearchResults = [], isLoading: localSearchLoading } = useSearchLocalPeople( - localOnly ? debouncedSearch : '' + + // Paginated local people (handles both search and browse) + const localPeopleQuery = useLocalPeopleInfinite( + localOnly ? debouncedSearch : '', + localOnly, ); // For TMDB mode, use infinite scroll queries const activeQuery = isSearching && !localOnly ? tmdbSearchQuery : !localOnly ? popularQuery : null; const tmdbPeople = activeQuery?.data?.pages.flatMap((p) => p.results) ?? []; - // For local mode with no search, fetch all local people - const { data: allLocalPeople = [], isLoading: allLocalLoading } = useSearchLocalPeople( - localOnly && !isSearching ? '*' : '' + // Flatten paginated local results + const allLocalPeople = useMemo( + () => localPeopleQuery.data?.pages.flatMap((p) => p.data) ?? [], + [localPeopleQuery.data], ); - // Determine displayed people + // Apply client-side favorite filter const filteredLocalPeople = useMemo(() => { if (!localOnly) return []; - const people = isSearching ? localSearchResults : allLocalPeople; if (filter === 'favorite') { - return people.filter((p: any) => p.favorite); + return allLocalPeople.filter((p: any) => p.favorite); } - return people; - }, [localOnly, isSearching, localSearchResults, allLocalPeople, filter]); + return allLocalPeople; + }, [localOnly, allLocalPeople, filter]); const displayPeople = useMemo(() => { - if (localOnly) { - return filteredLocalPeople.slice(0, localVisibleCount); - } + if (localOnly) return filteredLocalPeople; return tmdbPeople; - }, [localOnly, filteredLocalPeople, localVisibleCount, tmdbPeople]); - - const hasMoreLocal = localOnly && localVisibleCount < filteredLocalPeople.length; + }, [localOnly, filteredLocalPeople, tmdbPeople]); const observerRef = useRef(null); const lastRef = useCallback( @@ -76,14 +68,16 @@ export function PeoplePage() { observerRef.current = new IntersectionObserver((entries) => { if (!entries[0].isIntersecting) return; if (localOnly) { - if (hasMoreLocal) setLocalVisibleCount((c) => c + LOCAL_PAGE_SIZE); + if (localPeopleQuery.hasNextPage && !localPeopleQuery.isFetchingNextPage) { + localPeopleQuery.fetchNextPage(); + } } else if (activeQuery && !activeQuery.isFetchingNextPage && activeQuery.hasNextPage) { activeQuery.fetchNextPage(); } }); observerRef.current.observe(node); }, - [localOnly, hasMoreLocal, activeQuery?.isFetchingNextPage, activeQuery?.hasNextPage, activeQuery?.fetchNextPage] + [localOnly, localPeopleQuery.hasNextPage, localPeopleQuery.isFetchingNextPage, localPeopleQuery.fetchNextPage, activeQuery?.isFetchingNextPage, activeQuery?.hasNextPage, activeQuery?.fetchNextPage] ); useEffect(() => { @@ -91,7 +85,7 @@ export function PeoplePage() { }, []); const isLoading = localOnly - ? (isSearching ? localSearchLoading : allLocalLoading) + ? localPeopleQuery.isLoading : activeQuery?.isLoading; return ( diff --git a/react_frontend/src/pages/PersonDetailsPage.tsx b/react_frontend/src/pages/PersonDetailsPage.tsx index cf6306e..2857286 100644 --- a/react_frontend/src/pages/PersonDetailsPage.tsx +++ b/react_frontend/src/pages/PersonDetailsPage.tsx @@ -1,6 +1,12 @@ -import { useParams, useNavigate } from 'react-router-dom'; +import { useParams, useNavigate, useLocation } from 'react-router-dom'; import { useState } from 'react'; -import { usePerson, usePersonTmdbFilmography, useUpdatePerson } from '@/hooks/usePeople'; +import { + usePerson, + usePersonTmdbFilmography, + useUpdatePerson, + useTmdbPersonPreview, + useFetchPersonByTmdbId, +} from '@/hooks/usePeople'; import { useLibrary } from '@/hooks/useEntries'; import { useLibraryStore } from '@/stores/useLibraryStore'; import { Skeleton } from '@/components/ui/skeleton'; @@ -9,13 +15,27 @@ 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, Heart } from 'lucide-react'; +import { ChevronLeft, ChevronRight, Heart, Plus, Loader2, ExternalLink } from 'lucide-react'; import type { TmdbEntry } from '@/types'; import type { TmdbFilmographyItem } from '@/api/people'; +const TMDB_IMG = 'https://image.tmdb.org/t/p'; + export function PersonDetailsPage() { - const { id } = useParams<{ id: string }>(); - const personId = parseInt(id!, 10); + const { id, tmdbId } = useParams<{ id?: string; tmdbId?: string }>(); + const location = useLocation(); + const isTmdbMode = location.pathname.startsWith('/people/tmdb/'); + + if (isTmdbMode && tmdbId) { + return ; + } + + return ; +} + +/* ─── Local DB person ─── */ + +function LocalPersonView({ personId }: { personId: number }) { const navigate = useNavigate(); const { data: person, isLoading, isError } = usePerson(personId); const { data: tmdbFilmography, isLoading: tmdbLoading } = usePersonTmdbFilmography(personId); @@ -39,22 +59,11 @@ export function PersonDetailsPage() { } if (isLoading || !person) { - return ( -
-
- -
- - - -
-
-
- ); + return ; } return ( -
+
@@ -75,12 +84,13 @@ export function PersonDetailsPage() {
-
- {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} @@ -101,6 +111,148 @@ export function PersonDetailsPage() { ); } +/* ─── TMDB person (not in library) ─── */ + +function TmdbPersonView({ tmdbId }: { tmdbId: number }) { + const navigate = useNavigate(); + const { data: preview, isLoading, isError } = useTmdbPersonPreview(tmdbId); + const fetchPerson = useFetchPersonByTmdbId(); + useLibrary(); + const libraryMap = useLibraryStore((s) => s.entries); + const libraryLoaded = useLibraryStore((s) => s.isLoaded); + + if (isError) { + return ( +

+

Person not found

+

+ Could not load person data from TMDB. +

+ +
+ ); + } + + if (isLoading || !preview) { + return ; + } + + const profileUrl = preview.profile_path + ? `${TMDB_IMG}/w300${preview.profile_path}` + : undefined; + + const handleAddToLibrary = () => { + fetchPerson.mutate(tmdbId, { + onSuccess: (person) => { + navigate(`/people/${person.id}`, { replace: true }); + }, + }); + }; + + return ( +
+
+ + + + {preview.name.split(' ').map((n: string) => n[0]).join('')} + + +
+
+

{preview.name}

+ +
+ + {preview.biography && ( +

+ {preview.biography} +

+ )} +
+
+ + + + +
+ ); +} + +/* ─── Shared components ─── */ + +function PersonMeta({ + known_for_department, + birthday, + deathday, + place_of_birth, + tmdb_id, +}: { + known_for_department?: string | null; + birthday?: string | null; + deathday?: string | null; + place_of_birth?: string | null; + tmdb_id?: number | null; +}) { + return ( +
+ {known_for_department && {known_for_department}} + {birthday && (<>•Born: {formatDate(birthday)})} + {deathday && (<>•Died: {formatDate(deathday)})} + {place_of_birth && (<>•{place_of_birth})} + {tmdb_id && ( + <> + • + + + TMDB + + + )} +
+ ); +} + +function PersonSkeleton() { + return ( +
+
+ +
+ + + +
+
+
+ ); +} + +/* ─── Filmography ─── */ + interface FilmographySectionProps { libraryMap: Map; libraryLoaded: boolean; diff --git a/react_frontend/src/router.tsx b/react_frontend/src/router.tsx index 20c1ee9..61e3eeb 100644 --- a/react_frontend/src/router.tsx +++ b/react_frontend/src/router.tsx @@ -71,11 +71,6 @@ const PersonDetailsPage = lazy(() => default: m.PersonDetailsPage, })) ); -const PersonTmdbRedirectPage = lazy(() => - import('@/pages/PersonTmdbRedirectPage').then((m) => ({ - default: m.PersonTmdbRedirectPage, - })) -); const SettingsPage = lazy(() => import('@/pages/SettingsPage').then((m) => ({ default: m.SettingsPage })) ); @@ -218,7 +213,7 @@ export const router = createBrowserRouter([ path: '/people/tmdb/:tmdbId', element: ( }> - + ), },