import { useParams, useNavigate } from "react-router-dom";
import { useState, useEffect } from "react";
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";
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 { getTmdbImageUrl } from "@/lib/api";
import { formatDate, calculateAge } from "@/lib/media-utils";
import {
ChevronLeft,
ChevronRight,
Heart,
Plus,
Loader2,
ExternalLink,
} from "lucide-react";
import type { TmdbEntry } from "@/types";
import type {
TmdbFilmographyItem,
TmdbFilmographyResponse,
} from "@/api/people";
/** 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;
}
/**
* 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 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
{isLocal
? "This person could not be loaded. They may not exist in the database."
: "Could not load person data from TMDB."}
);
}
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 (