feat(people): unify person detail page for DB and TMDB sources
- Merge PersonDetailsPage and PersonTmdbRedirectPage into one component - /people/:id fetches from local DB with favorite toggle - /people/tmdb/:tmdbId fetches from TMDB with "Add to Library" button - Add TMDB external link in person metadata - Fix CastList links for TMDB preview cast (use /people/tmdb/:tmdbId) - Replace 11MB getAll() with server-side paginated infinite scroll - Add useLocalPeopleInfinite hook and PaginatedPeopleResponse type
This commit is contained in:
1 parent
6d676d803c
commit
db6da6782f
6 files changed
+231
-59
No files matched your search
@@ -1,6 +1,12 @@
|
||||
import { useParams, useNavigate } from 'react-router-dom';
|
||||
import { useParams, useNavigate, useLocation } from 'react-router-dom';
|
||||
import { useState } from 'react';
|
||||
import { usePerson, usePersonTmdbFilmography, useUpdatePerson } from '@/hooks/usePeople';
|
||||
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';
|
||||
@@ -9,13 +15,27 @@ 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 { ChevronLeft, ChevronRight, Heart } from 'lucide-react';
|
||||
import { ChevronLeft, ChevronRight, Heart, Plus, Loader2, ExternalLink } 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 PersonDetailsPage() {
|
||||
const { id } = useParams<{ id: string }>();
|
||||
const personId = parseInt(id!, 10);
|
||||
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)} />;
|
||||
}
|
||||
|
||||
/* ─── Local DB person ─── */
|
||||
|
||||
function LocalPersonView({ personId }: { personId: number }) {
|
||||
const navigate = useNavigate();
|
||||
const { data: person, isLoading, isError } = usePerson(personId);
|
||||
const { data: tmdbFilmography, isLoading: tmdbLoading } = usePersonTmdbFilmography(personId);
|
||||
@@ -39,22 +59,11 @@ export function PersonDetailsPage() {
|
||||
}
|
||||
|
||||
if (isLoading || !person) {
|
||||
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>
|
||||
);
|
||||
return <PersonSkeleton />;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
<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)} alt={person.name} />
|
||||
@@ -75,12 +84,13 @@ export function PersonDetailsPage() {
|
||||
<Heart className={`h-5 w-5 ${person.favorite ? 'fill-red-500 text-red-500' : 'text-muted-foreground'}`} />
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex flex-wrap items-center justify-center gap-2 text-sm text-muted-foreground sm:justify-start sm:gap-3">
|
||||
{person.known_for_department && <span>{person.known_for_department}</span>}
|
||||
{person.birthday && (<><span>•</span><span>Born: {formatDate(person.birthday)}</span></>)}
|
||||
{person.deathday && (<><span>•</span><span>Died: {formatDate(person.deathday)}</span></>)}
|
||||
{person.place_of_birth && (<><span>•</span><span>{person.place_of_birth}</span></>)}
|
||||
</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}
|
||||
@@ -101,6 +111,148 @@ export function PersonDetailsPage() {
|
||||
);
|
||||
}
|
||||
|
||||
/* ─── 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 (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 />;
|
||||
}
|
||||
|
||||
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}
|
||||
/>
|
||||
</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><span>Born: {formatDate(birthday)}</span></>)}
|
||||
{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;
|
||||
|
||||
Reference in new issue
Block a user