feat(artists): add virtualization with grouping, alpha index, and list view

This commit is contained in:
vicrodh committed 2026-01-23 01:00:58 -06:00
1 parent 496255744a
commit a3ecbe6617
2 files changed
+465 -119

No files matched your search

+224 -78
View File
@@ -2,33 +2,46 @@
import { onMount, onDestroy } from 'svelte';
import { Mic2, Upload } from 'lucide-svelte';
interface LocalArtist {
name: string;
album_count: number;
track_count: number;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Artist = any;
interface ArtistGroup {
key: string;
id: string;
artists: Artist[];
}
type VirtualItem =
| { type: 'header'; key: string; id: string; height: number }
| { type: 'row'; artists: Artist[]; height: number };
interface Props {
artists: LocalArtist[];
groups: ArtistGroup[];
artistImages: Map<string, string>;
showSettings: boolean;
showGroupHeaders: boolean;
onArtistClick: (name: string) => void;
onUploadImage: (name: string, e: MouseEvent) => void;
scrollToGroupId?: string;
}
let {
artists,
groups,
artistImages,
showSettings,
showGroupHeaders,
onArtistClick,
onUploadImage,
scrollToGroupId,
}: Props = $props();
// Constants
const CARD_MIN_WIDTH = 160;
const CARD_HEIGHT = 180; // Approximate height of artist card
const GAP = 24;
const BUFFER_ROWS = 2;
const CARD_WIDTH = 160;
const CARD_HEIGHT = 200; // Fixed card height
const GAP = 24; // Horizontal gap between cards
const ROW_GAP = 24; // Vertical gap between rows
const HEADER_HEIGHT = 44;
const BUFFER_ITEMS = 5;
// State
let containerEl: HTMLDivElement | null = $state(null);
@@ -39,48 +52,116 @@
// Computed: number of columns
let columns = $derived.by(() => {
if (containerWidth === 0) return 1;
return Math.max(1, Math.floor((containerWidth + GAP) / (CARD_MIN_WIDTH + GAP)));
return Math.max(1, Math.floor((containerWidth + GAP) / (CARD_WIDTH + GAP)));
});
// Computed: number of rows
let totalRows = $derived(Math.ceil(artists.length / columns));
// Computed: flatten groups into virtual items
let virtualItems = $derived.by(() => {
const items: (VirtualItem & { top: number; groupId?: string })[] = [];
let currentTop = 0;
const rowHeight = CARD_HEIGHT + ROW_GAP;
// Computed: total height
let totalHeight = $derived(totalRows * (CARD_HEIGHT + GAP) - GAP);
// Computed: visible row range
let visibleRowRange = $derived.by(() => {
const rowHeight = CARD_HEIGHT + GAP;
const firstVisibleRow = Math.floor(scrollTop / rowHeight);
const visibleRowCount = Math.ceil(containerHeight / rowHeight) + 1;
const startRow = Math.max(0, firstVisibleRow - BUFFER_ROWS);
const endRow = Math.min(totalRows - 1, firstVisibleRow + visibleRowCount + BUFFER_ROWS);
return { startRow, endRow };
});
// Computed: visible artists with positions
let visibleArtists = $derived.by(() => {
const { startRow, endRow } = visibleRowRange;
const result: { artist: LocalArtist; top: number; left: number }[] = [];
const rowHeight = CARD_HEIGHT + GAP;
const cardWidth = (containerWidth - GAP * (columns - 1)) / columns;
for (let row = startRow; row <= endRow; row++) {
for (let col = 0; col < columns; col++) {
const idx = row * columns + col;
if (idx >= artists.length) break;
result.push({
artist: artists[idx],
top: row * rowHeight,
left: col * (cardWidth + GAP),
for (const group of groups) {
// Add header if showing
if (showGroupHeaders && group.key) {
items.push({
type: 'header',
key: group.key,
id: group.id,
height: HEADER_HEIGHT,
top: currentTop,
groupId: group.id,
});
currentTop += HEADER_HEIGHT;
}
// Group artists into rows
const cols = columns;
for (let i = 0; i < group.artists.length; i += cols) {
const rowArtists = group.artists.slice(i, i + cols);
items.push({
type: 'row',
artists: rowArtists,
height: CARD_HEIGHT,
top: currentTop,
});
currentTop += rowHeight;
}
}
return items;
});
// Computed: total height
let totalHeight = $derived(
virtualItems.length > 0
? virtualItems[virtualItems.length - 1].top + virtualItems[virtualItems.length - 1].height
: 0
);
// Binary search for first visible item
function binarySearchStart(items: typeof virtualItems, targetTop: number): number {
let low = 0;
let high = items.length - 1;
let result = 0;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
const item = items[mid];
if (item.top + item.height > targetTop) {
result = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return result;
}
// Binary search for last visible item
function binarySearchEnd(items: typeof virtualItems, targetBottom: number, startFrom: number): number {
let low = startFrom;
let high = items.length - 1;
let result = high;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
const item = items[mid];
if (item.top > targetBottom) {
result = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return result;
}
// Computed: visible items
let visibleItems = $derived.by(() => {
if (virtualItems.length === 0) return [];
const viewportTop = scrollTop;
const viewportBottom = scrollTop + containerHeight;
const firstVisible = binarySearchStart(virtualItems, viewportTop);
const lastVisible = binarySearchEnd(virtualItems, viewportBottom, firstVisible);
const startIdx = Math.max(0, firstVisible - BUFFER_ITEMS);
const endIdx = Math.min(virtualItems.length - 1, lastVisible + BUFFER_ITEMS);
return virtualItems.slice(startIdx, endIdx + 1);
});
// Group positions for scroll-to
let groupPositions = $derived.by(() => {
const map = new Map<string, number>();
for (const item of virtualItems) {
if (item.groupId) {
map.set(item.groupId, item.top);
}
}
return map;
});
function handleScroll(e: Event) {
@@ -107,40 +188,76 @@
onDestroy(() => {
resizeObserver?.disconnect();
});
// Scroll to group when requested
$effect(() => {
if (scrollToGroupId && containerEl) {
const position = groupPositions.get(scrollToGroupId);
if (position !== undefined) {
containerEl.scrollTo({ top: position, behavior: 'smooth' });
}
}
});
export function scrollToGroup(groupId: string) {
const position = groupPositions.get(groupId);
if (position !== undefined && containerEl) {
containerEl.scrollTo({ top: position, behavior: 'smooth' });
}
}
function getItemKey(item: typeof virtualItems[0]): string {
if (item.type === 'header') return `header-${item.id}`;
return `row-${item.artists[0]?.name ?? item.top}`;
}
</script>
<div class="virtual-container" bind:this={containerEl} onscroll={handleScroll}>
<div class="virtual-content" style="height: {totalHeight}px;">
{#each visibleArtists as { artist, top, left } (artist.name)}
{@const artistImage = artistImages.get(artist.name)}
{#each visibleItems as item (getItemKey(item))}
<div
class="artist-card"
style="transform: translate({left}px, {top}px);"
role="button"
tabindex="0"
onclick={() => onArtistClick(artist.name)}
onkeydown={(e) => e.key === 'Enter' && onArtistClick(artist.name)}
class="virtual-item"
style="transform: translateY({item.top}px); height: {item.height}px;"
>
<div class="artist-icon" class:has-image={!!artistImage}>
{#if artistImage}
<img src={artistImage} alt={artist.name} class="artist-image" loading="lazy" />
{:else}
<Mic2 size={32} />
{/if}
</div>
{#if showSettings}
<button
class="artist-image-btn"
onclick={(e) => onUploadImage(artist.name, e)}
title="Upload custom image"
>
<Upload size={14} />
</button>
{#if item.type === 'header'}
<div class="group-header">
<span class="group-title">{item.key}</span>
</div>
{:else if item.type === 'row'}
<div class="artist-row">
{#each item.artists as artist (artist.name)}
{@const artistImage = artistImages.get(artist.name)}
<div
class="artist-card"
role="button"
tabindex="0"
onclick={() => onArtistClick(artist.name)}
onkeydown={(e) => e.key === 'Enter' && onArtistClick(artist.name)}
>
<div class="artist-icon" class:has-image={!!artistImage}>
{#if artistImage}
<img src={artistImage} alt={artist.name} class="artist-image" loading="lazy" />
{:else}
<Mic2 size={32} />
{/if}
</div>
{#if showSettings}
<button
class="artist-image-btn"
onclick={(e) => { e.stopPropagation(); onUploadImage(artist.name, e); }}
title="Upload custom image"
>
<Upload size={14} />
</button>
{/if}
<div class="artist-name">{artist.name}</div>
<div class="artist-stats">
{artist.album_count} albums &bull; {artist.track_count} tracks
</div>
</div>
{/each}
</div>
{/if}
<div class="artist-name">{artist.name}</div>
<div class="artist-stats">
{artist.album_count} albums &bull; {artist.track_count} tracks
</div>
</div>
{/each}
</div>
@@ -159,11 +276,38 @@
width: 100%;
}
.artist-card {
.virtual-item {
position: absolute;
width: calc((100% - 24px * 5) / 6);
min-width: 160px;
max-width: 200px;
left: 0;
right: 0;
will-change: transform;
}
.group-header {
display: flex;
align-items: center;
gap: 8px;
padding: 12px 0;
}
.group-title {
font-size: 14px;
font-weight: 700;
color: var(--text-primary);
letter-spacing: 0.04em;
text-transform: uppercase;
}
.artist-row {
display: flex;
gap: 24px;
padding: 0;
}
.artist-card {
width: 160px;
height: 200px;
flex-shrink: 0;
display: flex;
flex-direction: column;
align-items: center;
@@ -172,8 +316,9 @@
background: var(--bg-secondary);
border-radius: 12px;
cursor: pointer;
transition: background 150ms ease, transform 150ms ease;
will-change: transform;
transition: background 150ms ease;
position: relative;
box-sizing: border-box;
}
.artist-card:hover {
@@ -183,6 +328,7 @@
.artist-icon {
width: 80px;
height: 80px;
flex-shrink: 0;
border-radius: 50%;
display: flex;
align-items: center;
@@ -232,7 +378,7 @@
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 100%;
width: 100%;
}
.artist-stats {
+241 -41
View File
@@ -176,7 +176,11 @@
// Performance mode state
let useVirtualization = $state(isVirtualizationEnabled());
let virtualizedScrollTarget = $state<string | undefined>(undefined);
// Artist view state
let artistSearch = $state('');
let artistViewMode = $state<'grid' | 'list'>('grid');
let artistGroupingEnabled = $state(true); // Enable alpha grouping by default
let trackSearch = $state('');
let searchOpen = $state(false);
let searchInputEl: HTMLInputElement | undefined;
@@ -227,6 +231,48 @@
return artists.filter(artist => artist.name.toLowerCase().includes(needle));
});
// Memoized grouped artists with alpha index
let groupedArtistsMemo = $derived.by(() => {
const filtered = filteredArtistsMemo;
if (!artistGroupingEnabled) {
return {
grouped: [{ key: '', id: 'ungrouped', artists: filtered }],
alphaGroups: new Set<string>()
};
}
// Group by first letter
const groups = new Map<string, LocalArtist[]>();
for (const artist of filtered) {
const key = alphaGroupKey(artist.name);
let group = groups.get(key);
if (!group) {
group = [];
groups.set(key, group);
}
group.push(artist);
}
// Sort keys (# at end)
const keys = [...groups.keys()].sort((a, b) => {
if (a === '#') return 1;
if (b === '#') return -1;
return a.localeCompare(b);
});
const grouped = keys.map(key => ({
key,
id: `artist-alpha-${key}`,
artists: groups.get(key) ?? []
}));
return {
grouped,
alphaGroups: new Set(keys)
};
});
// Memoized filtered and grouped albums
let filteredAndGroupedAlbums = $derived.by(() => {
// Filter albums
@@ -2456,8 +2502,27 @@
<p>No artists in library</p>
</div>
{:else}
{@const { grouped: groupedArtists, alphaGroups: artistAlphaGroups } = groupedArtistsMemo}
{@const filteredArtists = filteredArtistsMemo}
<div class="artist-controls">
<div class="view-toggle">
<button
class="view-btn"
class:active={artistViewMode === 'grid'}
onclick={() => artistViewMode = 'grid'}
title="Grid view"
>
<LayoutGrid size={18} />
</button>
<button
class="view-btn"
class:active={artistViewMode === 'list'}
onclick={() => artistViewMode = 'list'}
title="List view"
>
<List size={18} />
</button>
</div>
<span class="album-count">{filteredArtists.length} artists</span>
</div>
@@ -2467,49 +2532,107 @@
<p>No artists match your search</p>
</div>
{:else}
{#if useVirtualization}
<VirtualizedArtistGrid
artists={filteredArtists}
{artistImages}
{showSettings}
onArtistClick={handleLocalArtistClick}
onUploadImage={handleUploadArtistImage}
/>
{:else}
<div class="artist-grid">
{#each filteredArtists as artist (artist.name)}
{@const artistImage = artistImages.get(artist.name)}
<div
class="artist-card"
role="button"
tabindex="0"
onclick={() => handleLocalArtistClick(artist.name)}
onkeydown={(event) => event.key === 'Enter' && handleLocalArtistClick(artist.name)}
>
<div class="artist-icon" class:has-image={!!artistImage}>
{#if artistImage}
<img src={artistImage} alt={artist.name} class="artist-image" loading="lazy" />
{:else}
<Mic2 size={32} />
{/if}
</div>
{#if showSettings}
<button
class="artist-image-btn"
onclick={(e) => handleUploadArtistImage(artist.name, e)}
title="Upload custom image"
>
<Upload size={14} />
</button>
{/if}
<div class="artist-name">{artist.name}</div>
<div class="artist-stats">
{artist.album_count} albums &bull; {artist.track_count} tracks
<div class="artist-sections" class:virtualized={useVirtualization}>
{#if useVirtualization}
{#if artistViewMode === 'grid'}
<div class="virtualized-container">
<VirtualizedArtistGrid
groups={groupedArtists}
{artistImages}
{showSettings}
showGroupHeaders={artistGroupingEnabled}
onArtistClick={handleLocalArtistClick}
onUploadImage={handleUploadArtistImage}
/>
</div>
{:else}
<!-- Artist list view -->
<div class="virtualized-container">
<div class="artist-list-virtualized">
{#each groupedArtists as group (group.id)}
{#if artistGroupingEnabled && group.key}
<div class="group-header">
<span class="group-title">{group.key}</span>
</div>
{/if}
{#each group.artists as artist (artist.name)}
{@const artistImage = artistImages.get(artist.name)}
<div
class="artist-list-row"
role="button"
tabindex="0"
onclick={() => handleLocalArtistClick(artist.name)}
onkeydown={(e) => e.key === 'Enter' && handleLocalArtistClick(artist.name)}
>
<div class="artist-list-icon" class:has-image={!!artistImage}>
{#if artistImage}
<img src={artistImage} alt={artist.name} loading="lazy" />
{:else}
<Mic2 size={20} />
{/if}
</div>
<div class="artist-list-info">
<div class="artist-list-name">{artist.name}</div>
<div class="artist-list-stats">
{artist.album_count} albums &bull; {artist.track_count} tracks
</div>
</div>
</div>
{/each}
{/each}
</div>
</div>
{/each}
</div>
{/if}
{/if}
{#if artistGroupingEnabled}
<div class="alpha-index">
{#each alphaIndexLetters as letter}
<button
class="alpha-letter"
class:disabled={!artistAlphaGroups.has(letter)}
onclick={() => scrollToGroup('artist-alpha', letter, artistAlphaGroups)}
>
{letter}
</button>
{/each}
</div>
{/if}
{:else}
<div class="artist-grid">
{#each filteredArtists as artist (artist.name)}
{@const artistImage = artistImages.get(artist.name)}
<div
class="artist-card"
role="button"
tabindex="0"
onclick={() => handleLocalArtistClick(artist.name)}
onkeydown={(event) => event.key === 'Enter' && handleLocalArtistClick(artist.name)}
>
<div class="artist-icon" class:has-image={!!artistImage}>
{#if artistImage}
<img src={artistImage} alt={artist.name} class="artist-image" loading="lazy" />
{:else}
<Mic2 size={32} />
{/if}
</div>
{#if showSettings}
<button
class="artist-image-btn"
onclick={(e) => handleUploadArtistImage(artist.name, e)}
title="Upload custom image"
>
<Upload size={14} />
</button>
{/if}
<div class="artist-name">{artist.name}</div>
<div class="artist-stats">
{artist.album_count} albums &bull; {artist.track_count} tracks
</div>
</div>
{/each}
</div>
{/if}
</div>
{/if}
{/if}
{:else if activeTab === 'tracks'}
@@ -3873,12 +3996,89 @@
}
/* Artist Grid */
.artist-sections {
display: flex;
gap: 12px;
align-items: flex-start;
}
.artist-sections.virtualized {
flex: 1;
height: calc(100vh - 280px);
min-height: 400px;
}
.artist-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
gap: 24px;
}
.artist-list-virtualized {
height: 100%;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 4px;
}
.artist-list-row {
display: flex;
align-items: center;
gap: 12px;
padding: 8px 12px;
background: var(--bg-secondary);
border-radius: 8px;
cursor: pointer;
transition: background 150ms ease;
}
.artist-list-row:hover {
background: var(--bg-tertiary);
}
.artist-list-icon {
width: 40px;
height: 40px;
flex-shrink: 0;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, var(--bg-tertiary) 0%, var(--bg-secondary) 100%);
color: var(--text-muted);
overflow: hidden;
}
.artist-list-icon.has-image {
background: none;
}
.artist-list-icon img {
width: 100%;
height: 100%;
object-fit: cover;
}
.artist-list-info {
flex: 1;
min-width: 0;
}
.artist-list-name {
font-size: 14px;
font-weight: 500;
color: var(--text-primary);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.artist-list-stats {
font-size: 12px;
color: var(--text-muted);
}
.artist-card {
position: relative;
display: flex;