From 8aca121b107c6b6e46510d4089be9e0fe081d32d Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Mon, 27 Apr 2026 12:33:02 +0100 Subject: [PATCH] fix: people details page not working for people that are not in db --- backend/controllers/peopleController.js | 24 ++ backend/routes/people.routes.js | 1 + react_frontend/src/api/people.ts | 17 ++ react_frontend/src/hooks/usePeople.ts | 12 +- .../src/pages/PersonTmdbRedirectPage.tsx | 283 ++++++++++++++++-- 5 files changed, 317 insertions(+), 20 deletions(-) diff --git a/backend/controllers/peopleController.js b/backend/controllers/peopleController.js index ffde287..9697b5d 100644 --- a/backend/controllers/peopleController.js +++ b/backend/controllers/peopleController.js @@ -105,6 +105,30 @@ class PeopleController { if (!data) return res.status(500).json({ message: 'Failed to search TMDB people' }); res.status(200).json(data); } + + async getTmdbPersonPreview(req, res) { + const { tmdbId } = req.params; + const personData = await tmdbService.fetchPersonDetails(tmdbId); + if (!personData) return res.status(404).json({ message: 'Person not found on TMDB' }); + + const filmography = await tmdbService.fetchPersonCombinedCredits(tmdbId); + + const preview = { + tmdb_id: personData.id, + name: personData.name, + biography: personData.biography || null, + birthday: personData.birthday || null, + deathday: personData.deathday || null, + place_of_birth: personData.place_of_birth || null, + profile_path: personData.profile_path || null, + known_for_department: personData.known_for_department || null, + popularity: personData.popularity || null, + imdb_id: personData.imdb_id || personData.external_ids?.imdb_id || null, + filmography: filmography || { cast: [], crew: [] }, + }; + + res.status(200).json(preview); + } } module.exports = new PeopleController(); diff --git a/backend/routes/people.routes.js b/backend/routes/people.routes.js index b942272..ecfdeb9 100644 --- a/backend/routes/people.routes.js +++ b/backend/routes/people.routes.js @@ -9,6 +9,7 @@ const peopleController = require('../controllers/peopleController'); // ! TMDB PROXY ROUTES (must be before /:id to avoid matching) peopleRouter.get('/tmdb/popular', peopleController.getTmdbPopularPeople); peopleRouter.get('/tmdb/search', peopleController.searchTmdbPeople); +peopleRouter.get('/tmdb/preview/:tmdbId', peopleController.getTmdbPersonPreview); // Get all people peopleRouter.get('/', peopleController.getAllPeople); diff --git a/react_frontend/src/api/people.ts b/react_frontend/src/api/people.ts index b23f84a..982f34b 100644 --- a/react_frontend/src/api/people.ts +++ b/react_frontend/src/api/people.ts @@ -21,6 +21,20 @@ export interface TmdbFilmographyResponse { crew: TmdbFilmographyItem[]; } +export interface TmdbPersonPreview { + tmdb_id: number; + name: string; + biography: string | null; + birthday: string | null; + deathday: string | null; + place_of_birth: string | null; + profile_path: string | null; + known_for_department: string | null; + popularity: number | null; + imdb_id: string | null; + filmography: TmdbFilmographyResponse; +} + export const peopleApi = { getAll: () => api.get('/people').then((r) => r.data), @@ -33,6 +47,9 @@ export const peopleApi = { getTmdbFilmography: (id: number) => api.get(`/people/${id}/tmdb-filmography`).then((r) => r.data), + previewTmdb: (tmdbId: number) => + api.get(`/people/tmdb/preview/${tmdbId}`).then((r) => r.data), + fetchByTmdbId: (tmdb_id: number, pinned = false) => api.post('/people/fetch', { tmdb_id, pinned }).then((r) => r.data), diff --git a/react_frontend/src/hooks/usePeople.ts b/react_frontend/src/hooks/usePeople.ts index b74c499..b1b0c9e 100644 --- a/react_frontend/src/hooks/usePeople.ts +++ b/react_frontend/src/hooks/usePeople.ts @@ -7,6 +7,7 @@ export const peopleKeys = { detail: (id: number) => ['people', id] as const, credits: (id: number) => ['people', id, 'credits'] as const, tmdbFilmography: (id: number) => ['people', id, 'tmdb-filmography'] as const, + tmdbPreview: (tmdbId: number) => ['people', 'tmdb-preview', tmdbId] as const, }; export function usePerson(id: number) { @@ -36,7 +37,16 @@ export function usePersonTmdbFilmography(id: number) { export function useFetchPersonByTmdbId() { return useMutation({ - mutationFn: (tmdb_id: number) => peopleApi.fetchByTmdbId(tmdb_id), + mutationFn: (tmdb_id: number) => peopleApi.fetchByTmdbId(tmdb_id, true), + }); +} + +export function useTmdbPersonPreview(tmdbId: number) { + return useQuery({ + queryKey: peopleKeys.tmdbPreview(tmdbId), + queryFn: () => peopleApi.previewTmdb(tmdbId), + enabled: !!tmdbId, + staleTime: 10 * 60 * 1000, }); } diff --git a/react_frontend/src/pages/PersonTmdbRedirectPage.tsx b/react_frontend/src/pages/PersonTmdbRedirectPage.tsx index abc8237..c183eac 100644 --- a/react_frontend/src/pages/PersonTmdbRedirectPage.tsx +++ b/react_frontend/src/pages/PersonTmdbRedirectPage.tsx @@ -1,44 +1,289 @@ -import { useEffect } from 'react'; +import { useState } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; -import { useFetchPersonByTmdbId } from '@/hooks/usePeople'; +import { useTmdbPersonPreview, useFetchPersonByTmdbId } 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 { Plus, Loader2, ChevronLeft, ChevronRight } 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 PersonTmdbRedirectPage() { const { tmdbId } = useParams<{ tmdbId: string }>(); const navigate = useNavigate(); + const tmdbIdNum = parseInt(tmdbId!, 10); + + const { data: preview, isLoading, isError } = useTmdbPersonPreview(tmdbIdNum); const fetchPerson = useFetchPersonByTmdbId(); + useLibrary(); + const libraryMap = useLibraryStore((s) => s.entries); + const libraryLoaded = useLibraryStore((s) => s.isLoaded); - useEffect(() => { - if (!tmdbId) return; - fetchPerson.mutate(parseInt(tmdbId, 10), { - onSuccess: (person) => { - navigate(`/people/${person.id}`, { replace: true }); - }, - }); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [tmdbId]); + if (isLoading || !preview) { + return ( +
+
+ +
+ + + +
+
+
+ ); + } - if (fetchPerson.isError) { + if (isError) { return (

Person not found

Could not load person data from TMDB.

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

{preview.name}

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

+ {preview.biography} +

+ )}
+ + + + +
+ ); +} + +interface TmdbFilmographySectionProps { + filmography: { cast: TmdbFilmographyItem[]; crew: TmdbFilmographyItem[] }; + libraryMap: Map; + libraryLoaded: boolean; +} + +function TmdbFilmographySection({ filmography, libraryMap, libraryLoaded }: TmdbFilmographySectionProps) { + const [libraryPage, setLibraryPage] = useState(0); + const [otherPage, setOtherPage] = useState(0); + const PAGE_SIZE = 6; + const GRID = 'grid grid-cols-2 gap-3 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6'; + + if (!libraryLoaded) { + return ( +
+

Filmography

+
+ {Array.from({ length: 6 }).map((_, i) => ( + + ))} +
+
+ ); + } + + const allCredits = [ + ...(filmography.cast ?? []), + ...(filmography.crew?.filter((c) => c.job === 'Director' || c.department === 'Directing') ?? []), + ]; + const seenIds = new Set(); + const deduped = allCredits.filter((c) => { + if (seenIds.has(c.id)) return false; + seenIds.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); + } + } + + const sorted = [...deduped].sort((a, b) => (b.popularity ?? 0) - (a.popularity ?? 0)); + const libraryCredits = sorted.filter((c) => tmdbIdToLibraryEntry.has(c.id)); + + const isSelfRole = (character?: string) => { + if (!character) return false; + const lower = character.toLowerCase().trim(); + return lower === 'self' || lower.startsWith('self -') || lower.startsWith('self (') || lower === 'themselves' || lower === 'himself' || lower === 'herself'; + }; + const otherCredits = sorted + .filter((c) => !tmdbIdToLibraryEntry.has(c.id)) + .sort((a, b) => { + const aSelf = isSelfRole(a.character) ? 1 : 0; + const bSelf = isSelfRole(b.character) ? 1 : 0; + if (aSelf !== bSelf) return aSelf - bSelf; + const dateA = new Date(a.release_date || a.first_air_date || '1900').getTime(); + const dateB = new Date(b.release_date || b.first_air_date || '1900').getTime(); + return dateB - dateA; + }); + + 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}

+ )} +
+ ); + })} +
+
+ )}
); }