import { useState, useEffect } from "react"; import { useParams, Link, useNavigate } from "react-router-dom"; import { useEntry, useEntrySeasons, useRefreshEntry, useEntryCredits, } from "@/hooks/useEntries"; import { useEntrySeenByEntry, useDeleteEntrySeen } from "@/hooks/useEntrySeen"; import { useCollections } from "@/hooks/useCollections"; 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 { ScrollArea, ScrollBar } from "@/components/ui/scroll-area"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Heart, MoreHorizontal, Eye, EyeOff, RefreshCw, FolderPlus, ImageIcon, Settings, Star, History, Trash2, ExternalLink, Play, BookmarkCheck, } from "lucide-react"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip"; import { CastList } from "@/components/entries/CastList"; import { SeasonsContainer } from "@/components/entries/SeasonsContainer"; import { StreamingSection } from "@/components/streaming/StreamingSection"; // EpisodeWatchHistory is available for future use import { EntrySettingsDialog } from "@/components/dialogs/EntrySettingsDialog"; import { EntrySeenDialog } from "@/components/dialogs/EntrySeenDialog"; import { EntryMediaDialog } from "@/components/dialogs/EntryMediaDialog"; import { AddEntryToCollectionDialog } from "@/components/dialogs/AddEntryToCollectionDialog"; import { RatingDialog } from "@/components/dialogs/RatingDialog"; import { MovieWatchHistoryDialog } from "@/components/dialogs/MovieWatchHistoryDialog"; import { ConfirmDialog } from "@/components/dialogs/ConfirmDialog"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { EntryDetailsSkeleton } from "@/components/skeletons/PageSkeletons"; import { getPosterUrl, getFullPosterUrl, getBackdropUrl, getCollectionBackdropUrl, getYear, formatRuntime, formatDate, } from "@/lib/media-utils"; import { useUpdateEntry, useDeleteEntry } from "@/hooks/useEntries"; import type { Collection, Entry, EntrySeen } from "@/types"; const WATCH_STATUSES = [ { value: "plan_to_watch", label: "Plan to Watch", color: "bg-slate-500/15 border-slate-500/30 text-slate-600 dark:text-slate-400", }, { value: "watching", label: "Watching", color: "bg-blue-500/15 border-blue-500/30 text-blue-600 dark:text-blue-400", }, { value: "completed", label: "Completed", color: "bg-green-500/15 border-green-500/30 text-green-600 dark:text-green-400", }, { value: "on_hold", label: "On Hold", color: "bg-amber-500/15 border-amber-500/30 text-amber-600 dark:text-amber-400", }, { value: "dropped", label: "Dropped", color: "bg-red-500/15 border-red-500/30 text-red-600 dark:text-red-400", }, ]; function getWatchStatusInfo(value: string | null | undefined) { return WATCH_STATUSES.find((s) => s.value === value); } export function EntryDetailsPage() { const { id } = useParams<{ id: string }>(); const entryId = parseInt(id!, 10); const navigate = useNavigate(); const { data: entry, isLoading } = useEntry(entryId); const { data: seasons } = useEntrySeasons(entryId); const { data: allCredits = [] } = useEntryCredits(entryId); const { data: watchHistory = [] } = useEntrySeenByEntry(entryId); const updateEntry = useUpdateEntry(); const refreshEntry = useRefreshEntry(); const deleteEntrySeen = useDeleteEntrySeen(); const deleteEntry = useDeleteEntry(); // Merge library entry with detail entry so we get both library flags AND full details (credits, etc.) const libraryEntry = useLibraryStore((s) => s.entries.get(entryId)); const displayEntry = libraryEntry && entry ? { ...libraryEntry, ...entry, isInLibrary: true } : entry || libraryEntry; const [showSettings, setShowSettings] = useState(false); const [showSeenDialog, setShowSeenDialog] = useState(false); const [showMediaDialog, setShowMediaDialog] = useState(false); const [showCollectionDialog, setShowCollectionDialog] = useState(false); const [showRatingDialog, setShowRatingDialog] = useState(false); const [showWatchHistoryDialog, setShowWatchHistoryDialog] = useState(false); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [showStatusDialog, setShowStatusDialog] = useState(false); const [showPosterDialog, setShowPosterDialog] = useState(false); const [editSeenRecord, setEditSeenRecord] = useState(null); // Track recently viewed for command palette const addRecentlyViewed = useCommandPaletteStore((s) => s.addRecentlyViewed); useEffect(() => { if (displayEntry) { addRecentlyViewed({ id: `lib-${entryId}`, title: displayEntry.title, type: displayEntry.is_tvseries ? "tv" : "movie", poster_path: displayEntry.poster_path, libraryId: entryId, tmdbId: displayEntry.entryIds?.tmdb_id ?? undefined, }); } }, [entryId, displayEntry?.title]); if (isLoading || !displayEntry) { return ; } const e = displayEntry; // Use useEntryCredits (allCredits) which includes both cast AND crew (director). // e.credits from useEntry is a limited preview (10 items). const credits = allCredits.length > 0 ? allCredits : e.credits || []; const backdrop = getBackdropUrl(e); const poster = getPosterUrl(e); const director = credits.find( (c) => c.cast_type === "crew" && c.job === "Director", ); const toggleFavorite = () => { updateEntry.mutate({ id: e.id, data: { favorit: !e.favorit } }); }; return (
{/* Backdrop banner */}
{backdrop ? ( ) : (
)}
{/* Main content */}
{/* Action buttons — top right. The row overlaps the title below, so only the button itself takes pointer events. */}
{/* Watch & Rate */} setShowSeenDialog(true)}> Mark as watched setShowRatingDialog(true)}> Rate this {e.is_tvseries && } {e.is_tvseries && ( setShowStatusDialog(true)}> Set Status )} {!e.is_tvseries && ( setShowWatchHistoryDialog(true)} > Watch History )} {/* Organize */} {e.favorit ? "Unfavorite" : "Favorite"} setShowCollectionDialog(true)}> Add to collection updateEntry.mutate({ id: e.id, data: { hidden: !e.hidden } }) } > {e.hidden ? "Unhide" : "Hide"} {/* Manage */} refreshEntry.mutate(e.id)} disabled={refreshEntry.isPending} > {refreshEntry.isPending ? "Refreshing..." : "Refresh details"} setShowMediaDialog(true)}> Manage media setShowSettings(true)}> Settings {/* Danger */} setShowDeleteConfirm(true)} > Delete entry
{/* Two-column header: sticky poster | info */}
{/* LEFT: Sticky poster */}
{/* External links */} {e.entryIds && (
{e.entryIds.tmdb_id && ( TMDB )} {e.entryIds.imdb_id && ( IMDb )}
)}
{/* RIGHT: Entry info */}
{/* Title + director */}

