Files
Showy/react_frontend/src/components/entries/EntryCard.tsx
T
Mohammed AbadandCopilot a42d4ee7d7 feat(frontend): fix hooks crash, persist library filters, improve UI
- Fix "rendered more hooks" crash in CollectionDetailsPage by moving all hooks before conditionals
- Persist library search/filters only on back navigation using useNavigationType
- Add resetAll() to useLibraryFiltersStore
- Move sort toolbar to hero bottom-right in CollectionDetailsPage
- Fall back to entry backdrops when collection has none
- Show full cast in TmdbPreviewPage matching EntryDetailsPage layout
- Remove tagline/icons from TmdbPreviewPage to match in-library style
- Lazy load people in local DB mode with infinite scroll
- Move All/Favorite filter to right side in PeoplePage, always visible
- Track dropdownOpen state in EntryCard to fix menu close flash

Co-authored-by: Copilot <copilot@github.com>
2026-04-27 11:49:00 +01:00

507 lines
17 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 = (
<div className="group relative overflow-hidden rounded-lg transition-all duration-300 hover:scale-[1.02] hover:shadow-xl animate-in fade-in zoom-in-95">
{/* Poster */}
<div className="relative aspect-2/3 w-full overflow-hidden rounded-lg bg-muted">
<img
src={posterUrl}
alt={title}
className="h-full w-full object-cover transition-transform duration-300 group-hover:scale-105"
loading="lazy"
/>
{/* Library badge – only on home / search (tmdbEntry context) */}
{isInLibrary && tmdbEntry && (
<Badge className="absolute left-2 top-2 bg-primary text-primary-foreground text-[10px] px-1.5 py-0.5">
In Library
</Badge>
)}
{/* Favorite indicator – hidden on hover when actions button shows */}
{libraryEntry?.favorit && (
<Star className="absolute right-2 top-2 h-4 w-4 fill-yellow-400 text-yellow-400 transition-opacity group-hover:opacity-0" />
)}
{/* Quick actions button – top right, shown on hover */}
{isInLibrary && (
<div className={`absolute right-2 top-2 z-20 transition-opacity ${dropdownOpen ? 'opacity-100' : 'opacity-0 group-hover:opacity-100'}`}>
<DropdownMenu open={dropdownOpen} onOpenChange={setDropdownOpen}>
<DropdownMenuTrigger asChild>
<Button
variant="secondary"
size="icon"
className="h-7 w-7"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
}}
>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent
align="end"
className="w-52"
onClick={(e) => e.stopPropagation()}
>
{/* Watch & Rate */}
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
setShowSeenDialog(true);
}}
>
<Eye className="mr-2 h-4 w-4" />
Mark as watched
</DropdownMenuItem>
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
setShowRatingDialog(true);
}}
>
<Star className="mr-2 h-4 w-4" />
Rate this
</DropdownMenuItem>
{!isTv && (
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
setShowWatchHistoryDialog(true);
}}
>
<History className="mr-2 h-4 w-4" />
Watch History
</DropdownMenuItem>
)}
<DropdownMenuSeparator />
{/* Organize */}
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
toggleFavorite();
}}
>
<Heart
className={`mr-2 h-4 w-4 ${libraryEntry?.favorit ? "fill-current" : ""}`}
/>
{libraryEntry?.favorit ? "Unfavorite" : "Favorite"}
</DropdownMenuItem>
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
setShowCollectionDialog(true);
}}
>
<FolderPlus className="mr-2 h-4 w-4" />
Add to collection
</DropdownMenuItem>
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
toggleHidden();
}}
>
<EyeOff className="mr-2 h-4 w-4" />
{libraryEntry?.hidden ? "Unhide" : "Hide"}
</DropdownMenuItem>
<DropdownMenuSeparator />
{/* Manage */}
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
refreshEntry.mutate(libraryEntry!.id);
}}
disabled={refreshEntry.isPending}
>
<RefreshCw
className={`mr-2 h-4 w-4 ${refreshEntry.isPending ? "animate-spin" : ""}`}
/>
{refreshEntry.isPending ? "Refreshing..." : "Refresh details"}
</DropdownMenuItem>
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
setShowMediaDialog(true);
}}
>
<ImageIcon className="mr-2 h-4 w-4" />
Manage media
</DropdownMenuItem>
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
setShowSettingsDialog(true);
}}
>
<Settings className="mr-2 h-4 w-4" />
Settings
</DropdownMenuItem>
{/* Collection removal */}
{collectionId && onRemoveFromCollection && (
<>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
setShowRemoveCollectionConfirm(true);
}}
className="text-destructive focus:text-destructive"
>
<X className="mr-2 h-4 w-4" />
Remove from collection
</DropdownMenuItem>
</>
)}
<DropdownMenuSeparator />
{/* Danger */}
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
setShowConfirm(true);
}}
className="text-destructive focus:text-destructive"
>
<Trash2 className="mr-2 h-4 w-4" />
Delete entry
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
)}
{/* Entry type pill */}
{showTypeBadge && tmdbEntry && (
<Badge
variant="secondary"
className="absolute bottom-2 right-2 text-[10px] px-1.5 py-0.5 opacity-90 z-10 pointer-events-none"
>
{isTv ? "TV Series" : "Movie"}
</Badge>
)}
{/* Hover overlay */}
<div className="absolute inset-0 flex flex-col justify-end bg-linear-to-t from-black/90 via-black/40 to-transparent opacity-0 transition-opacity duration-200 group-hover:opacity-100">
<div className="p-3 space-y-1">
<p className="text-sm font-semibold text-white line-clamp-2">
{title}
</p>
<div className="flex items-center gap-2 text-xs text-white/70">
{rating != null && rating > 0 && (
<span className="flex items-center gap-1">
<Star className="h-3 w-3 fill-yellow-400 text-yellow-400" />
{typeof rating === "number" ? rating.toFixed(1) : rating}
</span>
)}
{year && <span>{year}</span>}
{runtime && <span>{runtime}</span>}
</div>
{/* Add to Library – only for non-library entries */}
{!isInLibrary && tmdbEntry && (
<div className="pt-1">
<Button
size="sm"
className="h-7 w-full text-xs"
onClick={handleAdd}
disabled={createEntry.isPending}
>
{createEntry.isPending ? (
<Loader2 className="mr-1 h-3 w-3 animate-spin" />
) : (
<Plus className="mr-1 h-3 w-3" />
)}
{createEntry.isPending ? "Adding..." : "Add to Library"}
</Button>
</div>
)}
</div>
</div>
</div>
</div>
);
return (
<>
{entryLink ? <Link to={entryLink}>{card}</Link> : card}
<ConfirmDialog
open={showConfirm}
onOpenChange={setShowConfirm}
title="Delete Entry"
description={`Are you sure you want to delete "${title}" from your library? This action cannot be undone.`}
confirmLabel="Delete"
onConfirm={confirmRemove}
variant="destructive"
/>
{collectionId && onRemoveFromCollection && (
<ConfirmDialog
open={showRemoveCollectionConfirm}
onOpenChange={setShowRemoveCollectionConfirm}
title="Remove from Collection"
description={`Remove "${title}" from this collection?`}
confirmLabel="Remove"
onConfirm={() => {
if (libraryEntry) onRemoveFromCollection(libraryEntry.id);
setShowRemoveCollectionConfirm(false);
}}
variant="destructive"
/>
)}
{libraryEntry && showSeenDialog && (
<LazyEntrySeenDialog
entry={libraryEntry}
open={showSeenDialog}
onOpenChange={setShowSeenDialog}
/>
)}
{libraryEntry && showRatingDialog && (
<RatingDialog
open={showRatingDialog}
onOpenChange={setShowRatingDialog}
entry={libraryEntry}
/>
)}
{libraryEntry && showWatchHistoryDialog && (
<LazyMovieWatchHistoryDialog
entryId={libraryEntry.id}
open={showWatchHistoryDialog}
onOpenChange={setShowWatchHistoryDialog}
/>
)}
{libraryEntry && showCollectionDialog && (
<AddEntryToCollectionDialog
open={showCollectionDialog}
onOpenChange={setShowCollectionDialog}
entryId={libraryEntry.id}
/>
)}
{libraryEntry && showMediaDialog && (
<EntryMediaDialog
open={showMediaDialog}
onOpenChange={setShowMediaDialog}
entryId={libraryEntry.id}
medias={libraryEntry.medias || []}
/>
)}
{libraryEntry && showSettingsDialog && (
<EntrySettingsDialog
open={showSettingsDialog}
onOpenChange={setShowSettingsDialog}
entry={libraryEntry}
/>
)}
</>
);
}
/* 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 (
<EntrySeenDialog
open={open}
onOpenChange={onOpenChange}
entry={entry}
seasons={seasons}
mode="add"
/>
);
}
function LazyMovieWatchHistoryDialog({
entryId,
open,
onOpenChange,
}: {
entryId: number;
open: boolean;
onOpenChange: (open: boolean) => void;
}) {
const { data: watchHistory = [] } = useEntrySeenByEntry(entryId);
const deleteEntrySeen = useDeleteEntrySeen();
return (
<MovieWatchHistoryDialog
open={open}
onOpenChange={onOpenChange}
entryId={entryId}
watchHistory={watchHistory}
onDelete={(seenId) => deleteEntrySeen.mutate({ id: seenId, entryId })}
/>
);
}
export function EntryCardSkeleton() {
return (
<div className="space-y-2">
<Skeleton className="aspect-2/3 w-full rounded-lg" />
</div>
);
}