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 { useState, useMemo, useEffect } from "react";
|
||||||
import { useParams, useNavigate } from 'react-router-dom';
|
import { useParams, useNavigate } from "react-router-dom";
|
||||||
import { useCollection, useDeleteCollection, useGetCollectionParts, useRemoveEntryFromCollection } from '@/hooks/useCollections';
|
import {
|
||||||
import { EntryLibraryGrid } from '@/components/entries/EntryLibraryGrid';
|
useCollection,
|
||||||
import { getCollectionPosterUrl, getBackdropUrl } from '@/lib/media-utils';
|
useDeleteCollection,
|
||||||
import { getTmdbImageUrl } from '@/lib/api';
|
useGetCollectionParts,
|
||||||
import { Skeleton } from '@/components/ui/skeleton';
|
useRemoveEntryFromCollection,
|
||||||
import { Button } from '@/components/ui/button';
|
} 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 {
|
import {
|
||||||
DropdownMenu,
|
DropdownMenu,
|
||||||
DropdownMenuContent,
|
DropdownMenuContent,
|
||||||
DropdownMenuItem,
|
DropdownMenuItem,
|
||||||
DropdownMenuTrigger,
|
DropdownMenuTrigger,
|
||||||
} from '@/components/ui/dropdown-menu';
|
} from "@/components/ui/dropdown-menu";
|
||||||
import { Star, MoreVertical, Pencil, Download, Trash2, ArrowUp, ArrowDown } from 'lucide-react';
|
import {
|
||||||
import { CollectionEditDialog } from '@/components/dialogs/CollectionEditDialog';
|
Star,
|
||||||
import { ConfirmDialog } from '@/components/dialogs/ConfirmDialog';
|
MoreVertical,
|
||||||
import { toast } from 'sonner';
|
Pencil,
|
||||||
import type { Entry } from '@/types';
|
Download,
|
||||||
|
Trash2,
|
||||||
type CollectionSortField = 'order' | 'year' | 'title' | 'rating';
|
ArrowUp,
|
||||||
type SortDir = 'asc' | 'desc';
|
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() {
|
export function CollectionDetailsPage() {
|
||||||
const { id } = useParams<{ id: string }>();
|
const { id } = useParams<{ id: string }>();
|
||||||
@@ -27,53 +42,69 @@ export function CollectionDetailsPage() {
|
|||||||
const { data: collection, isLoading, isError } = useCollection(Number(id));
|
const { data: collection, isLoading, isError } = useCollection(Number(id));
|
||||||
const [showEditDialog, setShowEditDialog] = useState(false);
|
const [showEditDialog, setShowEditDialog] = useState(false);
|
||||||
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
|
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
|
||||||
const [sortField, setSortField] = useState<CollectionSortField>('order');
|
const collectionSortPrefs = useCollectionSortStore((s) => s.prefs);
|
||||||
const [sortDir, setSortDir] = useState<SortDir>('asc');
|
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 deleteCollection = useDeleteCollection();
|
||||||
const getCollectionParts = useGetCollectionParts();
|
const getCollectionParts = useGetCollectionParts();
|
||||||
const removeEntryFromCollection = useRemoveEntryFromCollection();
|
const removeEntryFromCollection = useRemoveEntryFromCollection();
|
||||||
|
|
||||||
const entries = useMemo(
|
const entries = useMemo(
|
||||||
() => (collection?.entries || []).map((e) => ({ ...e, isInLibrary: true })),
|
() => (collection?.entries || []).map((e) => ({ ...e, isInLibrary: true })),
|
||||||
[collection?.entries]
|
[collection?.entries],
|
||||||
);
|
);
|
||||||
|
|
||||||
const sortedEntries = useMemo(() => {
|
const sortedEntries = useMemo(() => {
|
||||||
return [...entries].sort((a, b) => {
|
return [...entries].sort((a, b) => {
|
||||||
let cmp = 0;
|
let cmp = 0;
|
||||||
switch (sortField) {
|
switch (sortField) {
|
||||||
case 'order': {
|
case "order": {
|
||||||
const oa = (a as Entry & { UserCollectionEntry?: { order: number } }).UserCollectionEntry?.order ?? 0;
|
const oa =
|
||||||
const ob = (b as Entry & { UserCollectionEntry?: { order: number } }).UserCollectionEntry?.order ?? 0;
|
(a as Entry & { UserCollectionEntry?: { order: number } })
|
||||||
|
.UserCollectionEntry?.order ?? 0;
|
||||||
|
const ob =
|
||||||
|
(b as Entry & { UserCollectionEntry?: { order: number } })
|
||||||
|
.UserCollectionEntry?.order ?? 0;
|
||||||
cmp = oa - ob;
|
cmp = oa - ob;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'year': {
|
case "year": {
|
||||||
const ya = a.release_date ? new Date(a.release_date).getFullYear() : 0;
|
const ya = a.release_date
|
||||||
const yb = b.release_date ? new Date(b.release_date).getFullYear() : 0;
|
? new Date(a.release_date).getFullYear()
|
||||||
|
: 0;
|
||||||
|
const yb = b.release_date
|
||||||
|
? new Date(b.release_date).getFullYear()
|
||||||
|
: 0;
|
||||||
cmp = ya - yb;
|
cmp = ya - yb;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'title':
|
case "title":
|
||||||
cmp = (a.title || '').localeCompare(b.title || '');
|
cmp = (a.title || "").localeCompare(b.title || "");
|
||||||
break;
|
break;
|
||||||
case 'rating': {
|
case "rating": {
|
||||||
const ra = a.entryRatings?.user_rating || a.entryRatings?.tmdb_rating || 0;
|
const ra =
|
||||||
const rb = b.entryRatings?.user_rating || b.entryRatings?.tmdb_rating || 0;
|
a.entryRatings?.user_rating || a.entryRatings?.tmdb_rating || 0;
|
||||||
|
const rb =
|
||||||
|
b.entryRatings?.user_rating || b.entryRatings?.tmdb_rating || 0;
|
||||||
cmp = ra - rb;
|
cmp = ra - rb;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sortDir === 'asc' ? cmp : -cmp;
|
return sortDir === "asc" ? cmp : -cmp;
|
||||||
});
|
});
|
||||||
}, [entries, sortField, sortDir]);
|
}, [entries, sortField, sortDir]);
|
||||||
|
|
||||||
// Fallback backdrop: use first entry's backdrop if collection has none
|
// Fallback backdrop: use first entry's backdrop if collection has none
|
||||||
const backdropUrl = useMemo(() => {
|
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
|
// Try to find a backdrop from one of the entries
|
||||||
const entryWithBackdrop = entries.find((e) => e.banner_path);
|
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
|
// Try entry medias
|
||||||
for (const e of entries) {
|
for (const e of entries) {
|
||||||
const url = getBackdropUrl(e);
|
const url = getBackdropUrl(e);
|
||||||
@@ -84,18 +115,17 @@ export function CollectionDetailsPage() {
|
|||||||
|
|
||||||
const handleSort = (field: CollectionSortField) => {
|
const handleSort = (field: CollectionSortField) => {
|
||||||
if (field === sortField) {
|
if (field === sortField) {
|
||||||
setSortDir((d) => (d === 'asc' ? 'desc' : 'asc'));
|
setSortPref(collectionId, field, sortDir === "asc" ? "desc" : "asc");
|
||||||
} else {
|
} else {
|
||||||
setSortField(field);
|
setSortPref(collectionId, field, field === "rating" ? "desc" : "asc");
|
||||||
setSortDir(field === 'rating' ? 'desc' : 'asc');
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle error redirect in useEffect (not during render)
|
// Handle error redirect in useEffect (not during render)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isLoading && (isError || (!collection && !isLoading))) {
|
if (!isLoading && (isError || (!collection && !isLoading))) {
|
||||||
toast.error('Collection not found');
|
toast.error("Collection not found");
|
||||||
navigate('/collections', { replace: true });
|
navigate("/collections", { replace: true });
|
||||||
}
|
}
|
||||||
}, [isLoading, isError, collection, navigate]);
|
}, [isLoading, isError, collection, navigate]);
|
||||||
|
|
||||||
@@ -117,7 +147,7 @@ export function CollectionDetailsPage() {
|
|||||||
|
|
||||||
const handleDelete = () => {
|
const handleDelete = () => {
|
||||||
deleteCollection.mutate(collection.id, {
|
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" />
|
<Pencil className="mr-2 h-4 w-4" />
|
||||||
Edit
|
Edit
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
<DropdownMenuItem onClick={handleGetParts} disabled={getCollectionParts.isPending}>
|
<DropdownMenuItem
|
||||||
|
onClick={handleGetParts}
|
||||||
|
disabled={getCollectionParts.isPending}
|
||||||
|
>
|
||||||
<Download className="mr-2 h-4 w-4" />
|
<Download className="mr-2 h-4 w-4" />
|
||||||
{getCollectionParts.isPending ? 'Fetching...' : 'Get All Parts'}
|
{getCollectionParts.isPending ? "Fetching..." : "Get All Parts"}
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
className="text-destructive focus:text-destructive"
|
className="text-destructive focus:text-destructive"
|
||||||
@@ -186,16 +219,17 @@ export function CollectionDetailsPage() {
|
|||||||
)}
|
)}
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
<p className="text-sm text-white/60">
|
<p className="text-sm text-white/60">
|
||||||
{entries.length} {entries.length === 1 ? 'entry' : 'entries'}
|
{entries.length} {entries.length === 1 ? "entry" : "entries"}
|
||||||
</p>
|
</p>
|
||||||
{collection.user_rating != null && collection.user_rating > 0 && (
|
{collection.user_rating != null &&
|
||||||
<div className="flex items-center gap-1">
|
collection.user_rating > 0 && (
|
||||||
<Star className="h-4 w-4 fill-yellow-400 text-yellow-400" />
|
<div className="flex items-center gap-1">
|
||||||
<span className="text-sm font-medium text-white">
|
<Star className="h-4 w-4 fill-yellow-400 text-yellow-400" />
|
||||||
{collection.user_rating}/10
|
<span className="text-sm font-medium text-white">
|
||||||
</span>
|
{collection.user_rating}/10
|
||||||
</div>
|
</span>
|
||||||
)}
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -203,20 +237,21 @@ export function CollectionDetailsPage() {
|
|||||||
{/* Sort – bottom right */}
|
{/* Sort – bottom right */}
|
||||||
{entries.length > 1 && (
|
{entries.length > 1 && (
|
||||||
<div className="ml-auto flex items-center gap-1 self-end">
|
<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
|
<Button
|
||||||
key={field}
|
key={field}
|
||||||
variant={sortField === field ? 'secondary' : 'ghost'}
|
variant={sortField === field ? "secondary" : "ghost"}
|
||||||
size="sm"
|
size="sm"
|
||||||
className="h-7 text-xs capitalize gap-1"
|
className="h-7 text-xs capitalize gap-1"
|
||||||
onClick={() => handleSort(field)}
|
onClick={() => handleSort(field)}
|
||||||
>
|
>
|
||||||
{field}
|
{field}
|
||||||
{sortField === field && (
|
{sortField === field &&
|
||||||
sortDir === 'asc'
|
(sortDir === "asc" ? (
|
||||||
? <ArrowUp className="h-3 w-3" />
|
<ArrowUp className="h-3 w-3" />
|
||||||
: <ArrowDown className="h-3 w-3" />
|
) : (
|
||||||
)}
|
<ArrowDown className="h-3 w-3" />
|
||||||
|
))}
|
||||||
</Button>
|
</Button>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
@@ -230,7 +265,10 @@ export function CollectionDetailsPage() {
|
|||||||
isLoading={false}
|
isLoading={false}
|
||||||
collectionId={collection.id}
|
collectionId={collection.id}
|
||||||
onRemoveFromCollection={(entryId) =>
|
onRemoveFromCollection={(entryId) =>
|
||||||
removeEntryFromCollection.mutate({ collectionId: collection.id, entryId })
|
removeEntryFromCollection.mutate({
|
||||||
|
collectionId: collection.id,
|
||||||
|
entryId,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|||||||
@@ -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<number, CollectionSortPref>;
|
||||||
|
setSort: (collectionId: number, field: CollectionSortField, dir: SortDir) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useCollectionSortStore = create<CollectionSortState>()(
|
||||||
|
persist(
|
||||||
|
(set) => ({
|
||||||
|
prefs: {},
|
||||||
|
setSort: (collectionId, field, dir) =>
|
||||||
|
set((state) => ({
|
||||||
|
prefs: {
|
||||||
|
...state.prefs,
|
||||||
|
[collectionId]: { field, dir },
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
name: 'showy-collection-sort',
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
Reference in new issue
Block a user