refactor(web): Library › Albums and › Artists are one A–Z page (DRY W8)

The two pages matched line for line apart from the noun, the filter field
and the sort key: the count, the quick filter, the lettered grid paged as
you scroll, the admin and listener empty states, and the "search the full
library" link when the filter matches nothing loaded. AlphaLibrary now
draws them once and each page supplies its query, noun, match, key and
card.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 03:23:54 -05:00
co-authored by Claude Opus 5.5
parent abf30b1295
commit 47423fb529
3 changed files with 160 additions and 183 deletions
+131
View File
@@ -0,0 +1,131 @@
<script lang="ts" generics="T">
import type { Snippet } from 'svelte';
import { plural } from '#lib/utils/plural.js';
import AlphabeticalGrid from './AlphabeticalGrid.svelte';
import ApiErrorBanner from './ApiErrorBanner.svelte';
import ListContinuation from './ListContinuation.svelte';
import QuickFilter from './QuickFilter.svelte';
import EmptyState from './EmptyState.svelte';
import { user } from '#lib/auth/user.svelte.js';
import { useDelayed } from '#lib/utils/useDelayed.svelte.js';
import type { Page } from '#lib/api/types.js';
// A whole-library A–Z page (Library › Albums, Library › Artists): the
// count, a filter over what has loaded, the lettered grid paged as you
// scroll, and the empty states. A filter that matches nothing loaded links
// to the full search, since the rest of the library may not have loaded.
type Pages = {
data?: { pages: Page<T>[] };
isPending: boolean;
isError: boolean;
error: unknown;
hasNextPage: boolean;
isFetchingNextPage: boolean;
isFetchNextPageError: boolean;
fetchNextPage: () => unknown;
refetch: () => unknown;
};
let {
query,
title,
one,
many,
matches,
getKey,
searchPath,
item
}: {
query: Pages;
title: string;
/** The noun for one and for many: 'album', 'albums'. */
one: string;
many: string;
/** Whether an item matches the filter text (already lower-cased). */
matches: (it: T, q: string) => boolean;
getKey: (it: T) => string;
/** The full-library search for this noun, e.g. '/search/albums'. */
searchPath: string;
item: Snippet<[T]>;
} = $props();
const items = $derived(query.data?.pages?.flatMap((p) => p.items) ?? []);
const total = $derived(query.data?.pages?.[0]?.total ?? 0);
const showSkeleton = useDelayed(() => query.isPending);
// A failed first page replaces the list; a failed later page keeps it.
const failed = $derived(query.isError && !query.isFetchNextPageError);
let filter = $state('');
const filtered = $derived.by(() => {
const q = filter.trim().toLowerCase();
return q ? items.filter((it) => matches(it, q)) : items;
});
</script>
<div class="space-y-4">
<header class="flex flex-wrap items-end justify-between gap-3">
<div>
<h1 class="font-display text-2xl font-medium text-text-primary">{title}</h1>
{#if !query.isPending && !failed}
<p class="text-sm text-text-secondary">{plural(total, one, many)}</p>
{/if}
</div>
{#if items.length > 0}
<QuickFilter bind:value={filter} placeholder={`Filter ${many}`} />
{/if}
</header>
{#if failed}
<ApiErrorBanner error={query.error} onRetry={query.refetch} />
{:else if showSkeleton.value && items.length === 0}
<p class="text-text-secondary">Loading…</p>
{:else if !query.isPending && total === 0}
<!-- Library folders are server config (library.scan_paths), not a
setting, so only an admin can act: they run a scan from Admin.
Everyone else is told whose job it is. -->
{#if user.value?.is_admin}
<EmptyState
title={`No ${many} yet`}
hint={`Library folders are set in the server's config. Once they're scanned, ${many} appear here.`}
>
{#snippet actions()}
<a
href="/admin"
class="inline-flex items-center rounded-md bg-action-secondary px-4 py-2 text-sm text-action-fg hover:opacity-90"
>
Run a scan
</a>
{/snippet}
</EmptyState>
{:else}
<EmptyState
title={`No ${many} yet`}
hint={`Once your server's admin has scanned the library, ${many} appear here.`}
/>
{/if}
{:else if filter.trim() && filtered.length === 0}
<p class="text-text-secondary">
No {many} in your loaded library match
<span class="font-medium">'{filter.trim()}'</span>.
<a href={`${searchPath}?q=${encodeURIComponent(filter.trim())}`} class="text-accent-fg hover:underline">
Search the full library →
</a>
</p>
{:else}
<AlphabeticalGrid
items={filtered}
{getKey}
hasMore={query.hasNextPage}
onLoadMore={() => query.fetchNextPage()}
{item}
/>
<ListContinuation
hasMore={query.hasNextPage}
loading={query.isFetchingNextPage}
failed={query.isFetchNextPageError}
onMore={() => query.fetchNextPage()}
endLabel="End of library"
/>
{/if}
</div>
+14 -92
View File
@@ -1,104 +1,26 @@
<script lang="ts">
import { plural } from '#lib/utils/plural.js';
import { pageTitle } from '#lib/branding.js';
import { createAlbumsAlphaInfiniteQuery } from '#lib/api/albums.js';
import AlbumCard from '#lib/components/AlbumCard.svelte';
import AlphabeticalGrid from '#lib/components/AlphabeticalGrid.svelte';
import ApiErrorBanner from '#lib/components/ApiErrorBanner.svelte';
import ListContinuation from '#lib/components/ListContinuation.svelte';
import QuickFilter from '#lib/components/QuickFilter.svelte';
import EmptyState from '#lib/components/EmptyState.svelte';
import { user } from '#lib/auth/user.svelte.js';
import { useDelayed } from '#lib/utils/useDelayed.svelte.js';
import AlphaLibrary from '#lib/components/AlphaLibrary.svelte';
import type { AlbumRef } from '#lib/api/types.js';
const queryStore = createAlbumsAlphaInfiniteQuery();
const query = $derived($queryStore);
const albums = $derived(query.data?.pages?.flatMap((p) => p.items) ?? []);
const total = $derived(query.data?.pages?.[0]?.total ?? 0);
const showSkeleton = useDelayed(() => query.isPending);
let filter = $state('');
const filtered = $derived.by(() => {
const q = filter.trim().toLowerCase();
if (!q) return albums;
return albums.filter(
(a) => a.title.toLowerCase().includes(q) || a.artist_name.toLowerCase().includes(q)
);
});
</script>
<svelte:head><title>{pageTitle('Library · Albums')}</title></svelte:head>
<div class="space-y-4">
<header class="flex flex-wrap items-end justify-between gap-3">
<div>
<h1 class="font-display text-2xl font-medium text-text-primary">Albums</h1>
{#if !query.isPending && !(query.isError && !query.isFetchNextPageError)}
<p class="text-sm text-text-secondary">
{plural(total, 'album', 'albums')}
</p>
{/if}
</div>
{#if albums.length > 0}
<QuickFilter bind:value={filter} placeholder="Filter albums" />
{/if}
</header>
{#if query.isError && !query.isFetchNextPageError}
<ApiErrorBanner error={query.error} onRetry={query.refetch} />
{:else if showSkeleton.value && albums.length === 0}
<p class="text-text-secondary">Loading…</p>
{:else if !query.isPending && total === 0}
<!-- Library folders are server config (library.scan_paths), not a
setting, so only an admin can act: they run a scan from Admin.
Everyone else is told whose job it is. -->
{#if user.value?.is_admin}
<EmptyState
title="No albums yet"
hint="Library folders are set in the server's config. Once they're scanned, albums appear here."
>
{#snippet actions()}
<a
href="/admin"
class="inline-flex items-center rounded-md bg-action-secondary px-4 py-2 text-sm text-action-fg hover:opacity-90"
>
Run a scan
</a>
{/snippet}
</EmptyState>
{:else}
<EmptyState
title="No albums yet"
hint="Once your server's admin has scanned the library, albums appear here."
/>
{/if}
{:else if filter.trim() && filtered.length === 0}
<p class="text-text-secondary">
No albums in your loaded library match
<span class="font-medium">'{filter.trim()}'</span>.
<a href={`/search/albums?q=${encodeURIComponent(filter.trim())}`} class="text-accent-fg hover:underline">
Search the full library →
</a>
</p>
{:else}
<AlphabeticalGrid
items={filtered}
getKey={(a: AlbumRef) => a.sort_title || a.title}
hasMore={!!query.hasNextPage}
onLoadMore={() => query.fetchNextPage()}
>
{#snippet item(album: AlbumRef)}
<AlbumCard {album} />
{/snippet}
</AlphabeticalGrid>
<ListContinuation
hasMore={!!query.hasNextPage}
loading={query.isFetchingNextPage}
failed={query.isFetchNextPageError}
onMore={() => query.fetchNextPage()}
endLabel="End of library"
/>
{/if}
</div>
<AlphaLibrary
{query}
title="Albums"
one="album"
many="albums"
matches={(a: AlbumRef, q) => a.title.toLowerCase().includes(q) || a.artist_name.toLowerCase().includes(q)}
getKey={(a: AlbumRef) => a.sort_title || a.title}
searchPath="/search/albums"
>
{#snippet item(album: AlbumRef)}
<AlbumCard {album} />
{/snippet}
</AlphaLibrary>
+15 -91
View File
@@ -1,102 +1,26 @@
<script lang="ts">
import { plural } from '#lib/utils/plural.js';
import { pageTitle } from '#lib/branding.js';
import { createArtistsQuery } from '#lib/api/queries.js';
import ArtistCard from '#lib/components/ArtistCard.svelte';
import AlphabeticalGrid from '#lib/components/AlphabeticalGrid.svelte';
import ApiErrorBanner from '#lib/components/ApiErrorBanner.svelte';
import ListContinuation from '#lib/components/ListContinuation.svelte';
import QuickFilter from '#lib/components/QuickFilter.svelte';
import EmptyState from '#lib/components/EmptyState.svelte';
import { user } from '#lib/auth/user.svelte.js';
import { useDelayed } from '#lib/utils/useDelayed.svelte.js';
import AlphaLibrary from '#lib/components/AlphaLibrary.svelte';
import type { ArtistRef } from '#lib/api/types.js';
const queryStore = $derived(createArtistsQuery('alpha'));
const queryStore = createArtistsQuery('alpha');
const query = $derived($queryStore);
const artists = $derived(query.data?.pages?.flatMap((p) => p.items) ?? []);
const total = $derived(query.data?.pages?.[0]?.total ?? 0);
const showSkeleton = useDelayed(() => query.isPending);
let filter = $state('');
const filtered = $derived.by(() => {
const q = filter.trim().toLowerCase();
if (!q) return artists;
return artists.filter((a) => a.name.toLowerCase().includes(q));
});
</script>
<svelte:head><title>{pageTitle('Library · Artists')}</title></svelte:head>
<div class="space-y-4">
<header class="flex flex-wrap items-end justify-between gap-3">
<div>
<h1 class="font-display text-2xl font-medium text-text-primary">Artists</h1>
{#if !query.isPending && !(query.isError && !query.isFetchNextPageError)}
<p class="text-sm text-text-secondary">
{plural(total, 'artist', 'artists')}
</p>
{/if}
</div>
{#if artists.length > 0}
<QuickFilter bind:value={filter} placeholder="Filter artists" />
{/if}
</header>
{#if query.isError && !query.isFetchNextPageError}
<ApiErrorBanner error={query.error} onRetry={query.refetch} />
{:else if showSkeleton.value && artists.length === 0}
<p class="text-text-secondary">Loading…</p>
{:else if !query.isPending && total === 0}
<!-- Library folders are server config (library.scan_paths), not a
setting, so only an admin can act: they run a scan from Admin.
Everyone else is told whose job it is. -->
{#if user.value?.is_admin}
<EmptyState
title="No artists yet"
hint="Library folders are set in the server's config. Once they're scanned, artists appear here."
>
{#snippet actions()}
<a
href="/admin"
class="inline-flex items-center rounded-md bg-action-secondary px-4 py-2 text-sm text-action-fg hover:opacity-90"
>
Run a scan
</a>
{/snippet}
</EmptyState>
{:else}
<EmptyState
title="No artists yet"
hint="Once your server's admin has scanned the library, artists appear here."
/>
{/if}
{:else if filter.trim() && filtered.length === 0}
<p class="text-text-secondary">
No artists in your loaded library match
<span class="font-medium">'{filter.trim()}'</span>.
<a href={`/search/artists?q=${encodeURIComponent(filter.trim())}`} class="text-accent-fg hover:underline">
Search the full library →
</a>
</p>
{:else}
<AlphabeticalGrid
items={filtered}
getKey={(a: ArtistRef) => a.sort_name || a.name}
hasMore={!!query.hasNextPage}
onLoadMore={() => query.fetchNextPage()}
>
{#snippet item(a: ArtistRef)}
<ArtistCard artist={a} />
{/snippet}
</AlphabeticalGrid>
<ListContinuation
hasMore={!!query.hasNextPage}
loading={query.isFetchingNextPage}
failed={query.isFetchNextPageError}
onMore={() => query.fetchNextPage()}
endLabel="End of library"
/>
{/if}
</div>
<AlphaLibrary
{query}
title="Artists"
one="artist"
many="artists"
matches={(a: ArtistRef, q) => a.name.toLowerCase().includes(q)}
getKey={(a: ArtistRef) => a.sort_name || a.name}
searchPath="/search/artists"
>
{#snippet item(a: ArtistRef)}
<ArtistCard artist={a} />
{/snippet}
</AlphaLibrary>