import { useState } from "react"; import { Link, useNavigate } from "react-router-dom"; import { Plus, Star, Loader2, Heart, EyeOff, Trash2, MoreHorizontal, X, Eye, History, FolderPlus, RefreshCw, ImageIcon, Settings, } from "lucide-react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Skeleton } from "@/components/ui/skeleton"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { getPosterUrl, getYear, formatRuntime } from "@/lib/media-utils"; import { useCreateEntry, useDeleteEntry, useUpdateEntry, useRefreshEntry, useEntrySeasons, } from "@/hooks/useEntries"; import { useEntrySeenByEntry, useDeleteEntrySeen } from "@/hooks/useEntrySeen"; import { useLibraryStore } from "@/stores/useLibraryStore"; import { ConfirmDialog } from "@/components/dialogs/ConfirmDialog"; import { EntrySeenDialog } from "@/components/dialogs/EntrySeenDialog"; import { RatingDialog } from "@/components/dialogs/RatingDialog"; import { MovieWatchHistoryDialog } from "@/components/dialogs/MovieWatchHistoryDialog"; import { AddEntryToCollectionDialog } from "@/components/dialogs/AddEntryToCollectionDialog"; import { EntryMediaDialog } from "@/components/dialogs/EntryMediaDialog"; import { EntrySettingsDialog } from "@/components/dialogs/EntrySettingsDialog"; import type { Entry, TmdbEntry } from "@/types"; interface EntryCardProps { entry?: Entry; tmdbEntry?: TmdbEntry; showTypeBadge?: boolean; /** When inside a collection, provide the collection ID to show "Remove from collection" action */ collectionId?: number; /** Callback to remove entry from collection */ onRemoveFromCollection?: (entryId: number) => void; } export function EntryCard({ entry, tmdbEntry, showTypeBadge, collectionId, onRemoveFromCollection, }: EntryCardProps) { const [showConfirm, setShowConfirm] = useState(false); const [showRemoveCollectionConfirm, setShowRemoveCollectionConfirm] = useState(false); const [showSeenDialog, setShowSeenDialog] = useState(false); const [showRatingDialog, setShowRatingDialog] = useState(false); const [showWatchHistoryDialog, setShowWatchHistoryDialog] = useState(false); const [showCollectionDialog, setShowCollectionDialog] = useState(false); const [showMediaDialog, setShowMediaDialog] = useState(false); const [showSettingsDialog, setShowSettingsDialog] = useState(false); const [dropdownOpen, setDropdownOpen] = useState(false); const navigate = useNavigate(); const createEntry = useCreateEntry(); const deleteEntry = useDeleteEntry(); const updateEntry = useUpdateEntry(); const refreshEntry = useRefreshEntry(); const libraryEntry = useLibraryStore((s) => { if (entry?.isInLibrary) return entry; if (tmdbEntry) { for (const e of s.entries.values()) { if (e.entryIds?.tmdb_id === tmdbEntry.id) return e; } } return null; }); const isInLibrary = !!libraryEntry; const displayEntry = libraryEntry || entry; // Resolve display data const title = displayEntry?.title || tmdbEntry?.title || tmdbEntry?.name || "Untitled"; const year = displayEntry ? getYear(displayEntry.release_date) : getYear(tmdbEntry?.release_date || tmdbEntry?.first_air_date || null); const rating = displayEntry?.entryRatings?.user_rating || displayEntry?.entryRatings?.tmdb_rating || tmdbEntry?.vote_average; const runtime = displayEntry && !displayEntry.is_tvseries ? formatRuntime(displayEntry.runtime) : ""; const isTv = displayEntry?.is_tvseries || tmdbEntry?.media_type === "tv" || !!tmdbEntry?.name; const posterUrl = displayEntry ? getPosterUrl(displayEntry) : tmdbEntry?.poster_path ? `https://image.tmdb.org/t/p/w342${tmdbEntry.poster_path}` : "/images/default-poster.png"; const entryLink = displayEntry ? `/entry/${displayEntry.id}` : tmdbEntry ? `/entry/tmdb/${isTv ? "tv" : "movie"}/${tmdbEntry.id}` : undefined; const handleAdd = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); if (!tmdbEntry) return; createEntry.mutate( { tmdb_id: tmdbEntry.id, is_tvserie: isTv }, { onSuccess: (newEntry) => navigate(`/entry/${newEntry.id}`) }, ); }; const confirmRemove = () => { if (libraryEntry) { deleteEntry.mutate(libraryEntry.id, { onSettled: () => setShowConfirm(false), }); } }; const toggleFavorite = () => { if (libraryEntry) { updateEntry.mutate({ id: libraryEntry.id, data: { favorit: !libraryEntry.favorit }, }); } }; const toggleHidden = () => { if (libraryEntry) { updateEntry.mutate({ id: libraryEntry.id, data: { hidden: !libraryEntry.hidden }, }); } }; const card = (
{/* Poster */}
{title} {/* Library badge – only on home / search (tmdbEntry context) */} {isInLibrary && tmdbEntry && ( In Library )} {/* Favorite indicator – hidden on hover when actions button shows */} {libraryEntry?.favorit && ( )} {/* Quick actions button – top right, shown on hover */} {isInLibrary && (
e.stopPropagation()} > {/* Watch & Rate */} { e.stopPropagation(); setShowSeenDialog(true); }} > Mark as watched { e.stopPropagation(); setShowRatingDialog(true); }} > Rate this {!isTv && ( { e.stopPropagation(); setShowWatchHistoryDialog(true); }} > Watch History )} {/* Organize */} { e.stopPropagation(); toggleFavorite(); }} > {libraryEntry?.favorit ? "Unfavorite" : "Favorite"} { e.stopPropagation(); setShowCollectionDialog(true); }} > Add to collection { e.stopPropagation(); toggleHidden(); }} > {libraryEntry?.hidden ? "Unhide" : "Hide"} {/* Manage */} { e.stopPropagation(); refreshEntry.mutate(libraryEntry!.id); }} disabled={refreshEntry.isPending} > {refreshEntry.isPending ? "Refreshing..." : "Refresh details"} { e.stopPropagation(); setShowMediaDialog(true); }} > Manage media { e.stopPropagation(); setShowSettingsDialog(true); }} > Settings {/* Collection removal */} {collectionId && onRemoveFromCollection && ( <> { e.stopPropagation(); setShowRemoveCollectionConfirm(true); }} className="text-destructive focus:text-destructive" > Remove from collection )} {/* Danger */} { e.stopPropagation(); setShowConfirm(true); }} className="text-destructive focus:text-destructive" > Delete entry
)} {/* Entry type pill */} {showTypeBadge && tmdbEntry && ( {isTv ? "TV Series" : "Movie"} )} {/* Hover overlay */}

