Unify person page for local and TMDB people
- Merge local and TMDB person views into a single PersonDetailsPage - Remove unused PersonTmdbRedirectPage - Show age tooltip on birthdays (person page and people list)
This commit is contained in:
1 parent
6d4c3e185a
commit
f88dfdae44
4 files changed
+183
-517
No files matched your search
@@ -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 <TmdbPersonView tmdbId={parseInt(tmdbId, 10)} />;
|
||||
}
|
||||
|
||||
return <LocalPersonView personId={parseInt(id!, 10)} />;
|
||||
/** 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 (
|
||||
<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">
|
||||
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."}
|
||||
</p>
|
||||
<Button variant="outline" onClick={() => navigate("/people")}>
|
||||
Back to People
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => (isLocal ? navigate("/people") : navigate(-1))}
|
||||
>
|
||||
{isLocal ? "Back to People" : "Go back"}
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
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 <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={getProfileUrl(person.profile_path)}
|
||||
src={
|
||||
person.profile_path
|
||||
? getTmdbImageUrl(person.profile_path, "w300")
|
||||
: undefined
|
||||
}
|
||||
alt={person.name}
|
||||
/>
|
||||
<AvatarFallback className="text-2xl sm:text-3xl">
|
||||
@@ -88,22 +167,33 @@ function LocalPersonView({ personId }: { personId: number }) {
|
||||
<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>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-8 w-8 shrink-0"
|
||||
onClick={() =>
|
||||
updatePerson.mutate({
|
||||
id: person.id,
|
||||
data: { favorite: !person.favorite },
|
||||
})
|
||||
}
|
||||
disabled={updatePerson.isPending}
|
||||
>
|
||||
<Heart
|
||||
className={`h-5 w-5 ${person.favorite ? "fill-red-500 text-red-500" : "text-muted-foreground"}`}
|
||||
/>
|
||||
</Button>
|
||||
{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}
|
||||
@@ -123,117 +213,12 @@ function LocalPersonView({ personId }: { personId: number }) {
|
||||
<Separator />
|
||||
|
||||
<FilmographySection
|
||||
// Reset pagination when moving between people on the same route
|
||||
key={isLocal ? `local-${localId}` : `tmdb-${tmdbIdNum}`}
|
||||
libraryMap={libraryMap}
|
||||
libraryLoaded={libraryLoaded}
|
||||
tmdbFilmography={tmdbFilmography}
|
||||
tmdbLoading={tmdbLoading}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/* ─── 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 (
|
||||
<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">
|
||||
Could not load person data from TMDB.
|
||||
</p>
|
||||
<Button variant="outline" onClick={() => navigate(-1)}>
|
||||
Go back
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (isLoading || !preview) {
|
||||
return <PersonSkeleton />;
|
||||
}
|
||||
|
||||
// Redirecting to local page, show skeleton while navigating
|
||||
if (preview.local_id) {
|
||||
return <PersonSkeleton />;
|
||||
}
|
||||
|
||||
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 (
|
||||
<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={profileUrl} alt={preview.name} />
|
||||
<AvatarFallback className="text-2xl sm:text-3xl">
|
||||
{preview.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-3 justify-center sm:justify-start">
|
||||
<h1 className="text-2xl font-bold sm:text-3xl">{preview.name}</h1>
|
||||
<Button
|
||||
onClick={handleAddToLibrary}
|
||||
disabled={fetchPerson.isPending}
|
||||
size="sm"
|
||||
>
|
||||
{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={preview.known_for_department}
|
||||
birthday={preview.birthday}
|
||||
deathday={preview.deathday}
|
||||
place_of_birth={preview.place_of_birth}
|
||||
tmdb_id={preview.tmdb_id}
|
||||
/>
|
||||
{preview.biography && (
|
||||
<p className="text-sm text-muted-foreground leading-relaxed max-w-2xl line-clamp-6">
|
||||
{preview.biography}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Separator />
|
||||
|
||||
<FilmographySection
|
||||
libraryMap={libraryMap}
|
||||
libraryLoaded={libraryLoaded}
|
||||
tmdbFilmography={preview.filmography}
|
||||
tmdbLoading={false}
|
||||
tmdbFilmography={filmography}
|
||||
tmdbLoading={filmographyLoading}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
@@ -260,7 +245,14 @@ function PersonMeta({
|
||||
{birthday && (
|
||||
<>
|
||||
<span>•</span>
|
||||
<span>Born: {formatDate(birthday)}</span>
|
||||
<Tooltip>
|
||||
<TooltipTrigger render={<span className="cursor-default" />}>
|
||||
Born: {formatDate(birthday)}
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
Age: {calculateAge(birthday, deathday)}
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</>
|
||||
)}
|
||||
{deathday && (
|
||||
|
||||
Reference in new issue
Block a user