diff --git a/react_frontend/src/lib/media-utils.ts b/react_frontend/src/lib/media-utils.ts index 1d75771..9d10c9e 100644 --- a/react_frontend/src/lib/media-utils.ts +++ b/react_frontend/src/lib/media-utils.ts @@ -137,3 +137,24 @@ export function getYear(dateStr: string | null): string { if (!dateStr) return ""; return new Date(dateStr).getFullYear().toString(); } + +export function calculateAge( + birthday: string | null | undefined, + deathday?: string | null, +): number | null { + if (!birthday) return null; + + const birth = new Date(birthday); + if (isNaN(birth.getTime())) return null; + + let end = deathday ? new Date(deathday) : new Date(); + if (deathday && isNaN(end.getTime())) end = new Date(); + + let age = end.getFullYear() - birth.getFullYear(); + const monthDiff = end.getMonth() - birth.getMonth(); + if (monthDiff < 0 || (monthDiff === 0 && end.getDate() < birth.getDate())) { + age--; + } + + return age; +} diff --git a/react_frontend/src/pages/PeoplePage.tsx b/react_frontend/src/pages/PeoplePage.tsx index a7035c8..03b18af 100644 --- a/react_frontend/src/pages/PeoplePage.tsx +++ b/react_frontend/src/pages/PeoplePage.tsx @@ -8,8 +8,13 @@ import { Skeleton } from "@/components/ui/skeleton"; import { Switch } from "@/components/ui/switch"; import { Label } from "@/components/ui/label"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import { + Tooltip, + TooltipContent, + TooltipTrigger, +} from "@/components/ui/tooltip"; import { Search, Heart } from "lucide-react"; -import { getProfileUrl } from "@/lib/media-utils"; +import { getProfileUrl, formatDate, calculateAge } from "@/lib/media-utils"; type PeopleFilter = "all" | "favorite"; @@ -207,6 +212,18 @@ export function PeoplePage() {

{person.name}

+ {person.birthday && ( + + }> + + Born: {formatDate(person.birthday)} + + + + Age: {calculateAge(person.birthday, person.deathday)} + + + )} ))} diff --git a/react_frontend/src/pages/PersonDetailsPage.tsx b/react_frontend/src/pages/PersonDetailsPage.tsx index 11257e1..92501aa 100644 --- a/react_frontend/src/pages/PersonDetailsPage.tsx +++ b/react_frontend/src/pages/PersonDetailsPage.tsx @@ -1,4 +1,4 @@ -import { useParams, useNavigate, useLocation } from "react-router-dom"; +import { useParams, useNavigate } from "react-router-dom"; import { useState, useEffect } from "react"; import { usePerson, @@ -13,8 +13,14 @@ 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 { + Tooltip, + TooltipContent, + TooltipTrigger, +} from "@/components/ui/tooltip"; import { EntryCard } from "@/components/entries/EntryCard"; -import { getProfileUrl, formatDate } from "@/lib/media-utils"; +import { getTmdbImageUrl } from "@/lib/api"; +import { formatDate, calculateAge } from "@/lib/media-utils"; import { ChevronLeft, ChevronRight, @@ -24,58 +30,131 @@ import { ExternalLink, } from "lucide-react"; import type { TmdbEntry } from "@/types"; -import type { TmdbFilmographyItem } from "@/api/people"; +import type { + TmdbFilmographyItem, + TmdbFilmographyResponse, +} from "@/api/people"; -const TMDB_IMG = "https://image.tmdb.org/t/p"; - -export function PersonDetailsPage() { - const { id, tmdbId } = useParams<{ id?: string; tmdbId?: string }>(); - const location = useLocation(); - const isTmdbMode = location.pathname.startsWith("/people/tmdb/"); - - if (isTmdbMode && tmdbId) { - return ; - } - - return ; +/** Person data normalized from either the local DB or a TMDB preview. */ +interface PersonProfile { + 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; + tmdb_id: number | null; + favorite: boolean; } -/* ─── Local DB person ─── */ - -function LocalPersonView({ personId }: { personId: number }) { +/** + * Person page for both sources: + * - `/people/:id`: the person is in the local DB, so details come from there + * and the filmography from TMDB via the local id. + * - `/people/tmdb/:tmdbId`: the person is not in the local DB, so everything + * comes from the TMDB preview. If the preview reports a local id, redirect. + */ +export function PersonDetailsPage() { + const { id, tmdbId } = useParams<{ id?: string; tmdbId?: string }>(); const navigate = useNavigate(); - const { data: person, isLoading, isError } = usePerson(personId); - const { data: tmdbFilmography, isLoading: tmdbLoading } = - usePersonTmdbFilmography(personId); + const localId = id ? parseInt(id, 10) : 0; + const isLocal = !!localId; + const tmdbIdNum = !isLocal && tmdbId ? parseInt(tmdbId, 10) : 0; + + // Queries are disabled when their id is 0, so only one source is fetched. + const localPerson = usePerson(localId); + const localFilmography = usePersonTmdbFilmography(localId); + const tmdbPreview = useTmdbPersonPreview(tmdbIdNum); + const updatePerson = useUpdatePerson(); + const fetchPerson = useFetchPersonByTmdbId(); useLibrary(); const libraryMap = useLibraryStore((s) => s.entries); const libraryLoaded = useLibraryStore((s) => s.isLoaded); + const existingLocalId = tmdbPreview.data?.local_id; + useEffect(() => { + if (existingLocalId) { + navigate(`/people/${existingLocalId}`, { replace: true }); + } + }, [existingLocalId, navigate]); + + const isError = isLocal ? localPerson.isError : tmdbPreview.isError; + if (isError) { return (

Person not found

- This person could not be loaded. They may not exist in the database. + {isLocal + ? "This person could not be loaded. They may not exist in the database." + : "Could not load person data from TMDB."}

-
); } - if (isLoading || !person) { + let person: PersonProfile | null = null; + let filmography: TmdbFilmographyResponse | undefined; + let filmographyLoading = false; + + if (isLocal && localPerson.data) { + const p = localPerson.data; + person = { + name: p.name, + biography: p.biography ?? null, + birthday: p.birthday ?? null, + deathday: p.deathday ?? null, + place_of_birth: p.place_of_birth ?? null, + profile_path: p.profile_path ?? null, + known_for_department: p.known_for_department ?? null, + tmdb_id: p.tmdb_id ?? null, + favorite: !!p.favorite, + }; + filmography = localFilmography.data; + filmographyLoading = localFilmography.isLoading; + } else if (!isLocal && tmdbPreview.data && !existingLocalId) { + const p = tmdbPreview.data; + person = { ...p, favorite: false }; + filmography = p.filmography; + } + + // Still loading, or about to redirect to the local page. + if (!person) { return ; } + const isFavorite = person.favorite; + + const toggleFavorite = () => { + updatePerson.mutate({ id: localId, data: { favorite: !isFavorite } }); + }; + + const addToLibrary = () => { + fetchPerson.mutate(tmdbIdNum, { + onSuccess: (added) => { + navigate(`/people/${added.id}`, { replace: true }); + }, + }); + }; + return (
@@ -88,22 +167,33 @@ function LocalPersonView({ personId }: { personId: number }) {

{person.name}

- + {isLocal ? ( + + ) : ( + + )}
-
- ); -} - -/* ─── 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 person already exists in local DB, redirect to their local page - useEffect(() => { - if (preview?.local_id) { - navigate(`/people/${preview.local_id}`, { replace: true }); - } - }, [preview?.local_id, navigate]); - - if (isError) { - return ( -
-

Person not found

-

- Could not load person data from TMDB. -

- -
- ); - } - - if (isLoading || !preview) { - return ; - } - - // Redirecting to local page, show skeleton while navigating - if (preview.local_id) { - 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} -

- )} -
-
- - - -
); @@ -260,7 +245,14 @@ function PersonMeta({ {birthday && ( <> • - Born: {formatDate(birthday)} + + }> + Born: {formatDate(birthday)} + + + Age: {calculateAge(birthday, deathday)} + + )} {deathday && ( diff --git a/react_frontend/src/pages/PersonTmdbRedirectPage.tsx b/react_frontend/src/pages/PersonTmdbRedirectPage.tsx deleted file mode 100644 index fcc38b8..0000000 --- a/react_frontend/src/pages/PersonTmdbRedirectPage.tsx +++ /dev/null @@ -1,364 +0,0 @@ -import { useState } from "react"; -import { useParams, useNavigate } from "react-router-dom"; -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); - - if (isLoading || !preview) { - return ( -
-
- -
- - - -
-
-
- ); - } - - 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} -

- )} -
- ); - })} -
-
- )} -
- ); -}