import { useState } from 'react'; import { Link } from 'react-router-dom'; import { Plus, Trash2, Star, Loader2 } from 'lucide-react'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Skeleton } from '@/components/ui/skeleton'; import { getPosterUrl, getYear, formatRuntime } from '@/lib/media-utils'; import { useCreateEntry, useDeleteEntry } from '@/hooks/useEntries'; import { useLibraryStore } from '@/stores/useLibraryStore'; import { ConfirmDialog } from '@/components/dialogs/ConfirmDialog'; import type { Entry, TmdbEntry } from '@/types'; interface EntryCardProps { entry?: Entry; tmdbEntry?: TmdbEntry; showTypeBadge?: boolean; } export function EntryCard({ entry, tmdbEntry, showTypeBadge }: EntryCardProps) { const [showConfirm, setShowConfirm] = useState(false); const createEntry = useCreateEntry(); const deleteEntry = useDeleteEntry(); 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}` : undefined; const handleAdd = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); if (!tmdbEntry) return; createEntry.mutate({ tmdb_id: tmdbEntry.id, is_tvserie: isTv }); }; const handleRemove = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); setShowConfirm(true); }; const confirmRemove = () => { if (libraryEntry) { deleteEntry.mutate(libraryEntry.id, { onSettled: () => setShowConfirm(false), }); } }; const card = (
{title}