Persist collection sort order across page reloads
Add useCollectionSortStore with localStorage persistence, keyed by collection ID. CollectionDetailsPage now reads/writes sort preferences through the store instead of local useState.
This commit is contained in:
1 parent
702cd537d1
commit
e434dd3b5e
2 files changed
+131
-59
No files matched your search
@@ -1,25 +1,40 @@
|
||||
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 { 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';
|
||||
} 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";
|
||||
import {
|
||||
useCollectionSortStore,
|
||||
type CollectionSortField,
|
||||
type SortDir,
|
||||
} from "@/stores/useCollectionSortStore";
|
||||
|
||||
export function CollectionDetailsPage() {
|
||||
const { id } = useParams<{ id: string }>();
|
||||
@@ -27,53 +42,69 @@ export function CollectionDetailsPage() {
|
||||
const { data: collection, isLoading, isError } = useCollection(Number(id));
|
||||
const [showEditDialog, setShowEditDialog] = useState(false);
|
||||
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
|
||||
const [sortField, setSortField] = useState<CollectionSortField>('order');
|
||||
const [sortDir, setSortDir] = useState<SortDir>('asc');
|
||||
const collectionSortPrefs = useCollectionSortStore((s) => s.prefs);
|
||||
const setSortPref = useCollectionSortStore((s) => s.setSort);
|
||||
const collectionId = Number(id);
|
||||
const sortPref = collectionSortPrefs[collectionId];
|
||||
const sortField: CollectionSortField = sortPref?.field ?? "order";
|
||||
const sortDir: SortDir = sortPref?.dir ?? "asc";
|
||||
const deleteCollection = useDeleteCollection();
|
||||
const getCollectionParts = useGetCollectionParts();
|
||||
const removeEntryFromCollection = useRemoveEntryFromCollection();
|
||||
|
||||
const entries = useMemo(
|
||||
() => (collection?.entries || []).map((e) => ({ ...e, isInLibrary: true })),
|
||||
[collection?.entries]
|
||||
[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;
|
||||
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;
|
||||
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 || '');
|
||||
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;
|
||||
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;
|
||||
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');
|
||||
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');
|
||||
if (entryWithBackdrop?.banner_path)
|
||||
return getTmdbImageUrl(entryWithBackdrop.banner_path, "w1280");
|
||||
// Try entry medias
|
||||
for (const e of entries) {
|
||||
const url = getBackdropUrl(e);
|
||||
@@ -84,18 +115,17 @@ export function CollectionDetailsPage() {
|
||||
|
||||
const handleSort = (field: CollectionSortField) => {
|
||||
if (field === sortField) {
|
||||
setSortDir((d) => (d === 'asc' ? 'desc' : 'asc'));
|
||||
setSortPref(collectionId, field, sortDir === "asc" ? "desc" : "asc");
|
||||
} else {
|
||||
setSortField(field);
|
||||
setSortDir(field === 'rating' ? 'desc' : 'asc');
|
||||
setSortPref(collectionId, field, 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 });
|
||||
toast.error("Collection not found");
|
||||
navigate("/collections", { replace: true });
|
||||
}
|
||||
}, [isLoading, isError, collection, navigate]);
|
||||
|
||||
@@ -117,7 +147,7 @@ export function CollectionDetailsPage() {
|
||||
|
||||
const handleDelete = () => {
|
||||
deleteCollection.mutate(collection.id, {
|
||||
onSuccess: () => navigate('/collections', { replace: true }),
|
||||
onSuccess: () => navigate("/collections", { replace: true }),
|
||||
});
|
||||
};
|
||||
|
||||
@@ -153,9 +183,12 @@ export function CollectionDetailsPage() {
|
||||
<Pencil className="mr-2 h-4 w-4" />
|
||||
Edit
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={handleGetParts} disabled={getCollectionParts.isPending}>
|
||||
<DropdownMenuItem
|
||||
onClick={handleGetParts}
|
||||
disabled={getCollectionParts.isPending}
|
||||
>
|
||||
<Download className="mr-2 h-4 w-4" />
|
||||
{getCollectionParts.isPending ? 'Fetching...' : 'Get All Parts'}
|
||||
{getCollectionParts.isPending ? "Fetching..." : "Get All Parts"}
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem
|
||||
className="text-destructive focus:text-destructive"
|
||||
@@ -186,16 +219,17 @@ export function CollectionDetailsPage() {
|
||||
)}
|
||||
<div className="flex items-center gap-3">
|
||||
<p className="text-sm text-white/60">
|
||||
{entries.length} {entries.length === 1 ? 'entry' : 'entries'}
|
||||
{entries.length} {entries.length === 1 ? "entry" : "entries"}
|
||||
</p>
|
||||
{collection.user_rating != null && collection.user_rating > 0 && (
|
||||
<div className="flex items-center gap-1">
|
||||
<Star className="h-4 w-4 fill-yellow-400 text-yellow-400" />
|
||||
<span className="text-sm font-medium text-white">
|
||||
{collection.user_rating}/10
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
{collection.user_rating != null &&
|
||||
collection.user_rating > 0 && (
|
||||
<div className="flex items-center gap-1">
|
||||
<Star className="h-4 w-4 fill-yellow-400 text-yellow-400" />
|
||||
<span className="text-sm font-medium text-white">
|
||||
{collection.user_rating}/10
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -203,20 +237,21 @@ export function CollectionDetailsPage() {
|
||||
{/* Sort – bottom right */}
|
||||
{entries.length > 1 && (
|
||||
<div className="ml-auto flex items-center gap-1 self-end">
|
||||
{(['order', 'year', 'title', 'rating'] as const).map((field) => (
|
||||
{(["order", "year", "title", "rating"] as const).map((field) => (
|
||||
<Button
|
||||
key={field}
|
||||
variant={sortField === field ? 'secondary' : 'ghost'}
|
||||
variant={sortField === field ? "secondary" : "ghost"}
|
||||
size="sm"
|
||||
className="h-7 text-xs capitalize gap-1"
|
||||
onClick={() => handleSort(field)}
|
||||
>
|
||||
{field}
|
||||
{sortField === field && (
|
||||
sortDir === 'asc'
|
||||
? <ArrowUp className="h-3 w-3" />
|
||||
: <ArrowDown className="h-3 w-3" />
|
||||
)}
|
||||
{sortField === field &&
|
||||
(sortDir === "asc" ? (
|
||||
<ArrowUp className="h-3 w-3" />
|
||||
) : (
|
||||
<ArrowDown className="h-3 w-3" />
|
||||
))}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
@@ -230,7 +265,10 @@ export function CollectionDetailsPage() {
|
||||
isLoading={false}
|
||||
collectionId={collection.id}
|
||||
onRemoveFromCollection={(entryId) =>
|
||||
removeEntryFromCollection.mutate({ collectionId: collection.id, entryId })
|
||||
removeEntryFromCollection.mutate({
|
||||
collectionId: collection.id,
|
||||
entryId,
|
||||
})
|
||||
}
|
||||
/>
|
||||
|
||||
|
||||
Reference in new issue
Block a user