diff --git a/src/lib/components/VirtualizedArtistGrid.svelte b/src/lib/components/VirtualizedArtistGrid.svelte index a66609a17..dcfc6eecf 100644 --- a/src/lib/components/VirtualizedArtistGrid.svelte +++ b/src/lib/components/VirtualizedArtistGrid.svelte @@ -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; 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(); + 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}`; + }
- {#each visibleArtists as { artist, top, left } (artist.name)} - {@const artistImage = artistImages.get(artist.name)} + {#each visibleItems as item (getItemKey(item))}
onArtistClick(artist.name)} - onkeydown={(e) => e.key === 'Enter' && onArtistClick(artist.name)} + class="virtual-item" + style="transform: translateY({item.top}px); height: {item.height}px;" > -
- {#if artistImage} - {artist.name} - {:else} - - {/if} -
- {#if showSettings} - + {#if item.type === 'header'} +
+ {item.key} +
+ {:else if item.type === 'row'} +
+ {#each item.artists as artist (artist.name)} + {@const artistImage = artistImages.get(artist.name)} +
onArtistClick(artist.name)} + onkeydown={(e) => e.key === 'Enter' && onArtistClick(artist.name)} + > +
+ {#if artistImage} + {artist.name} + {:else} + + {/if} +
+ {#if showSettings} + + {/if} +
{artist.name}
+
+ {artist.album_count} albums • {artist.track_count} tracks +
+
+ {/each} +
{/if} -
{artist.name}
-
- {artist.album_count} albums • {artist.track_count} tracks -
{/each}
@@ -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 { diff --git a/src/lib/components/views/LocalLibraryView.svelte b/src/lib/components/views/LocalLibraryView.svelte index 84a25c04c..b8b008a1c 100644 --- a/src/lib/components/views/LocalLibraryView.svelte +++ b/src/lib/components/views/LocalLibraryView.svelte @@ -176,7 +176,11 @@ // Performance mode state let useVirtualization = $state(isVirtualizationEnabled()); let virtualizedScrollTarget = $state(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() + }; + } + + // Group by first letter + const groups = new Map(); + 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 @@

No artists in library

{:else} + {@const { grouped: groupedArtists, alphaGroups: artistAlphaGroups } = groupedArtistsMemo} {@const filteredArtists = filteredArtistsMemo}
+
+ + +
{filteredArtists.length} artists
@@ -2467,49 +2532,107 @@

No artists match your search

{:else} - {#if useVirtualization} - - {:else} -
- {#each filteredArtists as artist (artist.name)} - {@const artistImage = artistImages.get(artist.name)} -
handleLocalArtistClick(artist.name)} - onkeydown={(event) => event.key === 'Enter' && handleLocalArtistClick(artist.name)} - > -
- {#if artistImage} - {artist.name} - {:else} - - {/if} -
- {#if showSettings} - - {/if} -
{artist.name}
-
- {artist.album_count} albums • {artist.track_count} tracks +
+ {#if useVirtualization} + {#if artistViewMode === 'grid'} +
+ +
+ {:else} + +
+
+ {#each groupedArtists as group (group.id)} + {#if artistGroupingEnabled && group.key} +
+ {group.key} +
+ {/if} + {#each group.artists as artist (artist.name)} + {@const artistImage = artistImages.get(artist.name)} +
handleLocalArtistClick(artist.name)} + onkeydown={(e) => e.key === 'Enter' && handleLocalArtistClick(artist.name)} + > +
+ {#if artistImage} + {artist.name} + {:else} + + {/if} +
+
+
{artist.name}
+
+ {artist.album_count} albums • {artist.track_count} tracks +
+
+
+ {/each} + {/each}
- {/each} -
- {/if} + {/if} + + {#if artistGroupingEnabled} +
+ {#each alphaIndexLetters as letter} + + {/each} +
+ {/if} + {:else} +
+ {#each filteredArtists as artist (artist.name)} + {@const artistImage = artistImages.get(artist.name)} +
handleLocalArtistClick(artist.name)} + onkeydown={(event) => event.key === 'Enter' && handleLocalArtistClick(artist.name)} + > +
+ {#if artistImage} + {artist.name} + {:else} + + {/if} +
+ {#if showSettings} + + {/if} +
{artist.name}
+
+ {artist.album_count} albums • {artist.track_count} tracks +
+
+ {/each} +
+ {/if} +
{/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;