- Merge local and TMDB person views into a single PersonDetailsPage - Remove unused PersonTmdbRedirectPage - Show age tooltip on birthdays (person page and people list)
536 lines
17 KiB
TypeScript
536 lines
17 KiB
TypeScript
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 (
|
||
<div className="flex flex-col items-center justify-center gap-3 py-20">
|
||
<p className="text-lg font-semibold">Person not found</p>
|
||
<p className="text-sm text-muted-foreground">
|
||
{isLocal
|
||
? "This person could not be loaded. They may not exist in the database."
|
||
: "Could not load person data from TMDB."}
|
||
</p>
|
||
<Button
|
||
variant="outline"
|
||
onClick={() => (isLocal ? navigate("/people") : navigate(-1))}
|
||
>
|
||
{isLocal ? "Back to People" : "Go back"}
|
||
</Button>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
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 <PersonSkeleton />;
|
||
}
|
||
|
||
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 (
|
||
<div className="space-y-8 animate-page-enter">
|
||
<div className="flex flex-col items-center gap-4 sm:flex-row sm:items-start sm:gap-8">
|
||
<Avatar className="h-32 w-32 shrink-0 sm:h-48 sm:w-48">
|
||
<AvatarImage
|
||
src={
|
||
person.profile_path
|
||
? getTmdbImageUrl(person.profile_path, "w300")
|
||
: undefined
|
||
}
|
||
alt={person.name}
|
||
/>
|
||
<AvatarFallback className="text-2xl sm:text-3xl">
|
||
{person.name
|
||
.split(" ")
|
||
.map((n: string) => n[0])
|
||
.join("")}
|
||
</AvatarFallback>
|
||
</Avatar>
|
||
<div className="space-y-2 text-center sm:text-left sm:space-y-3">
|
||
<div className="flex items-center gap-2 justify-center sm:justify-start">
|
||
<h1 className="text-2xl font-bold sm:text-3xl">{person.name}</h1>
|
||
{isLocal ? (
|
||
<Button
|
||
variant="ghost"
|
||
size="icon"
|
||
className="h-8 w-8 shrink-0"
|
||
onClick={toggleFavorite}
|
||
disabled={updatePerson.isPending}
|
||
>
|
||
<Heart
|
||
className={`h-5 w-5 ${isFavorite ? "fill-red-500 text-red-500" : "text-muted-foreground"}`}
|
||
/>
|
||
</Button>
|
||
) : (
|
||
<Button
|
||
onClick={addToLibrary}
|
||
disabled={fetchPerson.isPending}
|
||
size="sm"
|
||
className="ml-1"
|
||
>
|
||
{fetchPerson.isPending ? (
|
||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||
) : (
|
||
<Plus className="mr-2 h-4 w-4" />
|
||
)}
|
||
{fetchPerson.isPending ? "Adding..." : "Add to Library"}
|
||
</Button>
|
||
)}
|
||
</div>
|
||
<PersonMeta
|
||
known_for_department={person.known_for_department}
|
||
birthday={person.birthday}
|
||
deathday={person.deathday}
|
||
place_of_birth={person.place_of_birth}
|
||
tmdb_id={person.tmdb_id}
|
||
/>
|
||
{person.biography && (
|
||
<p className="text-sm text-muted-foreground leading-relaxed max-w-2xl line-clamp-6">
|
||
{person.biography}
|
||
</p>
|
||
)}
|
||
</div>
|
||
</div>
|
||
|
||
<Separator />
|
||
|
||
<FilmographySection
|
||
// Reset pagination when moving between people on the same route
|
||
key={isLocal ? `local-${localId}` : `tmdb-${tmdbIdNum}`}
|
||
libraryMap={libraryMap}
|
||
libraryLoaded={libraryLoaded}
|
||
tmdbFilmography={filmography}
|
||
tmdbLoading={filmographyLoading}
|
||
/>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
/* ─── 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 (
|
||
<div className="flex flex-wrap items-center justify-center gap-2 text-sm text-muted-foreground sm:justify-start sm:gap-3">
|
||
{known_for_department && <span>{known_for_department}</span>}
|
||
{birthday && (
|
||
<>
|
||
<span>•</span>
|
||
<Tooltip>
|
||
<TooltipTrigger render={<span className="cursor-default" />}>
|
||
Born: {formatDate(birthday)}
|
||
</TooltipTrigger>
|
||
<TooltipContent>
|
||
Age: {calculateAge(birthday, deathday)}
|
||
</TooltipContent>
|
||
</Tooltip>
|
||
</>
|
||
)}
|
||
{deathday && (
|
||
<>
|
||
<span>•</span>
|
||
<span>Died: {formatDate(deathday)}</span>
|
||
</>
|
||
)}
|
||
{place_of_birth && (
|
||
<>
|
||
<span>•</span>
|
||
<span>{place_of_birth}</span>
|
||
</>
|
||
)}
|
||
{tmdb_id && (
|
||
<>
|
||
<span>•</span>
|
||
<a
|
||
href={`https://www.themoviedb.org/person/${tmdb_id}`}
|
||
target="_blank"
|
||
rel="noopener noreferrer"
|
||
className="inline-flex items-center gap-1 text-muted-foreground hover:text-foreground transition-colors"
|
||
>
|
||
<ExternalLink className="h-3 w-3" />
|
||
TMDB
|
||
</a>
|
||
</>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function PersonSkeleton() {
|
||
return (
|
||
<div className="space-y-6">
|
||
<div className="flex flex-col items-center gap-4 sm:flex-row sm:items-start sm:gap-6">
|
||
<Skeleton className="h-32 w-32 rounded-full sm:h-48 sm:w-48" />
|
||
<div className="flex-1 space-y-3 w-full">
|
||
<Skeleton className="h-8 w-48 mx-auto sm:mx-0" />
|
||
<Skeleton className="h-4 w-32 mx-auto sm:mx-0" />
|
||
<Skeleton className="h-20 w-full" />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
/* ─── Filmography ─── */
|
||
|
||
interface FilmographySectionProps {
|
||
libraryMap: Map<number, import("@/types").Entry>;
|
||
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-2 gap-3 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6";
|
||
|
||
if (tmdbLoading || !libraryLoaded) {
|
||
return (
|
||
<div className="space-y-4">
|
||
<h2 className="text-xl font-semibold">Filmography</h2>
|
||
<div className={GRID}>
|
||
{Array.from({ length: 6 }).map((_, i) => (
|
||
<Skeleton key={i} className="aspect-2/3 rounded-lg" />
|
||
))}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
const allTmdbCredits = [
|
||
...(tmdbFilmography?.cast ?? []),
|
||
...(tmdbFilmography?.crew?.filter(
|
||
(c) => c.job === "Director" || c.department === "Directing",
|
||
) ?? []),
|
||
];
|
||
const seenTmdbIds = new Set<number>();
|
||
const dedupedCredits = allTmdbCredits.filter((c) => {
|
||
if (seenTmdbIds.has(c.id)) return false;
|
||
seenTmdbIds.add(c.id);
|
||
return true;
|
||
});
|
||
|
||
const tmdbIdToLibraryEntry = new Map<number, import("@/types").Entry>();
|
||
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));
|
||
|
||
// "Not in library": sort by release date (new→old), push "self" roles to the end
|
||
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 otherCreditsSorted = 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;
|
||
// Both same self status → sort by date descending (new first)
|
||
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;
|
||
});
|
||
const otherCredits = otherCreditsSorted;
|
||
|
||
if (sorted.length === 0) {
|
||
return (
|
||
<div className="space-y-4">
|
||
<h2 className="text-xl font-semibold">Filmography</h2>
|
||
<p className="text-sm text-muted-foreground">No filmography found.</p>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
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 (
|
||
<div className="space-y-8">
|
||
<h2 className="text-xl font-semibold">Filmography</h2>
|
||
|
||
{libraryCredits.length > 0 && (
|
||
<div className="space-y-3">
|
||
<div className="flex items-center justify-between">
|
||
<h3 className="text-sm font-medium text-muted-foreground uppercase tracking-wide">
|
||
In Your Library ({libraryCredits.length})
|
||
</h3>
|
||
{libraryTotalPages > 1 && (
|
||
<div className="flex items-center gap-1">
|
||
<Button
|
||
variant="ghost"
|
||
size="icon"
|
||
className="h-7 w-7"
|
||
disabled={libraryPage === 0}
|
||
onClick={() => setLibraryPage((p) => p - 1)}
|
||
>
|
||
<ChevronLeft className="h-4 w-4" />
|
||
</Button>
|
||
<span className="text-xs text-muted-foreground tabular-nums">
|
||
{libraryPage + 1}/{libraryTotalPages}
|
||
</span>
|
||
<Button
|
||
variant="ghost"
|
||
size="icon"
|
||
className="h-7 w-7"
|
||
disabled={libraryPage === libraryTotalPages - 1}
|
||
onClick={() => setLibraryPage((p) => p + 1)}
|
||
>
|
||
<ChevronRight className="h-4 w-4" />
|
||
</Button>
|
||
</div>
|
||
)}
|
||
</div>
|
||
<div className={GRID}>
|
||
{librarySlice.map((credit) => {
|
||
const entry = tmdbIdToLibraryEntry.get(credit.id)!;
|
||
return (
|
||
<div key={credit.id} className="space-y-1">
|
||
<EntryCard entry={entry} />
|
||
{credit.character && (
|
||
<p className="text-xs text-muted-foreground text-center line-clamp-1 px-1">
|
||
{credit.character}
|
||
</p>
|
||
)}
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{otherCredits.length > 0 && (
|
||
<div className="space-y-3">
|
||
<div className="flex items-center justify-between">
|
||
<h3 className="text-sm font-medium text-muted-foreground uppercase tracking-wide">
|
||
Not in Library ({otherCredits.length})
|
||
</h3>
|
||
{otherTotalPages > 1 && (
|
||
<div className="flex items-center gap-1">
|
||
<Button
|
||
variant="ghost"
|
||
size="icon"
|
||
className="h-7 w-7"
|
||
disabled={otherPage === 0}
|
||
onClick={() => setOtherPage((p) => p - 1)}
|
||
>
|
||
<ChevronLeft className="h-4 w-4" />
|
||
</Button>
|
||
<span className="text-xs text-muted-foreground tabular-nums">
|
||
{otherPage + 1}/{otherTotalPages}
|
||
</span>
|
||
<Button
|
||
variant="ghost"
|
||
size="icon"
|
||
className="h-7 w-7"
|
||
disabled={otherPage === otherTotalPages - 1}
|
||
onClick={() => setOtherPage((p) => p + 1)}
|
||
>
|
||
<ChevronRight className="h-4 w-4" />
|
||
</Button>
|
||
</div>
|
||
)}
|
||
</div>
|
||
<div className={GRID}>
|
||
{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 (
|
||
<div key={credit.id} className="space-y-1">
|
||
<EntryCard tmdbEntry={tmdbEntry} />
|
||
{credit.character && (
|
||
<p className="text-xs text-muted-foreground text-center line-clamp-1 px-1">
|
||
{credit.character}
|
||
</p>
|
||
)}
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|