feat: manga collections (0.17.0)

User-owned named lists of mangas with an add-to-collection modal on
the manga page and dedicated /collections and /collections/:id pages.

- Schema (0010): `collections` (per-user case-insensitive name
  uniqueness) + `collection_mangas` join with cascade FKs.
- Endpoints: full CRUD on `/v1/collections`, idempotent add/remove
  for `/v1/collections/:id/mangas`, and `/v1/mangas/:id/my-collections`
  for the modal's pre-checked state. Owner-mismatch surfaces as 404
  (not 403) so the API doesn't disclose collection existence to
  non-owners; the frontend funnels 401 to /login. Three-state PATCH
  via a new shared `domain::patch::Patch<T>` lets clients distinguish
  "leave alone", "clear", and "set" for description.
- Frontend: reusable `Modal` component (focus trap, opt-in
  backdrop close, ESC) and `AddToCollectionModal` with optimistic
  toggling that's race-safe under fast clicks. /collections page
  renders cover-collage cards; /collections/:id is editable with
  per-card remove. Top nav gets a Collections link.

155 backend tests (incl. 21 collection tests covering ownership,
idempotence, sample-cover enrichment, three-state PATCH, FK race);
88 frontend tests; svelte-check clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
MechaCat02
2026-05-17 17:43:06 +02:00
parent 5e92a2c450
commit 274cc819ca
24 changed files with 2689 additions and 100 deletions

View File

