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:
@@ -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>
|
||||
Reference in New Issue
Block a user