{e.title}

{director?.people?.name && (

by{" "} {director.people.name}

)}
{/* Meta */}
{e.release_date && ( {getYear(e.release_date)}

{formatDate(e.release_date)}

)} {e.is_tvseries && e.status && ( <> • {e.status} )} {e.runtime && !e.is_tvseries && ( <> • {formatRuntime(e.runtime)} )} {!e.is_tvseries && watchHistory.length > 0 && ( <> • {watchHistory.length === 1 ? ( Seen {formatDate(watchHistory[0].date_seen)} ) : ( Seen {watchHistory.length} times
{watchHistory.map((wh) => (

{formatDate(wh.date_seen)}

))}
)} )} {!e.is_tvseries && watchHistory.length === 0 && ( <> • )}
{/* Genres */} {e.genres && e.genres.length > 0 && (
{e.genres.map((g) => ( {g.name} ))}
)} {/* Ratings */}
{e.entryRatings?.imdb_rating != null && e.entryRatings.imdb_rating > 0 && ( IMDb: {e.entryRatings.imdb_rating}/10 )} {e.entryRatings?.tmdb_rating != null && e.entryRatings.tmdb_rating > 0 && ( TMDB: {e.entryRatings.tmdb_rating.toFixed(1)}/10 )} {e.entryRatings?.user_rating != null && e.entryRatings.user_rating > 0 ? ( ) : ( )} {/* Favorite pill */}

{e.favorit ? "Remove from favorites" : "Add to favorites"}

{/* Watch status pill — TV only */} {e.is_tvseries && e.watch_status && (() => { const info = getWatchStatusInfo(e.watch_status); return ( ); })()}
{/* Storyline */} {e.storyline && (

{e.storyline}

)}
{/* Sections */}
{/* Streaming player + Trailer */} {credits.length > 0 && ( <> )} {e.is_tvseries && seasons && seasons.length > 0 && ( <> )} {/* {watchHistory.length > 0 && ( <> deleteEntrySeen.mutate({ id: seenId, entryId: e.id }) } /> )} */}
{/* Dialogs */} {showSettings && ( )} {showSeenDialog && ( )} {showMediaDialog && ( )} {showCollectionDialog && ( )} {showRatingDialog && ( )} {showWatchHistoryDialog && ( deleteEntrySeen.mutate({ id: seenId, entryId: e.id }) } onEdit={(record) => { setEditSeenRecord(record); setShowWatchHistoryDialog(false); }} /> )} {editSeenRecord && ( { if (!open) setEditSeenRecord(null); }} entry={e} seasons={seasons} mode="edit" editRecord={editSeenRecord} /> )} { deleteEntry.mutate(e.id, { onSuccess: () => navigate("/library"), }); setShowDeleteConfirm(false); }} /> {e.title} poster {e.title} Set Watch Status
{WATCH_STATUSES.map((s) => ( ))}
); } /* ---------- Collections section ---------- */ function EntryCollectionsSection({ entryId }: { entryId: number }) { const { data: allCollections = [], isLoading } = useCollections(); const collections = allCollections.filter((col: Collection) => col.entries?.some((e: Entry) => e.id === entryId), ); if (isLoading || collections.length === 0) return null; return ( <>

In Collections

{collections.map((col: Collection) => (
{col.name}

{col.name}

{col.entries && (

{col.entries.length}{" "} {col.entries.length === 1 ? "title" : "titles"}

)}
))}
); }