From e434dd3b5e35f6e983b90512be75d17e086123d3 Mon Sep 17 00:00:00 2001 From: X4Lo Date: Wed, 17 Jun 2026 03:57:43 +0100 Subject: [PATCH] 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. --- .../src/pages/CollectionDetailsPage.tsx | 156 +++++++++++------- .../src/stores/useCollectionSortStore.ts | 34 ++++ 2 files changed, 131 insertions(+), 59 deletions(-) create mode 100644 react_frontend/src/stores/useCollectionSortStore.ts diff --git a/react_frontend/src/pages/CollectionDetailsPage.tsx b/react_frontend/src/pages/CollectionDetailsPage.tsx index 96a71c8..81d506d 100644 --- a/react_frontend/src/pages/CollectionDetailsPage.tsx +++ b/react_frontend/src/pages/CollectionDetailsPage.tsx @@ -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('order'); - const [sortDir, setSortDir] = useState('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() { Edit - + - {getCollectionParts.isPending ? 'Fetching...' : 'Get All Parts'} + {getCollectionParts.isPending ? "Fetching..." : "Get All Parts"}

- {entries.length} {entries.length === 1 ? 'entry' : 'entries'} + {entries.length} {entries.length === 1 ? "entry" : "entries"}

- {collection.user_rating != null && collection.user_rating > 0 && ( -
- - - {collection.user_rating}/10 - -
- )} + {collection.user_rating != null && + collection.user_rating > 0 && ( +
+ + + {collection.user_rating}/10 + +
+ )} @@ -203,20 +237,21 @@ export function CollectionDetailsPage() { {/* Sort – bottom right */} {entries.length > 1 && (
- {(['order', 'year', 'title', 'rating'] as const).map((field) => ( + {(["order", "year", "title", "rating"] as const).map((field) => ( ))}
@@ -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, + }) } /> diff --git a/react_frontend/src/stores/useCollectionSortStore.ts b/react_frontend/src/stores/useCollectionSortStore.ts new file mode 100644 index 0000000..7996340 --- /dev/null +++ b/react_frontend/src/stores/useCollectionSortStore.ts @@ -0,0 +1,34 @@ +import { create } from 'zustand'; +import { persist } from 'zustand/middleware'; + +export type CollectionSortField = 'order' | 'year' | 'title' | 'rating'; +export type SortDir = 'asc' | 'desc'; + +interface CollectionSortPref { + field: CollectionSortField; + dir: SortDir; +} + +interface CollectionSortState { + /** Map of collection id -> sort preference */ + prefs: Record; + setSort: (collectionId: number, field: CollectionSortField, dir: SortDir) => void; +} + +export const useCollectionSortStore = create()( + persist( + (set) => ({ + prefs: {}, + setSort: (collectionId, field, dir) => + set((state) => ({ + prefs: { + ...state.prefs, + [collectionId]: { field, dir }, + }, + })), + }), + { + name: 'showy-collection-sort', + } + ) +);