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 = (
{title}