@@ -7,6 +7,7 @@
import { theme } from '$lib/theme.svelte';
import Upload from '@lucide/svelte/icons/upload';
import Bookmark from '@lucide/svelte/icons/bookmark';
import FolderOpen from '@lucide/svelte/icons/folder-open';
import Settings from '@lucide/svelte/icons/settings';
import LogOut from '@lucide/svelte/icons/log-out';
import '$lib/styles/tokens.css';
@@ -56,6 +57,10 @@
<Bookmark size={18} aria-hidden="true" />
<span>Bookmarks</span>
</a>
<a class="nav-link" href="/collections">
<FolderOpen size={18} aria-hidden="true" />
<span>Collections</span>
</a>
</nav>
<div class="session" data-testid="session-area">
{#if !session.loaded}

View File

@@ -0,0 +1,158 @@
<script lang="ts">
import { fileUrl } from '$lib/api/client';
import FolderOpen from '@lucide/svelte/icons/folder-open';
let { data } = $props();
const collections = $derived(data.collections);
</script>
<svelte:head>
<title>Collections — Mangalord</title>
</svelte:head>
<h1>Collections</h1>
{#if !data.authenticated}
<p class="status">
<a href="/login">Sign in</a> to see and manage your collections.
</p>
{:else if data.error}
<p class="error" role="alert">{data.error}</p>
{:else if collections.length === 0}
<p class="status" data-testid="collections-empty">
You don't have any collections yet. Open any manga and use
<strong>Add to collection</strong> to start one.
</p>
{:else}
<ul class="grid" data-testid="collections-list">
{#each collections as c (c.id)}
<li class="card">
<a href="/collections/{c.id}" class="cover-link" tabindex="-1" aria-hidden="true">
<div class="collage">
{#if c.sample_covers.length === 0}
<div class="collage-empty">
<FolderOpen size={36} aria-hidden="true" />
</div>
{:else}
{#each c.sample_covers as cover (cover)}
<img
src={fileUrl(cover)}
alt=""
class="collage-cover"
loading="lazy"
/>
{/each}
{/if}
</div>
</a>
<div class="meta">
<a href="/collections/{c.id}" class="name" data-testid={`collection-${c.id}`}>
{c.name}
</a>
<span class="count">
{c.manga_count}
{c.manga_count === 1 ? 'manga' : 'mangas'}
</span>
</div>
</li>
{/each}
</ul>
{/if}
<style>
.status {
color: var(--text-muted);
}
.error {
color: var(--danger);
}
.grid {
list-style: none;
padding: 0;
margin: 0;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: var(--space-4);
}
.card {
display: flex;
flex-direction: column;
gap: var(--space-2);
}
.cover-link {
display: block;
line-height: 0;
}
.collage {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
gap: 2px;
aspect-ratio: 2 / 3;
border-radius: var(--radius-md);
overflow: hidden;
background: var(--surface);
}
.collage-empty {
grid-column: 1 / -1;
grid-row: 1 / -1;
display: flex;
align-items: center;
justify-content: center;
color: var(--text-muted);
}
.collage-cover {
width: 100%;
height: 100%;
object-fit: cover;
}
/* If only one cover, it fills the whole card. */
.collage-cover:only-child {
grid-column: 1 / -1;
grid-row: 1 / -1;
}
/* If two covers, split vertically. */
.collage-cover:first-child:nth-last-child(2),
.collage-cover:first-child:nth-last-child(2) ~ .collage-cover {
grid-row: 1 / -1;
}
/* If three covers: the first spans the left column, the other two stack on the right. */
.collage-cover:first-child:nth-last-child(3) {
grid-row: 1 / -1;
}
.meta {
display: flex;
flex-direction: column;
min-width: 0;
gap: var(--space-1);
}
.name {
font-weight: var(--weight-semibold);
color: var(--text);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.name:hover {
color: var(--primary);
text-decoration: none;
}
.count {
color: var(--text-muted);
font-size: var(--font-xs);
}
</style>

View File

@@ -0,0 +1,20 @@
import { ApiError } from '$lib/api/client';
import { listMyCollections } from '$lib/api/collections';
import type { PageLoad } from './$types';
export const ssr = false;
export const load: PageLoad = async () => {
try {
const page = await listMyCollections({ limit: 200 });
return { collections: page.items, authenticated: true, error: null };
} catch (e) {
if (e instanceof ApiError && e.status === 401) {
return { collections: [], authenticated: false, error: null };
}
if (e instanceof ApiError) {
return { collections: [], authenticated: true, error: e.message };
}
throw e;
}
};

View File

@@ -0,0 +1,313 @@
<script lang="ts">
import { goto } from '$app/navigation';
import {
deleteCollection,
removeMangaFromCollection,
updateCollection
} from '$lib/api/collections';
import type { Manga } from '$lib/api/client';
import MangaCard from '$lib/components/MangaCard.svelte';
import ArrowLeft from '@lucide/svelte/icons/arrow-left';
import Pencil from '@lucide/svelte/icons/pencil';
import Check from '@lucide/svelte/icons/check';
import Trash2 from '@lucide/svelte/icons/trash-2';
import X from '@lucide/svelte/icons/x';
let { data } = $props();
// svelte-ignore state_referenced_locally
let collection = $state({ ...data.collection });
// svelte-ignore state_referenced_locally
let mangas = $state<Manga[]>([...data.mangas]);
let editing = $state(false);
let editName = $state('');
let editDescription = $state('');
let editError: string | null = $state(null);
let editBusy = $state(false);
function startEdit() {
editName = collection.name;
editDescription = collection.description ?? '';
editError = null;
editing = true;
}
async function saveEdit() {
if (editBusy) return;
editBusy = true;
editError = null;
try {
const updated = await updateCollection(collection.id, {
name: editName.trim(),
description: editDescription.trim() || null
});
collection = updated;
editing = false;
} catch (e) {
editError = (e as Error).message;
} finally {
editBusy = false;
}
}
async function onDeleteCollection() {
if (!confirm(`Delete collection "${collection.name}"? This cannot be undone.`)) {
return;
}
try {
await deleteCollection(collection.id);
goto('/collections');
} catch (e) {
editError = (e as Error).message;
}
}
async function onRemoveManga(m: Manga) {
const snapshot = mangas;
mangas = mangas.filter((x) => x.id !== m.id);
try {
await removeMangaFromCollection(collection.id, m.id);
} catch (e) {
mangas = snapshot;
editError = (e as Error).message;
}
}
</script>
<svelte:head>
<title>{collection.name} — Mangalord</title>
</svelte:head>
<nav class="back">
<a href="/collections" class="back-link">
<ArrowLeft size={16} aria-hidden="true" />
<span>All collections</span>
</a>
</nav>
<header class="overview">
{#if editing}
<form
class="edit-form"
onsubmit={(e) => {
e.preventDefault();
void saveEdit();
}}
action="javascript:void(0)"
>
<input
type="text"
bind:value={editName}
maxlength="64"
required
aria-label="Collection name"
data-testid="collection-edit-name"
/>
<textarea
bind:value={editDescription}
rows="2"
maxlength="1024"
placeholder="Description (optional)"
aria-label="Collection description"
data-testid="collection-edit-description"
></textarea>
<div class="edit-actions">
<button
type="submit"
class="primary"
disabled={!editName.trim() || editBusy}
data-testid="collection-edit-save"
>
<Check size={14} aria-hidden="true" />
<span>Save</span>
</button>
<button type="button" onclick={() => (editing = false)} disabled={editBusy}>
<X size={14} aria-hidden="true" />
<span>Cancel</span>
</button>
</div>
</form>
{:else}
<div class="title-row">
<h1 data-testid="collection-name">{collection.name}</h1>
<button
type="button"
class="icon-btn"
onclick={startEdit}
aria-label="Edit collection"
title="Edit"
data-testid="collection-edit-open"
>
<Pencil size={16} aria-hidden="true" />
</button>
<button
type="button"
class="icon-btn danger"
onclick={onDeleteCollection}
aria-label="Delete collection"
title="Delete"
data-testid="collection-delete"
>
<Trash2 size={16} aria-hidden="true" />
</button>
</div>
{#if collection.description}
<p class="description" data-testid="collection-description">
{collection.description}
</p>
{/if}
{/if}
{#if editError}
<p class="error" role="alert">{editError}</p>
{/if}
</header>
{#if mangas.length === 0}
<p class="status" data-testid="collection-empty">
This collection is empty.
</p>
{:else}
<ul class="manga-grid" data-testid="collection-manga-list">
{#each mangas as m (m.id)}
<li class="card-with-remove">
<MangaCard manga={m} testid={`collection-manga-${m.id}`} />
<button
type="button"
class="remove"
onclick={() => onRemoveManga(m)}
aria-label={`Remove ${m.title} from collection`}
title="Remove from collection"
data-testid={`collection-remove-manga-${m.id}`}
>
<X size={14} aria-hidden="true" />
</button>
</li>
{/each}
</ul>
{/if}
<style>
.back {
margin-bottom: var(--space-3);
}
.back-link {
display: inline-flex;
align-items: center;
gap: var(--space-1);
color: var(--text-muted);
font-size: var(--font-sm);
}
.overview {
margin-bottom: var(--space-5);
}
.title-row {
display: flex;
align-items: center;
gap: var(--space-2);
}
.title-row h1 {
margin: 0;
flex: 1;
}
.description {
color: var(--text-muted);
margin: var(--space-2) 0 0;
white-space: pre-wrap;
}
.edit-form {
display: flex;
flex-direction: column;
gap: var(--space-2);
}
.edit-actions {
display: flex;
gap: var(--space-2);
}
.primary {
background: var(--primary);
color: var(--primary-contrast);
border-color: var(--primary);
}
.primary:hover:not(:disabled) {
background: var(--primary-hover);
border-color: var(--primary-hover);
}
.error {
color: var(--danger);
margin: var(--space-2) 0 0;
}
.status {
color: var(--text-muted);
}
.manga-grid {
list-style: none;
padding: 0;
margin: 0;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
gap: var(--space-4);
}
.card-with-remove {
position: relative;
list-style: none;
}
.remove {
position: absolute;
top: var(--space-1);
right: var(--space-1);
display: inline-flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
padding: 0;
background: rgba(0, 0, 0, 0.6);
color: white;
border: 0;
border-radius: 50%;
cursor: pointer;
opacity: 0;
transition: opacity var(--transition);
}
.card-with-remove:hover .remove,
.card-with-remove:focus-within .remove {
opacity: 1;
}
.icon-btn {
display: inline-flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
padding: 0;
background: transparent;
color: var(--text-muted);
border: 1px solid transparent;
border-radius: var(--radius-sm);
}
.icon-btn:hover {
background: var(--surface-elevated);
color: var(--text);
}
.icon-btn.danger:hover {
color: var(--danger);
}
</style>

View File

@@ -0,0 +1,40 @@
import { error, redirect } from '@sveltejs/kit';
import { ApiError } from '$lib/api/client';
import {
getCollection,
listCollectionMangas
} from '$lib/api/collections';
import type { PageLoad } from './$types';
export const ssr = false;
export const load: PageLoad = async ({ params, url }) => {
try {
const [collection, mangas] = await Promise.all([
getCollection(params.id),
listCollectionMangas(params.id, { limit: 200 })
]);
return {
collection,
mangas: mangas.items,
total: mangas.page.total
};
} catch (e) {
if (e instanceof ApiError) {
// 401 means the user's session is gone — bounce to login
// and preserve where they wanted to go.
if (e.status === 401) {
const next = encodeURIComponent(url.pathname);
redirect(302, `/login?next=${next}`);
}
// 403 (post-Phase-3-polish the backend collapses this to
// 404 already, but keep the branch for defense-in-depth)
// and 404 both render the standard not-found page so the
// URL doesn't disclose collection existence to non-owners.
if (e.status === 404 || e.status === 403) {
error(404, 'Collection not found');
}
}
throw e;
}
};

View File

@@ -11,7 +11,9 @@
import { listTags, type Tag } from '$lib/api/tags';
import { session } from '$lib/session.svelte';
import Chip from '$lib/components/Chip.svelte';
import AddToCollectionModal from '$lib/components/AddToCollectionModal.svelte';
import Plus from '@lucide/svelte/icons/plus';
import FolderPlus from '@lucide/svelte/icons/folder-plus';
let { data } = $props();
const manga = $derived(data.manga);
@@ -148,6 +150,8 @@
}
const statusLabel = $derived(manga.status === 'completed' ? 'Completed' : 'Ongoing');
let collectionModalOpen = $state(false);
</script>
<svelte:head>
@@ -284,25 +288,44 @@
{/if}
{#if session.user}
<button
type="button"
class="bookmark"
class:active={mangaBookmark}
onclick={toggleBookmark}
disabled={busy}
aria-pressed={mangaBookmark ? 'true' : 'false'}
data-testid="bookmark-toggle"
>
{mangaBookmark ? '★ Bookmarked' : '☆ Bookmark'}
</button>
<div class="action-row">
<button
type="button"
class="action"
class:active={mangaBookmark}
onclick={toggleBookmark}
disabled={busy}
aria-pressed={mangaBookmark ? 'true' : 'false'}
data-testid="bookmark-toggle"
>
{mangaBookmark ? '★ Bookmarked' : '☆ Bookmark'}
</button>
<button
type="button"
class="action"
onclick={() => (collectionModalOpen = true)}
data-testid="add-to-collection-open"
>
<FolderPlus size={16} aria-hidden="true" />
<span>Add to collection</span>
</button>
</div>
{:else}
<a class="bookmark" href="/login" data-testid="bookmark-signin">
Sign in to bookmark
<a class="action" href="/login" data-testid="bookmark-signin">
Sign in to bookmark or collect
</a>
{/if}
</div>
</header>
{#if session.user}
<AddToCollectionModal
open={collectionModalOpen}
mangaId={manga.id}
onClose={() => (collectionModalOpen = false)}
/>
{/if}
<section aria-label="chapters">
<h2>Chapters</h2>
{#if chapters.length === 0}
@@ -475,11 +498,17 @@
font-size: var(--font-sm);
}
.bookmark {
.action-row {
display: flex;
gap: var(--space-2);
margin-top: var(--space-2);
flex-wrap: wrap;
}
.action {
display: inline-flex;
align-items: center;
gap: var(--space-2);
margin-top: var(--space-2);
padding: 0 var(--space-3);
height: 36px;
border: 1px solid var(--border-strong);
@@ -496,12 +525,12 @@
color var(--transition);
}
.bookmark:hover {
.action:hover {
background: var(--surface-elevated);
text-decoration: none;
}
.bookmark.active {
.action.active {
background: var(--warning-soft-bg);
border-color: var(--warning-border);
color: var(--text);