feat(web): add /books/[slug]/chapters listing page
Some checks failed
CI / Scraper / Lint (pull_request) Failing after 8s
CI / Scraper / Test (pull_request) Successful in 9s
CI / Scraper / Docker Push (pull_request) Has been skipped
CI / UI / Build (pull_request) Successful in 22s
CI / UI / Docker Push (pull_request) Has been skipped
CI / UI / Build (push) Successful in 36s
CI / UI / Docker Push (push) Successful in 29s
iOS CI / Build (push) Successful in 2m0s
iOS CI / Build (pull_request) Successful in 3m29s
iOS CI / Test (push) Successful in 5m45s
iOS CI / Test (pull_request) Successful in 5m8s

Full chapter index with client-side search, 100-chapter page groups,
jump-to-current banner, and amber highlight on reading chapter.
This commit is contained in:
Admin
2026-03-10 22:06:33 +05:00
parent 2e5fe54615
commit 8d4bba7964
2 changed files with 235 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { getBook, listChapterIdx, getProgress } from '$lib/server/pocketbase';
import { log } from '$lib/server/logger';
export const load: PageServerLoad = async ({ params, locals }) => {
const { slug } = params;
const book = await getBook(slug).catch((e) => {
log.error('chapters', 'getBook failed', { slug, err: String(e) });
return null;
});
if (!book) error(404, `Book "${slug}" not found`);
let chapters, progress;
try {
[chapters, progress] = await Promise.all([
listChapterIdx(slug),
getProgress(locals.sessionId, slug, locals.user?.id)
]);
} catch (e) {
log.error('chapters', 'failed to load chapters', { slug, err: String(e) });
throw error(500, 'Failed to load chapters');
}
return {
book: { slug: book.slug, title: book.title, cover: book.cover ?? '', totalChapters: book.total_chapters },
chapters,
lastChapter: progress?.chapter ?? null
};
};

View File

