diff --git a/src/lib/components/Sidebar.svelte b/src/lib/components/Sidebar.svelte index 57e6e0d02..f0044e5af 100644 --- a/src/lib/components/Sidebar.svelte +++ b/src/lib/components/Sidebar.svelte @@ -124,6 +124,21 @@ let showCreateFolderModal = $state(false); let newFolderName = $state(''); + // Virtual scroll state for playlists + type VirtualPlaylistItem = + | { type: 'folder-header'; folder: PlaylistFolder; folderId: string; top: number; height: number } + | { type: 'folder-playlist'; playlist: Playlist; folder: PlaylistFolder; folderId: string; top: number; height: number } + | { type: 'root-playlist'; playlist: Playlist; folderId: null; top: number; height: number } + | { type: 'collapsed-folder'; folder: PlaylistFolder; folderId: string; top: number; height: number }; + + let playlistScrollEl: HTMLDivElement | null = $state(null); + let playlistScrollTop = $state(0); + let playlistContainerHeight = $state(0); + + const PLAYLIST_ITEM_HEIGHT = 34; // 32px item + 2px gap + const PLAYLIST_FOLDER_HEADER_HEIGHT = 34; // ~32px header + 2px gap + const PLAYLIST_BUFFER_ITEMS = 10; + // Context menu state let contextMenu = $state<{ visible: boolean; @@ -588,6 +603,154 @@ // Check if any playlists exist in root (no folder) let rootPlaylists = $derived(getPlaylistsInFolder(null)); + // Flattened virtual playlist items for virtualized scrolling + let virtualPlaylistItems = $derived.by(() => { + const items: VirtualPlaylistItem[] = []; + let currentTop = 0; + + // Reference folderExpandState to trigger re-derivation when folders are toggled + // (isFolderExpanded reads from a non-reactive Set, so we need this dependency) + void folderExpandState; + + if (isExpanded) { + // Expanded sidebar: folders with headers + playlists, then root playlists + for (const folder of folders) { + const folderPlaylists = getPlaylistsInFolder(folder.id); + const expanded = isFolderExpanded(folder.id); + + // Folder header + items.push({ + type: 'folder-header', + folder, + folderId: folder.id, + top: currentTop, + height: PLAYLIST_FOLDER_HEADER_HEIGHT + }); + currentTop += PLAYLIST_FOLDER_HEADER_HEIGHT; + + // Folder playlists (only if expanded) + if (expanded) { + for (const playlist of folderPlaylists) { + items.push({ + type: 'folder-playlist', + playlist, + folder, + folderId: folder.id, + top: currentTop, + height: PLAYLIST_ITEM_HEIGHT + }); + currentTop += PLAYLIST_ITEM_HEIGHT; + } + } + } + + // Root playlists + for (const playlist of rootPlaylists) { + items.push({ + type: 'root-playlist', + playlist, + folderId: null, + top: currentTop, + height: PLAYLIST_ITEM_HEIGHT + }); + currentTop += PLAYLIST_ITEM_HEIGHT; + } + } else { + // Collapsed sidebar: folder icons + playlist icons + for (const folder of folders) { + items.push({ + type: 'collapsed-folder', + folder, + folderId: folder.id, + top: currentTop, + height: PLAYLIST_ITEM_HEIGHT + }); + currentTop += PLAYLIST_ITEM_HEIGHT; + } + + for (const playlist of rootPlaylists) { + items.push({ + type: 'root-playlist', + playlist, + folderId: null, + top: currentTop, + height: PLAYLIST_ITEM_HEIGHT + }); + currentTop += PLAYLIST_ITEM_HEIGHT; + } + } + + return items; + }); + + let totalPlaylistHeight = $derived( + virtualPlaylistItems.length > 0 + ? virtualPlaylistItems[virtualPlaylistItems.length - 1].top + virtualPlaylistItems[virtualPlaylistItems.length - 1].height + : 0 + ); + + // Binary search for first visible item + function playlistBinarySearchStart(items: VirtualPlaylistItem[], 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 playlistBinarySearchEnd(items: VirtualPlaylistItem[], 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; + } + + let visiblePlaylistItems = $derived.by(() => { + if (virtualPlaylistItems.length === 0) return []; + + const viewportTop = playlistScrollTop; + const viewportBottom = playlistScrollTop + playlistContainerHeight; + + const firstVisible = playlistBinarySearchStart(virtualPlaylistItems, viewportTop); + const lastVisible = playlistBinarySearchEnd(virtualPlaylistItems, viewportBottom, firstVisible); + + const startIdx = Math.max(0, firstVisible - PLAYLIST_BUFFER_ITEMS); + const endIdx = Math.min(virtualPlaylistItems.length - 1, lastVisible + PLAYLIST_BUFFER_ITEMS); + + return virtualPlaylistItems.slice(startIdx, endIdx + 1); + }); + + function handlePlaylistScroll(e: Event) { + playlistScrollTop = (e.target as HTMLDivElement).scrollTop; + } + + function getPlaylistItemKey(item: VirtualPlaylistItem): string { + if (item.type === 'folder-header' || item.type === 'collapsed-folder') return `f-${item.folder.id}`; + return `p-${item.playlist.id}`; + } + // Subscribe to global floating menu store $effect(() => { const unsubscribe = subscribeFloatingMenu(() => { @@ -796,6 +959,22 @@ }; }); + // ResizeObserver for playlist virtual scroll container + $effect(() => { + if (playlistScrollEl) { + playlistContainerHeight = playlistScrollEl.clientHeight; + + const observer = new ResizeObserver((entries) => { + for (const entry of entries) { + playlistContainerHeight = entry.contentRect.height; + } + }); + observer.observe(playlistScrollEl); + + return () => observer.disconnect(); + } + }); + // Sidebar search handlers const SEARCH_NAV_THRESHOLD = 3; // Navigate to search after this many characters @@ -1229,76 +1408,67 @@ {/if} {#if !playlistsCollapsed || !isExpanded} -