import { useState, useMemo, useEffect } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import { useCollection, useDeleteCollection, useGetCollectionParts, useRemoveEntryFromCollection } from '@/hooks/useCollections'; import { EntryLibraryGrid } from '@/components/entries/EntryLibraryGrid'; import { getCollectionPosterUrl, getBackdropUrl } from '@/lib/media-utils'; import { getTmdbImageUrl } from '@/lib/api'; import { Skeleton } from '@/components/ui/skeleton'; import { Button } from '@/components/ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { Star, MoreVertical, Pencil, Download, Trash2, ArrowUp, ArrowDown } from 'lucide-react'; import { CollectionEditDialog } from '@/components/dialogs/CollectionEditDialog'; import { ConfirmDialog } from '@/components/dialogs/ConfirmDialog'; import { toast } from 'sonner'; import type { Entry } from '@/types'; type CollectionSortField = 'order' | 'year' | 'title' | 'rating'; type SortDir = 'asc' | 'desc'; export function CollectionDetailsPage() { const { id } = useParams<{ id: string }>(); const navigate = useNavigate(); const { data: collection, isLoading, isError } = useCollection(Number(id)); const [showEditDialog, setShowEditDialog] = useState(false); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [sortField, setSortField] = useState('order'); const [sortDir, setSortDir] = useState('asc'); const deleteCollection = useDeleteCollection(); const getCollectionParts = useGetCollectionParts(); const removeEntryFromCollection = useRemoveEntryFromCollection(); const entries = useMemo( () => (collection?.entries || []).map((e) => ({ ...e, isInLibrary: true })), [collection?.entries] ); const sortedEntries = useMemo(() => { return [...entries].sort((a, b) => { let cmp = 0; switch (sortField) { case 'order': { const oa = (a as Entry & { UserCollectionEntry?: { order: number } }).UserCollectionEntry?.order ?? 0; const ob = (b as Entry & { UserCollectionEntry?: { order: number } }).UserCollectionEntry?.order ?? 0; cmp = oa - ob; break; } case 'year': { const ya = a.release_date ? new Date(a.release_date).getFullYear() : 0; const yb = b.release_date ? new Date(b.release_date).getFullYear() : 0; cmp = ya - yb; break; } case 'title': cmp = (a.title || '').localeCompare(b.title || ''); break; case 'rating': { const ra = a.entryRatings?.user_rating || a.entryRatings?.tmdb_rating || 0; const rb = b.entryRatings?.user_rating || b.entryRatings?.tmdb_rating || 0; cmp = ra - rb; break; } } return sortDir === 'asc' ? cmp : -cmp; }); }, [entries, sortField, sortDir]); // Fallback backdrop: use first entry's backdrop if collection has none const backdropUrl = useMemo(() => { if (collection?.backdrop_path) return getTmdbImageUrl(collection.backdrop_path, 'w1280'); // Try to find a backdrop from one of the entries const entryWithBackdrop = entries.find((e) => e.banner_path); if (entryWithBackdrop?.banner_path) return getTmdbImageUrl(entryWithBackdrop.banner_path, 'w1280'); // Try entry medias for (const e of entries) { const url = getBackdropUrl(e); if (url) return url; } return null; }, [collection?.backdrop_path, entries]); const handleSort = (field: CollectionSortField) => { if (field === sortField) { setSortDir((d) => (d === 'asc' ? 'desc' : 'asc')); } else { setSortField(field); setSortDir(field === 'rating' ? 'desc' : 'asc'); } }; // Handle error redirect in useEffect (not during render) useEffect(() => { if (!isLoading && (isError || (!collection && !isLoading))) { toast.error('Collection not found'); navigate('/collections', { replace: true }); } }, [isLoading, isError, collection, navigate]); if (isLoading) { return (
{Array.from({ length: 6 }).map((_, i) => ( ))}
); } if (!collection) return null; const handleDelete = () => { deleteCollection.mutate(collection.id, { onSuccess: () => navigate('/collections', { replace: true }), }); }; const handleGetParts = () => { getCollectionParts.mutate(collection.id); }; return (
{/* Hero banner */}
{backdropUrl ? ( {collection.name} ) : (
)}
{/* Quick action menu - top right */}
setShowEditDialog(true)}> Edit {getCollectionParts.isPending ? 'Fetching...' : 'Get All Parts'} setShowDeleteConfirm(true)} > Delete
{collection.name}

{collection.name}

{collection.description && (

{collection.description}

)}

{entries.length} {entries.length === 1 ? 'entry' : 'entries'}

{collection.user_rating != null && collection.user_rating > 0 && (
{collection.user_rating}/10
)}
{/* Sort – bottom right */} {entries.length > 1 && (
{(['order', 'year', 'title', 'rating'] as const).map((field) => ( ))}
)}
{/* Entries grid */} removeEntryFromCollection.mutate({ collectionId: collection.id, entryId }) } /> {/* Edit dialog */} {showEditDialog && ( )} {/* Delete confirmation */}
); }