diff --git a/src/lib/components/views/ArtistDetailView.svelte b/src/lib/components/views/ArtistDetailView.svelte index 67fa84183..3c87d4cdf 100644 --- a/src/lib/components/views/ArtistDetailView.svelte +++ b/src/lib/components/views/ArtistDetailView.svelte @@ -139,6 +139,11 @@ let searchInputEl = $state(null); let currentSearchIndex = $state(0); + // Album sorting state + type AlbumSortMode = 'default' | 'newest' | 'oldest' | 'title-asc' | 'title-desc'; + let albumSortMode = $state('default'); + let showAlbumSortMenu = $state(false); + // Download status tracking let albumDownloadStatuses = $state>(new Map()); @@ -196,6 +201,7 @@ similarArtists = []; similarArtistImageErrors = new Set(); activeJumpSection = 'about'; + albumSortMode = 'default'; // Reset sort when artist changes loadTopTracks(); loadSimilarArtists(); @@ -203,6 +209,24 @@ loadArtistAlbumDownloadStatuses(); }); + // Close sort menu when clicking outside + $effect(() => { + if (!showAlbumSortMenu) return; + + function handleClick() { + showAlbumSortMenu = false; + } + + // Delay to avoid closing immediately + setTimeout(() => { + document.addEventListener('click', handleClick); + }, 0); + + return () => { + document.removeEventListener('click', handleClick); + }; + }); + async function loadArtistAlbumDownloadStatuses() { const allAlbums = [ ...artist.albums, @@ -531,38 +555,70 @@ let showJumpNav = $derived(jumpSections.length > 1); - // Search filtering + // Album sorting helper + type AlbumItem = { id: string; title: string; year?: string; artwork: string; quality: string }; + function sortAlbums(albums: T[], mode: AlbumSortMode): T[] { + if (mode === 'default') return albums; + return [...albums].sort((a, b) => { + switch (mode) { + case 'newest': { + const yearA = a.year || '0000'; + const yearB = b.year || '0000'; + return yearB.localeCompare(yearA); + } + case 'oldest': { + const yearA = a.year || '9999'; + const yearB = b.year || '9999'; + return yearA.localeCompare(yearB); + } + case 'title-asc': + return a.title.localeCompare(b.title); + case 'title-desc': + return b.title.localeCompare(a.title); + default: + return 0; + } + }); + } + + // Search filtering and sorting let searchLower = $derived(searchQuery.toLowerCase().trim()); - let filteredAlbums = $derived( - searchLower + let filteredAlbums = $derived.by(() => { + let albums = searchLower ? artist.albums.filter(a => a.title.toLowerCase().includes(searchLower)) - : artist.albums - ); - let filteredEpsSingles = $derived( - searchLower + : artist.albums; + return sortAlbums(albums, albumSortMode); + }); + let filteredEpsSingles = $derived.by(() => { + let albums = searchLower ? artist.epsSingles.filter(a => a.title.toLowerCase().includes(searchLower)) - : artist.epsSingles - ); - let filteredLiveAlbums = $derived( - searchLower + : artist.epsSingles; + return sortAlbums(albums, albumSortMode); + }); + let filteredLiveAlbums = $derived.by(() => { + let albums = searchLower ? artist.liveAlbums.filter(a => a.title.toLowerCase().includes(searchLower)) - : artist.liveAlbums - ); - let filteredCompilations = $derived( - searchLower + : artist.liveAlbums; + return sortAlbums(albums, albumSortMode); + }); + let filteredCompilations = $derived.by(() => { + let albums = searchLower ? artist.compilations.filter(a => a.title.toLowerCase().includes(searchLower)) - : artist.compilations - ); - let filteredTributes = $derived( - searchLower + : artist.compilations; + return sortAlbums(albums, albumSortMode); + }); + let filteredTributes = $derived.by(() => { + let albums = searchLower ? artist.tributes.filter(a => a.title.toLowerCase().includes(searchLower)) - : artist.tributes - ); - let filteredOthers = $derived( - searchLower + : artist.tributes; + return sortAlbums(albums, albumSortMode); + }); + let filteredOthers = $derived.by(() => { + let albums = searchLower ? artist.others.filter(a => a.title.toLowerCase().includes(searchLower)) - : artist.others - ); + : artist.others; + return sortAlbums(albums, albumSortMode); + }); let filteredPlaylists = $derived( searchLower ? artist.playlists.filter(p => p.title.toLowerCase().includes(searchLower)) @@ -841,6 +897,59 @@ {/each} + +
+ + {#if showAlbumSortMenu} +
+ + + + + +
+ {/if} +