feat(mangas): per-field sort options + direction toggle on the catalog (0.88.0)

Sort the manga catalog by created / updated / title / author with an
independent asc/desc direction control. An omitted `order` defaults per field
(dates newest-first, text A→Z), matching the UI, so a bare `?sort=<field>`
means the same thing in the browser and over the API; `sort=recent` remains a
back-compat alias for `created`.

- Backend: SortField/SortOrder parsed with validation (structured 422 on bad
  input), per-field default_order, NULLS LAST only on the nullable author key,
  and migration 0033 indexing mangas(updated_at DESC, id) to back the default
  sort and its id tie-break.
- Frontend: catalog sort field + direction (SegmentedControl) on desktop and a
  mobile bottom sheet; pure helpers in $lib/mangaSort; keyboard-accessible
  direction control; visible Direction labels and a sheet "Done" button.
- Tests: backend integration coverage (defaults, alias, invalid input,
  ascending-id tie-break), frontend unit + e2e.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
MechaCat02
2026-06-25 07:15:51 +02:00
parent 93b7e451bf
commit dee53fa212
16 changed files with 936 additions and 75 deletions

View File

@@ -7,13 +7,25 @@
listMangas,
type MangaCard as MangaCardData,
type MangaSort,
type SortOrder,
type MangaStatus
} from '$lib/api/mangas';
import { listGenres, type Genre } from '$lib/api/genres';
import { listTags, type Tag } from '$lib/api/tags';
import {
DEFAULT_SORT,
SORT_FIELD_LABELS,
defaultOrderFor,
dirOptions as dirOptionsFor,
coerceSort,
coerceOrder,
sortLabel as composeSortLabel,
sortUrlParams
} from '$lib/mangaSort';
import Chip from '$lib/components/Chip.svelte';
import MangaCard from '$lib/components/MangaCard.svelte';
import Pager from '$lib/components/Pager.svelte';
import SegmentedControl from '$lib/components/SegmentedControl.svelte';
import Sheet from '$lib/components/Sheet.svelte';
import Search from '@lucide/svelte/icons/search';
import SlidersHorizontal from '@lucide/svelte/icons/sliders-horizontal';
@@ -25,7 +37,8 @@
let mangas: MangaCardData[] = $state([]);
let search = $state('');
let sort = $state<MangaSort>('recent');
let sort = $state<MangaSort>(DEFAULT_SORT);
let order = $state<SortOrder>(defaultOrderFor(DEFAULT_SORT));
let statusFilter = $state<'' | MangaStatus>('');
let selectedGenres = $state<Genre[]>([]);
let selectedTags = $state<Tag[]>([]);
@@ -55,7 +68,12 @@
(statusFilter ? 1 : 0) + selectedGenres.length + selectedTags.length
);
const sortLabel = $derived(sort === 'title' ? 'Title (A→Z)' : 'Recent');
// Field/direction labelling and defaults live in $lib/mangaSort (unit
// tested there). Date fields read newest-first; text fields A→Z. The
// direction stays freely togglable — defaultOrderFor only picks the
// direction applied when the field changes.
const dirOptions = $derived(dirOptionsFor(sort));
const sortLabel = $derived(composeSortLabel(sort, order));
const totalPages = $derived(
total != null && total > 0 ? Math.ceil(total / PAGE_SIZE) : 1
@@ -76,6 +94,7 @@
genreIds: selectedGenres.map((g) => g.id),
tagIds: selectedTags.map((t) => t.id),
sort,
order,
limit: PAGE_SIZE,
offset: (currentPage - 1) * PAGE_SIZE
});
@@ -92,7 +111,11 @@
if (!browser) return;
const params = new URLSearchParams();
if (search.trim()) params.set('q', search.trim());
if (sort !== 'recent') params.set('sort', sort);
// Defaults are omitted to keep the URL clean; the backend resolves an
// absent `sort`/`order` the same way, so the omission is unambiguous.
const sp = sortUrlParams(sort, order);
if (sp.sort) params.set('sort', sp.sort);
if (sp.order) params.set('order', sp.order);
if (statusFilter) params.set('status', statusFilter);
if (selectedGenres.length)
params.set('genres', selectedGenres.map((g) => g.id).join(','));
@@ -126,8 +149,10 @@
// Genre objects so the chip rows render real labels.
const url = new URL($page.url);
search = url.searchParams.get('q') ?? '';
const s = url.searchParams.get('sort');
if (s === 'title' || s === 'recent') sort = s;
sort = coerceSort(url.searchParams.get('sort'));
// Fall back to the field's natural default when `order` is absent or
// invalid, mirroring how syncUrl omits the default.
order = coerceOrder(url.searchParams.get('order'), sort);
const st = url.searchParams.get('status');
statusFilter = st === 'ongoing' || st === 'completed' ? st : '';
const genreIds = (url.searchParams.get('genres') ?? '')
@@ -166,6 +191,15 @@
}
function onSortChange() {
// Changing the field snaps the direction back to that field's natural
// default (dates → Newest, text → A→Z); the user can flip afterwards.
order = defaultOrderFor(sort);
resetAndReload();
}
function pickOrder(next: SortOrder) {
if (next === order) return;
order = next;
resetAndReload();
}
@@ -249,12 +283,12 @@
}
function pickSort(next: MangaSort) {
if (next === sort) {
sortSheetOpen = false;
return;
}
if (next === sort) return;
sort = next;
sortSheetOpen = false;
// Match desktop: snap to the field's natural direction. The sheet
// stays open so the user can also adjust the direction before
// dismissing it.
order = defaultOrderFor(sort);
resetAndReload();
}
@@ -504,10 +538,22 @@
<label class="sort">
<span>Sort</span>
<select bind:value={sort} onchange={onSortChange} data-testid="sort-select">
<option value="recent">Recent</option>
<option value="title">Title (A→Z)</option>
<option value="updated">Last updated</option>
<option value="created">Date added</option>
<option value="title">Title</option>
<option value="author">Author</option>
</select>
</label>
<div class="sort">
<span>Direction</span>
<SegmentedControl
options={dirOptions}
value={order}
onchange={pickOrder}
ariaLabel="Sort direction"
testid="sort-order"
/>
</div>
</div>
</form>
@@ -526,30 +572,44 @@
onClose={() => (sortSheetOpen = false)}
testid="sort-sheet"
>
<div class="sort-options">
<label>
<input
type="radio"
name="sort-mobile"
value="recent"
aria-label="Recent"
checked={sort === 'recent'}
onchange={() => pickSort('recent')}
/>
<span>Recent</span>
</label>
<label>
<input
type="radio"
name="sort-mobile"
value="title"
aria-label="Title (A→Z)"
checked={sort === 'title'}
onchange={() => pickSort('title')}
/>
<span>Title (A→Z)</span>
</label>
<p id="sort-field-heading" class="sort-section-label">Sort by</p>
<div class="sort-options" role="radiogroup" aria-labelledby="sort-field-heading">
{#each Object.entries(SORT_FIELD_LABELS) as [value, label] (value)}
<label>
<input
type="radio"
name="sort-mobile"
{value}
aria-label={label}
checked={sort === value}
onchange={() => pickSort(value as MangaSort)}
/>
<!-- aria-label duplicates the visible span text on purpose so
the e2e getByRole('radio', { name }) lookup is stable. -->
<span>{label}</span>
</label>
{/each}
</div>
<div class="sort-direction">
<p class="sort-section-label">Direction</p>
<SegmentedControl
options={dirOptions}
value={order}
onchange={pickOrder}
ariaLabel="Sort direction"
testid="sort-order-mobile"
/>
</div>
{#snippet footer()}
<button
type="button"
class="primary"
onclick={() => (sortSheetOpen = false)}
data-testid="sort-done"
>
Done
</button>
{/snippet}
</Sheet>
{#if loading}
@@ -836,6 +896,32 @@
cursor: pointer;
}
.sort-direction {
margin-top: var(--space-3);
padding-top: var(--space-3);
border-top: 1px solid var(--border);
}
/* Section headings inside the mobile sort sheet ("Sort by" / "Direction")
so each group is visibly labelled, mirroring the desktop inline labels. */
.sort-section-label {
margin: 0 0 var(--space-2);
font-size: var(--font-sm);
font-weight: var(--weight-medium);
color: var(--text-muted);
}
.primary {
border: 1px solid var(--primary);
background: var(--primary);
color: var(--primary-contrast);
}
.primary:hover {
background: var(--primary-hover);
border-color: var(--primary-hover);
}
.icon-btn {
display: inline-flex;
align-items: center;