perf(sidebar): virtualize playlist list to prevent UI freeze
Replace the non-virtualized playlist rendering in the Sidebar with an inline virtual scroll implementation. Users with 500+ playlists experienced 20-second UI freezes because all playlists were mounted as DOM nodes simultaneously. Now only ~20-30 visible items (plus buffer) are rendered at any time, using absolute positioning with translateY and binary search for efficient viewport calculation. Follows the existing VirtualizedArtistList pattern. Fixes #56
This commit is contained in:
1 parent
22055d15e4
commit
05414eae37
1 file changed
+237
-61
@@ -124,6 +124,21 @@
|
|||||||
let showCreateFolderModal = $state(false);
|
let showCreateFolderModal = $state(false);
|
||||||
let newFolderName = $state('');
|
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
|
// Context menu state
|
||||||
let contextMenu = $state<{
|
let contextMenu = $state<{
|
||||||
visible: boolean;
|
visible: boolean;
|
||||||
@@ -588,6 +603,154 @@
|
|||||||
// Check if any playlists exist in root (no folder)
|
// Check if any playlists exist in root (no folder)
|
||||||
let rootPlaylists = $derived(getPlaylistsInFolder(null));
|
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
|
// Subscribe to global floating menu store
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
const unsubscribe = subscribeFloatingMenu(() => {
|
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
|
// Sidebar search handlers
|
||||||
const SEARCH_NAV_THRESHOLD = 3; // Navigate to search after this many characters
|
const SEARCH_NAV_THRESHOLD = 3; // Navigate to search after this many characters
|
||||||
|
|
||||||
@@ -1229,76 +1408,67 @@
|
|||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if !playlistsCollapsed || !isExpanded}
|
{#if !playlistsCollapsed || !isExpanded}
|
||||||
<div class="playlists-scroll">
|
<div class="playlists-scroll" bind:this={playlistScrollEl} onscroll={handlePlaylistScroll}>
|
||||||
{#if playlistsLoading}
|
{#if playlistsLoading}
|
||||||
{#if isExpanded}
|
{#if isExpanded}
|
||||||
<div class="playlists-loading">{$t('actions.loading')}</div>
|
<div class="playlists-loading">{$t('actions.loading')}</div>
|
||||||
{/if}
|
{/if}
|
||||||
{:else if visiblePlaylists.length > 0 || folders.length > 0}
|
{:else if visiblePlaylists.length > 0 || folders.length > 0}
|
||||||
<nav class="playlists-nav">
|
<div class="playlists-virtual-content" style="height: {totalPlaylistHeight}px;">
|
||||||
<!-- Folders with their playlists -->
|
{#each visiblePlaylistItems as item (getPlaylistItemKey(item))}
|
||||||
{#each folders as folder (folder.id)}
|
<div class="playlists-virtual-item" style="transform: translateY({item.top}px); height: {item.height}px;">
|
||||||
{@const folderPlaylists = getPlaylistsInFolder(folder.id)}
|
{#if item.type === 'folder-header'}
|
||||||
{@const isExpanded_ = isFolderExpanded(folder.id)}
|
{@const folderPlaylists = getPlaylistsInFolder(item.folder.id)}
|
||||||
{#if isExpanded}
|
{@const isFolderExp = isFolderExpanded(item.folder.id)}
|
||||||
<div class="folder-item">
|
|
||||||
<button
|
<button
|
||||||
class="folder-header"
|
class="folder-header"
|
||||||
onclick={() => handleToggleFolder(folder.id)}
|
onclick={() => handleToggleFolder(item.folder.id)}
|
||||||
>
|
>
|
||||||
<Folder size={14} />
|
<Folder size={14} />
|
||||||
<span class="folder-name">{folder.name}</span>
|
<span class="folder-name">{item.folder.name}</span>
|
||||||
<span class="folder-count">{folderPlaylists.length}</span>
|
<span class="folder-count">{folderPlaylists.length}</span>
|
||||||
<span class="folder-chevron" class:expanded={isExpanded_}>
|
<span class="folder-chevron" class:expanded={isFolderExp}>
|
||||||
<ChevronRight size={12} />
|
<ChevronRight size={12} />
|
||||||
</span>
|
</span>
|
||||||
</button>
|
</button>
|
||||||
{#if isExpanded_}
|
{:else if item.type === 'folder-playlist'}
|
||||||
<div class="folder-playlists">
|
<NavigationItem
|
||||||
{#each folderPlaylists as playlist (playlist.id)}
|
label={item.playlist.name}
|
||||||
<NavigationItem
|
tooltip={getPlaylistTooltip(item.playlist, true)}
|
||||||
label={playlist.name}
|
active={activeView === 'playlist' && selectedPlaylistId === item.playlist.id}
|
||||||
tooltip={getPlaylistTooltip(playlist, true)}
|
onclick={() => handlePlaylistClick(item.playlist)}
|
||||||
active={activeView === 'playlist' && selectedPlaylistId === playlist.id}
|
onHover={() => loadPlaylistTooltip(item.playlist)}
|
||||||
onclick={() => handlePlaylistClick(playlist)}
|
oncontextmenu={(e) => handlePlaylistContextMenu(e, item.playlist, item.folderId)}
|
||||||
onHover={() => loadPlaylistTooltip(playlist)}
|
showLabel={true}
|
||||||
oncontextmenu={(e) => handlePlaylistContextMenu(e, playlist, folder.id)}
|
indented={true}
|
||||||
showLabel={true}
|
>
|
||||||
indented={true}
|
{#snippet icon()}<ListMusic size={14} />{/snippet}
|
||||||
>
|
</NavigationItem>
|
||||||
{#snippet icon()}<ListMusic size={14} />{/snippet}
|
{:else if item.type === 'root-playlist'}
|
||||||
</NavigationItem>
|
<NavigationItem
|
||||||
{/each}
|
label={item.playlist.name}
|
||||||
</div>
|
tooltip={getPlaylistTooltip(item.playlist, isExpanded)}
|
||||||
{/if}
|
active={activeView === 'playlist' && selectedPlaylistId === item.playlist.id}
|
||||||
</div>
|
onclick={() => handlePlaylistClick(item.playlist)}
|
||||||
{:else}
|
onHover={() => loadPlaylistTooltip(item.playlist)}
|
||||||
<!-- Collapsed sidebar: show folder icon only -->
|
oncontextmenu={(e) => handlePlaylistContextMenu(e, item.playlist, null)}
|
||||||
<button
|
showLabel={isExpanded}
|
||||||
class="collapsed-folder-btn"
|
>
|
||||||
onclick={(e) => showFolderPopover(e, folder)}
|
{#snippet icon()}<ListMusic size={14} />{/snippet}
|
||||||
title="{folder.name} ({folderPlaylists.length})"
|
</NavigationItem>
|
||||||
>
|
{:else if item.type === 'collapsed-folder'}
|
||||||
<Folder size={14} />
|
{@const folderPlaylists = getPlaylistsInFolder(item.folder.id)}
|
||||||
</button>
|
<button
|
||||||
{/if}
|
class="collapsed-folder-btn"
|
||||||
|
onclick={(e) => showFolderPopover(e, item.folder)}
|
||||||
|
title="{item.folder.name} ({folderPlaylists.length})"
|
||||||
|
>
|
||||||
|
<Folder size={14} />
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
|
</div>
|
||||||
<!-- Root playlists (not in any folder) -->
|
|
||||||
{#each rootPlaylists as playlist (playlist.id)}
|
|
||||||
<NavigationItem
|
|
||||||
label={playlist.name}
|
|
||||||
tooltip={getPlaylistTooltip(playlist, isExpanded)}
|
|
||||||
active={activeView === 'playlist' && selectedPlaylistId === playlist.id}
|
|
||||||
onclick={() => handlePlaylistClick(playlist)}
|
|
||||||
onHover={() => loadPlaylistTooltip(playlist)}
|
|
||||||
oncontextmenu={(e) => handlePlaylistContextMenu(e, playlist, null)}
|
|
||||||
showLabel={isExpanded}
|
|
||||||
>
|
|
||||||
{#snippet icon()}<ListMusic size={14} />{/snippet}
|
|
||||||
</NavigationItem>
|
|
||||||
{/each}
|
|
||||||
</nav>
|
|
||||||
{:else if userPlaylists.length > 0}
|
{:else if userPlaylists.length > 0}
|
||||||
{#if isExpanded}
|
{#if isExpanded}
|
||||||
<div class="no-playlists">{$t('playlist.allHidden')}</div>
|
<div class="no-playlists">{$t('playlist.allHidden')}</div>
|
||||||
@@ -1731,11 +1901,17 @@
|
|||||||
color: var(--text-primary);
|
color: var(--text-primary);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Playlist list view */
|
/* Virtualized playlist list */
|
||||||
.playlists-nav {
|
.playlists-virtual-content {
|
||||||
display: flex;
|
position: relative;
|
||||||
flex-direction: column;
|
width: 100%;
|
||||||
gap: 2px;
|
}
|
||||||
|
|
||||||
|
.playlists-virtual-item {
|
||||||
|
position: absolute;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
will-change: transform;
|
||||||
}
|
}
|
||||||
|
|
||||||
.playlists-scroll {
|
.playlists-scroll {
|
||||||
|
|||||||
Reference in new issue
Block a user