Home page cards now check library store and link directly to EntryDetailsPage when the entry is already added. Created useTmdbEntryLink hook for shared library-check logic. Added TmdbCastsPage with filter/search/sort for TMDB preview cast & crew, and limited credits to 12 on TmdbPreviewPage.
262 lines
8.9 KiB
TypeScript
262 lines
8.9 KiB
TypeScript
import { useParams, useNavigate } from "react-router-dom";
|
|
import { useEffect } from "react";
|
|
import { useTmdbPreview, useCreateEntry } from "@/hooks/useEntries";
|
|
import { useLibraryStore } from "@/stores/useLibraryStore";
|
|
import { useCommandPaletteStore } from "@/stores/useCommandPaletteStore";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Separator } from "@/components/ui/separator";
|
|
import { Star, Plus, Loader2, ExternalLink } from "lucide-react";
|
|
import { EntryDetailsSkeleton } from "@/components/skeletons/PageSkeletons";
|
|
import { CastList } from "@/components/entries/CastList";
|
|
import { getYear, formatRuntime } from "@/lib/media-utils";
|
|
import type { Credit } from "@/types";
|
|
|
|
const TMDB_IMG = "https://image.tmdb.org/t/p";
|
|
|
|
export function TmdbPreviewPage() {
|
|
const { type, tmdbId } = useParams<{ type: string; tmdbId: string }>();
|
|
const navigate = useNavigate();
|
|
const tmdbIdNum = parseInt(tmdbId!, 10);
|
|
const mediaType = type as "movie" | "tv";
|
|
|
|
const {
|
|
data: preview,
|
|
isLoading,
|
|
isError,
|
|
} = useTmdbPreview(tmdbIdNum, mediaType);
|
|
const createEntry = useCreateEntry();
|
|
|
|
// Check if already in library
|
|
const libraryEntry = useLibraryStore((s) => {
|
|
for (const e of s.entries.values()) {
|
|
if (e.entryIds?.tmdb_id === tmdbIdNum) return e;
|
|
}
|
|
return null;
|
|
});
|
|
|
|
// Redirect immediately if entry is already in library — skip the preview step
|
|
useEffect(() => {
|
|
if (libraryEntry) {
|
|
navigate(`/entry/${libraryEntry.id}`, { replace: true });
|
|
}
|
|
}, [libraryEntry, navigate]);
|
|
|
|
// Track recently viewed for command palette
|
|
const addRecentlyViewed = useCommandPaletteStore((s) => s.addRecentlyViewed);
|
|
useEffect(() => {
|
|
if (preview) {
|
|
addRecentlyViewed({
|
|
id: `tmdb-${tmdbIdNum}`,
|
|
title: preview.title,
|
|
type: mediaType,
|
|
poster_path: preview.poster_path,
|
|
libraryId: libraryEntry?.id,
|
|
tmdbId: tmdbIdNum,
|
|
});
|
|
}
|
|
}, [tmdbIdNum, preview?.title]);
|
|
|
|
if (isLoading || !preview) {
|
|
return <EntryDetailsSkeleton />;
|
|
}
|
|
|
|
if (isError) {
|
|
return (
|
|
<div className="flex flex-col items-center justify-center gap-4 py-20">
|
|
<p className="text-muted-foreground">Could not load entry from TMDB.</p>
|
|
<Button variant="outline" onClick={() => navigate(-1)}>
|
|
Go back
|
|
</Button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const backdrop = preview.backdrop_path
|
|
? `${TMDB_IMG}/w1280${preview.backdrop_path}`
|
|
: null;
|
|
const poster = preview.poster_path
|
|
? `${TMDB_IMG}/w500${preview.poster_path}`
|
|
: "/images/default-poster.png";
|
|
|
|
const handleAddToLibrary = () => {
|
|
createEntry.mutate(
|
|
{ tmdb_id: preview.tmdb_id, is_tvserie: preview.is_tvseries },
|
|
{
|
|
onSuccess: (newEntry) =>
|
|
navigate(`/entry/${newEntry.id}`, { replace: true }),
|
|
},
|
|
);
|
|
};
|
|
|
|
return (
|
|
<div>
|
|
{/* Backdrop banner */}
|
|
<div className="relative -mx-6 -mt-6 h-52 overflow-hidden">
|
|
{backdrop ? (
|
|
<img
|
|
src={backdrop}
|
|
alt=""
|
|
className="h-full w-full object-cover opacity-25 blur-sm scale-105"
|
|
/>
|
|
) : (
|
|
<div className="h-full w-full bg-linear-to-br from-primary/15 to-primary/5" />
|
|
)}
|
|
<div className="absolute inset-0 bg-linear-to-t from-background via-background/60 to-transparent" />
|
|
</div>
|
|
|
|
{/* Main content */}
|
|
<div className="mx-auto max-w-4xl">
|
|
{/* Add to Library button — top right */}
|
|
<div className="flex justify-end gap-2 -mt-10 relative z-10 mb-2">
|
|
{libraryEntry ? (
|
|
<Button
|
|
variant="outline"
|
|
onClick={() => navigate(`/entry/${libraryEntry.id}`)}
|
|
>
|
|
Already in Library — View
|
|
</Button>
|
|
) : (
|
|
<Button
|
|
onClick={handleAddToLibrary}
|
|
disabled={createEntry.isPending}
|
|
>
|
|
{createEntry.isPending ? (
|
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
|
) : (
|
|
<Plus className="mr-2 h-4 w-4" />
|
|
)}
|
|
{createEntry.isPending ? "Adding..." : "Add to Library"}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
|
|
{/* Two-column header: poster | info */}
|
|
<div className="flex flex-col gap-6 relative items-start sm:flex-row sm:gap-8 sm:-mt-8">
|
|
{/* LEFT: Poster */}
|
|
<div className="w-32 sm:w-44 shrink-0 self-start mx-auto sm:mx-0">
|
|
<img
|
|
src={poster}
|
|
alt={preview.title}
|
|
className="w-full aspect-2/3 rounded-xl object-cover shadow-2xl ring-1 ring-border/50"
|
|
/>
|
|
{/* External links */}
|
|
<div className="flex items-center justify-center gap-3 mt-3">
|
|
{preview.tmdb_id && (
|
|
<a
|
|
href={`https://www.themoviedb.org/${preview.is_tvseries ? "tv" : "movie"}/${preview.tmdb_id}`}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="inline-flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground transition-colors"
|
|
>
|
|
<ExternalLink className="h-3 w-3" />
|
|
TMDB
|
|
</a>
|
|
)}
|
|
{preview.imdb_id && (
|
|
<a
|
|
href={`https://www.imdb.com/title/${preview.imdb_id}`}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="inline-flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground transition-colors"
|
|
>
|
|
<ExternalLink className="h-3 w-3" />
|
|
IMDb
|
|
</a>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* RIGHT: Info */}
|
|
<div className="flex-1 min-w-0 space-y-4">
|
|
{/* Title */}
|
|
<div>
|
|
<h1 className="text-3xl font-bold leading-tight">
|
|
{preview.title}
|
|
</h1>
|
|
</div>
|
|
|
|
{/* Meta */}
|
|
<div className="flex flex-wrap items-center gap-2 text-sm text-muted-foreground">
|
|
{preview.release_date && (
|
|
<span>{getYear(preview.release_date)}</span>
|
|
)}
|
|
{preview.is_tvseries && preview.status && (
|
|
<>
|
|
<span>•</span>
|
|
<span>{preview.status}</span>
|
|
</>
|
|
)}
|
|
{preview.runtime && !preview.is_tvseries && (
|
|
<>
|
|
{preview.release_date && <span>•</span>}
|
|
<span>{formatRuntime(preview.runtime)}</span>
|
|
</>
|
|
)}
|
|
</div>
|
|
|
|
{/* TV series season count */}
|
|
{preview.is_tvseries && preview.number_of_seasons != null && (
|
|
<div className="flex items-center gap-2 text-sm">
|
|
<Badge variant="secondary">
|
|
{preview.number_of_seasons}{" "}
|
|
{preview.number_of_seasons === 1 ? "Season" : "Seasons"}
|
|
</Badge>
|
|
{preview.number_of_episodes != null && (
|
|
<span className="text-muted-foreground">
|
|
({preview.number_of_episodes} episodes)
|
|
</span>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{/* Genres */}
|
|
{preview.genres.length > 0 && (
|
|
<div className="flex flex-wrap gap-1.5">
|
|
{preview.genres.map((g) => (
|
|
<Badge key={g.id} variant="outline" className="text-xs">
|
|
{g.name}
|
|
</Badge>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
{/* Rating */}
|
|
{preview.vote_average != null && preview.vote_average > 0 && (
|
|
<div className="flex items-center gap-2">
|
|
<span className="inline-flex items-center gap-1 rounded-full bg-blue-500/10 border border-blue-500/20 px-3 py-1 text-sm text-blue-600 dark:text-blue-400 font-medium">
|
|
<span className="font-bold">TMDB:</span>
|
|
{preview.vote_average.toFixed(1)}/10
|
|
</span>
|
|
</div>
|
|
)}
|
|
|
|
{/* Storyline */}
|
|
{preview.storyline && (
|
|
<>
|
|
<p className="text-sm text-muted-foreground leading-relaxed max-w-prose">
|
|
{preview.storyline}
|
|
</p>
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Sections */}
|
|
<div className="mt-10 space-y-8">
|
|
{preview.credits && preview.credits.length > 0 && (
|
|
<>
|
|
<Separator />
|
|
<CastList
|
|
credits={preview.credits as unknown as Credit[]}
|
|
maxItems={12}
|
|
allCastHref={`/entry/tmdb/${mediaType}/${tmdbIdNum}/cast`}
|
|
/>
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|