{title}

{rating != null && rating > 0 && ( {typeof rating === "number" ? rating.toFixed(1) : rating} )} {year && {year}} {runtime && {runtime}}
{/* Add to Library – only for non-library entries */} {!isInLibrary && tmdbEntry && (
)}
); return ( <> {entryLink ? {card} : card} {collectionId && onRemoveFromCollection && ( { if (libraryEntry) onRemoveFromCollection(libraryEntry.id); setShowRemoveCollectionConfirm(false); }} variant="destructive" /> )} {libraryEntry && showSeenDialog && ( )} {libraryEntry && showRatingDialog && ( )} {libraryEntry && showWatchHistoryDialog && ( )} {libraryEntry && showCollectionDialog && ( )} {libraryEntry && showMediaDialog && ( )} {libraryEntry && showSettingsDialog && ( )} ); } /* Lazy wrappers – only mount when dialog is open so hooks fetch on demand */ function LazyEntrySeenDialog({ entry, open, onOpenChange, }: { entry: Entry; open: boolean; onOpenChange: (open: boolean) => void; }) { const { data: seasons = [] } = useEntrySeasons(entry.id); return ( ); } function LazyMovieWatchHistoryDialog({ entryId, open, onOpenChange, }: { entryId: number; open: boolean; onOpenChange: (open: boolean) => void; }) { const { data: watchHistory = [] } = useEntrySeenByEntry(entryId); const deleteEntrySeen = useDeleteEntrySeen(); return ( deleteEntrySeen.mutate({ id: seenId, entryId })} /> ); } export function EntryCardSkeleton() { return (
); }