@@ -0,0 +1,203 @@
<script lang="ts">
import type { PageData } from './$types';
import type { ChapterIdx } from '$lib/server/pocketbase';
let { data }: { data: PageData } = $props();
const PAGE_SIZE = 100;
// ── Search ──────────────────────────────────────────────────────────────────
let searchQuery = $state('');
const filtered = $derived(
(() => {
const q = searchQuery.trim().toLowerCase();
if (!q) return data.chapters;
return data.chapters.filter(
(c: ChapterIdx) =>
String(c.number).includes(q) ||
c.title.toLowerCase().includes(q)
);
})()
);
// ── Page groups (only shown when not searching) ──────────────────────────
const totalGroups = $derived(Math.ceil(data.chapters.length / PAGE_SIZE));
// Which group the current chapter is in (0-indexed)
const currentGroup = $derived(
data.lastChapter
? Math.floor(
(data.chapters.findIndex((c: ChapterIdx) => c.number === data.lastChapter)) /
PAGE_SIZE
)
: 0
);
let activeGroup = $state(0);
// On mount, jump to the group containing the current chapter
$effect(() => {
if (data.lastChapter && currentGroup >= 0) {
activeGroup = currentGroup;
}
});
const visibleChapters = $derived(
searchQuery.trim()
? filtered
: data.chapters.slice(activeGroup * PAGE_SIZE, (activeGroup + 1) * PAGE_SIZE)
);
function groupLabel(i: number): string {
const from = i * PAGE_SIZE + 1;
const to = Math.min((i + 1) * PAGE_SIZE, data.chapters.length);
return `${from}${to}`;
}
</script>
<svelte:head>
<title>{data.book.title} — Chapters — libnovel</title>
</svelte:head>
<!-- ── Back link + title ─────────────────────────────────────────────────── -->
<div class="flex items-center gap-3 mb-5">
<a
href="/books/{data.book.slug}"
class="flex items-center gap-1.5 text-zinc-400 hover:text-zinc-200 transition-colors text-sm"
>
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7"/>
</svg>
Back
</a>
<span class="text-zinc-700">/</span>
<h1 class="text-base font-semibold text-zinc-200 truncate">{data.book.title}</h1>
</div>
<!-- ── Search bar ───────────────────────────────────────────────────────── -->
<div class="relative mb-4">
<svg class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-zinc-500 pointer-events-none" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<circle cx="11" cy="11" r="8"/><path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-4.35-4.35"/>
</svg>
<input
type="search"
placeholder="Search chapters…"
bind:value={searchQuery}
class="w-full pl-9 pr-4 py-2.5 rounded-lg bg-zinc-800 border border-zinc-700 text-zinc-200 placeholder-zinc-500 text-sm focus:outline-none focus:border-amber-400 transition-colors"
/>
{#if searchQuery}
<button
onclick={() => (searchQuery = '')}
class="absolute right-3 top-1/2 -translate-y-1/2 text-zinc-500 hover:text-zinc-300"
aria-label="Clear search"
>
<svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"/>
</svg>
</button>
{/if}
</div>
<!-- ── Page-group selector (hidden while searching) ──────────────────────── -->
{#if !searchQuery && totalGroups > 1}
<div class="flex flex-wrap gap-1.5 mb-4">
{#each Array(totalGroups) as _, i}
<button
onclick={() => (activeGroup = i)}
class="px-2.5 py-1 rounded text-xs font-medium transition-colors
{activeGroup === i
? 'bg-amber-400 text-zinc-900'
: 'bg-zinc-800 text-zinc-400 hover:bg-zinc-700 hover:text-zinc-200'}
{currentGroup === i && activeGroup !== i ? 'ring-1 ring-amber-400/50' : ''}"
>
{groupLabel(i)}
</button>
{/each}
</div>
{/if}
<!-- ── Jump-to-current banner ──────────────────────────────────────────── -->
{#if data.lastChapter && data.lastChapter > 0 && !searchQuery && activeGroup !== currentGroup}
<button
onclick={() => (activeGroup = currentGroup)}
class="flex items-center gap-2 w-full px-3 py-2 mb-3 rounded-lg bg-amber-400/10 border border-amber-400/25 text-amber-400 text-sm hover:bg-amber-400/20 transition-colors"
>
<svg class="w-4 h-4 flex-shrink-0" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7"/>
</svg>
Jump to Ch.{data.lastChapter}
</button>
{/if}
<!-- ── Chapter list ───────────────────────────────────────────────────── -->
{#if visibleChapters.length === 0}
{#if searchQuery}
<p class="text-zinc-500 text-sm py-8 text-center">No chapters match "{searchQuery}"</p>
{:else}
<p class="text-zinc-500 text-sm">No chapters available yet.</p>
{/if}
{:else}
<!-- Result count while searching -->
{#if searchQuery}
<p class="text-xs text-zinc-500 mb-2">{visibleChapters.length} result{visibleChapters.length === 1 ? '' : 's'}</p>
{/if}
<div class="flex flex-col gap-0.5">
{#each visibleChapters as chapter}
{@const isCurrent = data.lastChapter === chapter.number}
<a
href="/books/{data.book.slug}/chapters/{chapter.number}"
id="ch-{chapter.number}"
class="flex items-center gap-3 px-3 py-2.5 rounded transition-colors group
{isCurrent ? 'bg-zinc-800' : 'hover:bg-zinc-800/60'}"
>
<!-- Number badge -->
<span
class="w-9 text-right text-sm font-mono flex-shrink-0
{isCurrent ? 'text-amber-400 font-semibold' : 'text-zinc-600'}"
>
{chapter.number}
</span>
<!-- Title -->
<span
class="flex-1 min-w-0 text-sm truncate transition-colors
{isCurrent ? 'text-amber-300 font-medium' : 'text-zinc-300 group-hover:text-zinc-100'}"
>
{chapter.title || `Chapter ${chapter.number}`}
</span>
<!-- Date — desktop only -->
{#if chapter.date_label}
<span class="hidden sm:block text-xs text-zinc-600 flex-shrink-0">
{chapter.date_label}
</span>
{/if}
<!-- Reading indicator -->
{#if isCurrent}
<span class="text-xs text-amber-500 font-medium flex-shrink-0">reading</span>
{/if}
</a>
{/each}
</div>
<!-- Bottom page-group nav (mirrors top, for long lists) -->
{#if !searchQuery && totalGroups > 1}
<div class="flex flex-wrap gap-1.5 mt-5 pt-4 border-t border-zinc-800">
{#each Array(totalGroups) as _, i}
<button
onclick={() => { activeGroup = i; window.scrollTo({ top: 0, behavior: 'smooth' }); }}
class="px-2.5 py-1 rounded text-xs font-medium transition-colors
{activeGroup === i
? 'bg-amber-400 text-zinc-900'
: 'bg-zinc-800 text-zinc-400 hover:bg-zinc-700 hover:text-zinc-200'}
{currentGroup === i && activeGroup !== i ? 'ring-1 ring-amber-400/50' : ''}"
>
{groupLabel(i)}
</button>
{/each}
</div>
{/if}
{/if}