mirror of
https://github.com/kilyabin/psysonic.git
synced 2026-07-22 06:25:41 +00:00
2318f9e07a
Per bcorporaal's request in #253: making a queue from multiple albums required either replacing the queue (Play) or going into each album detail page first. Three new entry points cover the common workflows: 1. Hover button on the album cover next to Play. Both buttons share the same accent style and size so the secondary action is just as readable as the primary — first attempt with a darker pill was hard to see and unbalanced the hover. 2. Context-menu entry "Enqueue Album" between "Open Album" and "Go to Artist". The i18n key already existed (was used by the album-song sub-context); just wiring up the action. 3. Multi-select toolbar in Albums.tsx: new "Enqueue (N)" button placed before "Add Offline" so power users selecting several albums no longer have to right-click. Plus a context-menu entry "Enqueue N Albums" for the same flow when a multi-album right-click happens anywhere else. All multi-album fetches use Promise.all(getAlbum(...)) — Navidrome handles parallel requests instantly (per memory note from PR #246). i18n: 4 new keys per locale (3 with pluralisation: contextMenu.enqueueAlbums, albums.enqueueSelected, albums.enqueueQueued). Co-authored-by: Psychotoxical <dev@psysonic.app> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
325 lines
13 KiB
TypeScript
325 lines
13 KiB
TypeScript
import React, { useEffect, useState, useCallback, useRef, useMemo } from 'react';
|
|
import AlbumCard from '../components/AlbumCard';
|
|
import GenreFilterBar from '../components/GenreFilterBar';
|
|
import YearFilterButton from '../components/YearFilterButton';
|
|
import SortDropdown from '../components/SortDropdown';
|
|
import { getAlbumList, getAlbumsByGenre, getAlbum, SubsonicAlbum, buildDownloadUrl } from '../api/subsonic';
|
|
import { songToTrack } from '../store/playerStore';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useAuthStore } from '../store/authStore';
|
|
import { useOfflineStore } from '../store/offlineStore';
|
|
import { useDownloadModalStore } from '../store/downloadModalStore';
|
|
import { usePlayerStore } from '../store/playerStore';
|
|
import { invoke } from '@tauri-apps/api/core';
|
|
import { join } from '@tauri-apps/api/path';
|
|
import { showToast } from '../utils/toast';
|
|
import { useZipDownloadStore } from '../store/zipDownloadStore';
|
|
import { CheckSquare2, Download, HardDriveDownload, ListMusic, Disc3, ListPlus } from 'lucide-react';
|
|
|
|
type SortType = 'alphabeticalByName' | 'alphabeticalByArtist';
|
|
type CompFilter = 'all' | 'only' | 'hide';
|
|
|
|
const PAGE_SIZE = 30;
|
|
|
|
function sanitizeFilename(name: string): string {
|
|
return name.replace(/[<>:"/\\|?*\x00-\x1f]/g, '_').trim() || 'download';
|
|
}
|
|
|
|
async function fetchByGenres(genres: string[]): Promise<SubsonicAlbum[]> {
|
|
const results = await Promise.all(genres.map(g => getAlbumsByGenre(g, 500, 0)));
|
|
const seen = new Set<string>();
|
|
return results.flat().filter(a => { if (seen.has(a.id)) return false; seen.add(a.id); return true; });
|
|
}
|
|
|
|
export default function Albums() {
|
|
const { t } = useTranslation();
|
|
const musicLibraryFilterVersion = useAuthStore(s => s.musicLibraryFilterVersion);
|
|
const auth = useAuthStore();
|
|
const serverId = useAuthStore(s => s.activeServerId ?? '');
|
|
const downloadAlbum = useOfflineStore(s => s.downloadAlbum);
|
|
const requestDownloadFolder = useDownloadModalStore(s => s.requestFolder);
|
|
|
|
const [albums, setAlbums] = useState<SubsonicAlbum[]>([]);
|
|
const [sort, setSort] = useState<SortType>('alphabeticalByName');
|
|
const [loading, setLoading] = useState(true);
|
|
const [page, setPage] = useState(0);
|
|
const [hasMore, setHasMore] = useState(true);
|
|
const [selectedGenres, setSelectedGenres] = useState<string[]>([]);
|
|
const [yearFrom, setYearFrom] = useState('');
|
|
const [yearTo, setYearTo] = useState('');
|
|
const [compFilter, setCompFilter] = useState<CompFilter>('all');
|
|
const observerTarget = useRef<HTMLDivElement>(null);
|
|
|
|
// ── Multi-selection ──────────────────────────────────────────────────────
|
|
const [selectionMode, setSelectionMode] = useState(false);
|
|
const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());
|
|
|
|
const toggleSelectionMode = () => {
|
|
setSelectionMode(v => !v);
|
|
setSelectedIds(new Set());
|
|
};
|
|
|
|
const toggleSelect = useCallback((id: string) => {
|
|
setSelectedIds(prev => {
|
|
const next = new Set(prev);
|
|
if (next.has(id)) next.delete(id); else next.add(id);
|
|
return next;
|
|
});
|
|
}, []);
|
|
|
|
const clearSelection = () => {
|
|
setSelectionMode(false);
|
|
setSelectedIds(new Set());
|
|
};
|
|
|
|
const visibleAlbums = useMemo(() => {
|
|
if (compFilter === 'all') return albums;
|
|
if (compFilter === 'only') return albums.filter(a => a.isCompilation);
|
|
return albums.filter(a => !a.isCompilation);
|
|
}, [albums, compFilter]);
|
|
|
|
const selectedAlbums = visibleAlbums.filter(a => selectedIds.has(a.id));
|
|
const openContextMenu = usePlayerStore(state => state.openContextMenu);
|
|
const enqueue = usePlayerStore(state => state.enqueue);
|
|
|
|
const handleEnqueueSelected = async () => {
|
|
if (selectedAlbums.length === 0) return;
|
|
try {
|
|
// Parallel — Navidrome handles concurrent getAlbum requests fine.
|
|
const results = await Promise.all(selectedAlbums.map(a => getAlbum(a.id).catch(() => null)));
|
|
const tracks = results.flatMap(r => r ? r.songs.map(songToTrack) : []);
|
|
if (tracks.length > 0) {
|
|
enqueue(tracks);
|
|
showToast(t('albums.enqueueQueued', { count: selectedAlbums.length }), 2500, 'info');
|
|
}
|
|
} finally {
|
|
clearSelection();
|
|
}
|
|
};
|
|
|
|
const cycleCompFilter = () => {
|
|
setCompFilter(v => v === 'all' ? 'only' : v === 'only' ? 'hide' : 'all');
|
|
};
|
|
|
|
const handleDownloadZips = async () => {
|
|
if (selectedAlbums.length === 0) return;
|
|
const folder = auth.downloadFolder || await requestDownloadFolder();
|
|
if (!folder) return;
|
|
const { start, complete, fail } = useZipDownloadStore.getState();
|
|
clearSelection();
|
|
for (const album of selectedAlbums) {
|
|
const downloadId = crypto.randomUUID();
|
|
const filename = `${sanitizeFilename(album.name)}.zip`;
|
|
const destPath = await join(folder, filename);
|
|
const url = buildDownloadUrl(album.id);
|
|
start(downloadId, filename);
|
|
try {
|
|
await invoke('download_zip', { id: downloadId, url, destPath });
|
|
complete(downloadId);
|
|
} catch (e) {
|
|
fail(downloadId);
|
|
console.error('ZIP download failed for', album.name, e);
|
|
showToast(t('albums.downloadZipFailed', { name: album.name }), 4000, 'error');
|
|
}
|
|
}
|
|
};
|
|
|
|
const handleAddOffline = async () => {
|
|
if (selectedAlbums.length === 0) return;
|
|
let queued = 0;
|
|
for (const album of selectedAlbums) {
|
|
try {
|
|
const detail = await getAlbum(album.id);
|
|
downloadAlbum(album.id, album.name, album.artist, album.coverArt, album.year, detail.songs, serverId);
|
|
queued++;
|
|
} catch {
|
|
showToast(t('albums.offlineFailed', { name: album.name }), 3000, 'error');
|
|
}
|
|
}
|
|
if (queued > 0) showToast(t('albums.offlineQueuing', { count: queued }), 3000, 'info');
|
|
clearSelection();
|
|
};
|
|
|
|
// ── Data loading ─────────────────────────────────────────────────────────
|
|
const genreFiltered = selectedGenres.length > 0;
|
|
const fromNum = parseInt(yearFrom, 10);
|
|
const toNum = parseInt(yearTo, 10);
|
|
const yearActive = !isNaN(fromNum) && !isNaN(toNum) && fromNum >= 1 && toNum >= 1;
|
|
|
|
const load = useCallback(async (
|
|
sortType: SortType,
|
|
offset: number,
|
|
append = false,
|
|
yearFilter?: { from: number; to: number },
|
|
) => {
|
|
setLoading(true);
|
|
try {
|
|
const extra = yearFilter ? { fromYear: yearFilter.from, toYear: yearFilter.to } : {};
|
|
const type = yearFilter ? 'byYear' : sortType;
|
|
const data = await getAlbumList(type, PAGE_SIZE, offset, extra);
|
|
if (append) setAlbums(prev => [...prev, ...data]);
|
|
else setAlbums(data);
|
|
setHasMore(data.length === PAGE_SIZE);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [musicLibraryFilterVersion]);
|
|
|
|
const loadFiltered = useCallback(async (genres: string[], sortType: SortType) => {
|
|
setLoading(true);
|
|
try {
|
|
const data = await fetchByGenres(genres);
|
|
const sorted = [...data].sort((a, b) =>
|
|
sortType === 'alphabeticalByArtist'
|
|
? a.artist.localeCompare(b.artist)
|
|
: a.name.localeCompare(b.name)
|
|
);
|
|
setAlbums(sorted);
|
|
setHasMore(false);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [musicLibraryFilterVersion]);
|
|
|
|
useEffect(() => {
|
|
setPage(0);
|
|
if (genreFiltered) {
|
|
loadFiltered(selectedGenres, sort);
|
|
} else if (yearActive) {
|
|
load(sort, 0, false, { from: fromNum, to: toNum });
|
|
} else {
|
|
load(sort, 0);
|
|
}
|
|
}, [sort, genreFiltered, selectedGenres, yearActive, fromNum, toNum, load, loadFiltered]);
|
|
|
|
const loadMore = useCallback(() => {
|
|
if (loading || !hasMore || genreFiltered) return;
|
|
const next = page + 1;
|
|
setPage(next);
|
|
const yf = yearActive ? { from: fromNum, to: toNum } : undefined;
|
|
load(sort, next * PAGE_SIZE, true, yf);
|
|
}, [loading, hasMore, page, sort, load, genreFiltered, yearActive, fromNum, toNum]);
|
|
|
|
useEffect(() => {
|
|
const observer = new IntersectionObserver(
|
|
entries => { if (entries[0].isIntersecting) loadMore(); },
|
|
// Prefetch ~1.5 screens ahead so the user never visibly waits at the
|
|
// bottom of the grid. 200px caught the user at the very edge.
|
|
{ rootMargin: '1500px' }
|
|
);
|
|
if (observerTarget.current) observer.observe(observerTarget.current);
|
|
return () => observer.disconnect();
|
|
}, [loadMore]);
|
|
|
|
const sortOptions: { value: SortType; label: string }[] = [
|
|
{ value: 'alphabeticalByName', label: t('albums.sortByName') },
|
|
{ value: 'alphabeticalByArtist', label: t('albums.sortByArtist') },
|
|
];
|
|
|
|
return (
|
|
<div className="content-body animate-fade-in">
|
|
<div className="page-sticky-header" style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', flexWrap: 'wrap', gap: '0.75rem' }}>
|
|
<h1 className="page-title" style={{ marginBottom: 0 }}>
|
|
{selectionMode && selectedIds.size > 0
|
|
? t('albums.selectionCount', { count: selectedIds.size })
|
|
: t('albums.title')}
|
|
</h1>
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: '0.5rem', flexWrap: 'wrap' }}>
|
|
{selectionMode && selectedIds.size > 0 ? (
|
|
<>
|
|
<button className="btn btn-surface albums-selection-action-btn" onClick={handleEnqueueSelected}>
|
|
<ListPlus size={15} />
|
|
{t('albums.enqueueSelected', { count: selectedIds.size })}
|
|
</button>
|
|
<button className="btn btn-surface albums-selection-action-btn" onClick={handleAddOffline}>
|
|
<HardDriveDownload size={15} />
|
|
{t('albums.addOffline')}
|
|
</button>
|
|
<button className="btn btn-surface albums-selection-action-btn" onClick={handleDownloadZips}>
|
|
<Download size={15} />
|
|
{t('albums.downloadZips')}
|
|
</button>
|
|
</>
|
|
) : (
|
|
<>
|
|
{!yearActive && (
|
|
<SortDropdown
|
|
value={sort}
|
|
options={sortOptions}
|
|
onChange={setSort}
|
|
/>
|
|
)}
|
|
|
|
<YearFilterButton
|
|
from={yearFrom}
|
|
to={yearTo}
|
|
onChange={(from, to) => { setYearFrom(from); setYearTo(to); }}
|
|
/>
|
|
|
|
<GenreFilterBar selected={selectedGenres} onSelectionChange={setSelectedGenres} />
|
|
|
|
<button
|
|
className={`btn btn-surface${compFilter !== 'all' ? ' btn-sort-active' : ''}`}
|
|
onClick={cycleCompFilter}
|
|
data-tooltip={
|
|
compFilter === 'all' ? t('albums.compilationTooltipAll')
|
|
: compFilter === 'only' ? t('albums.compilationTooltipOnly')
|
|
: t('albums.compilationTooltipHide')
|
|
}
|
|
data-tooltip-pos="bottom"
|
|
style={{
|
|
display: 'flex', alignItems: 'center', gap: '0.4rem',
|
|
...(compFilter !== 'all' ? { background: 'var(--accent)', color: 'var(--ctp-crust)' } : {}),
|
|
}}
|
|
>
|
|
<Disc3 size={14} />
|
|
{compFilter === 'all' ? t('albums.compilationLabel')
|
|
: compFilter === 'only' ? t('albums.compilationOnly')
|
|
: t('albums.compilationHide')}
|
|
</button>
|
|
</>
|
|
)}
|
|
|
|
<button
|
|
className={`btn btn-surface${selectionMode ? ' btn-sort-active' : ''}`}
|
|
onClick={toggleSelectionMode}
|
|
data-tooltip={selectionMode ? t('albums.cancelSelect') : t('albums.startSelect')}
|
|
data-tooltip-pos="bottom"
|
|
style={selectionMode ? { background: 'var(--accent)', color: 'var(--ctp-crust)' } : {}}
|
|
>
|
|
<CheckSquare2 size={15} />
|
|
{selectionMode ? t('albums.cancelSelect') : t('albums.select')}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{loading && albums.length === 0 ? (
|
|
<div style={{ display: 'flex', justifyContent: 'center', padding: '3rem' }}>
|
|
<div className="spinner" />
|
|
</div>
|
|
) : (
|
|
<>
|
|
<div className="album-grid-wrap">
|
|
{visibleAlbums.map(a => (
|
|
<AlbumCard
|
|
key={a.id}
|
|
album={a}
|
|
selectionMode={selectionMode}
|
|
selected={selectedIds.has(a.id)}
|
|
onToggleSelect={toggleSelect}
|
|
selectedAlbums={selectedAlbums}
|
|
/>
|
|
))}
|
|
</div>
|
|
{!genreFiltered && (
|
|
<div ref={observerTarget} style={{ height: '20px', margin: '2rem 0', display: 'flex', justifyContent: 'center' }}>
|
|
{loading && hasMore && <div className="spinner" style={{ width: 20, height: 20 }} />}
|
|
</div>
|
|
)}
|
|
</>
|
|
)}
|
|
|
|
</div>
|
|
);
|
|
}
|