import { buildCoverArtUrl, coverArtCacheKey } from '../api/subsonicStreamUrl'; import { getAlbum } from '../api/subsonicLibrary'; import type { SubsonicAlbum } from '../api/subsonicTypes'; import { songToTrack } from '../utils/playback/songToTrack'; import React, { memo, useMemo } from 'react'; import { useNavigate } from 'react-router-dom'; import { Play, ListPlus, HardDriveDownload, Check } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import { usePlayerStore } from '../store/playerStore'; import { useOfflineStore } from '../store/offlineStore'; import { useAuthStore } from '../store/authStore'; import CachedImage from './CachedImage'; import { OpenArtistRefInline } from './OpenArtistRefInline'; import { playAlbum } from '../utils/playback/playAlbum'; import { useDragDrop } from '../contexts/DragDropContext'; import { isAlbumRecentlyAdded } from '../utils/albumRecency'; import { deriveAlbumArtistRefs } from '../utils/album/deriveAlbumHeaderArtistRefs'; interface AlbumCardProps { album: SubsonicAlbum; selected?: boolean; selectionMode?: boolean; onToggleSelect?: (id: string, opts?: { shiftKey?: boolean }) => void; showRating?: boolean; selectedAlbums?: SubsonicAlbum[]; disableArtwork?: boolean; artworkSize?: number; } function AlbumCard({ album, selected, selectionMode, onToggleSelect, showRating = false, selectedAlbums = [], disableArtwork = false, artworkSize = 300, }: AlbumCardProps) { const { t } = useTranslation(); const navigate = useNavigate(); const openContextMenu = usePlayerStore(s => s.openContextMenu); const enqueue = usePlayerStore(s => s.enqueue); const serverId = useAuthStore(s => s.activeServerId ?? ''); const isOffline = useOfflineStore(s => { const meta = s.albums[`${serverId}:${album.id}`]; if (!meta || meta.trackIds.length === 0) return false; return meta.trackIds.every(tid => !!s.tracks[`${serverId}:${tid}`]); }); // buildCoverArtUrl emits a salted URL; memoize to avoid churn on rerenders. const coverUrl = useMemo( () => (album.coverArt ? buildCoverArtUrl(album.coverArt, artworkSize) : ''), [album.coverArt, artworkSize], ); const coverCacheKey = useMemo( () => (album.coverArt ? coverArtCacheKey(album.coverArt, artworkSize) : ''), [album.coverArt, artworkSize], ); const psyDrag = useDragDrop(); const isNewAlbum = isAlbumRecentlyAdded(album.created); const artistRefs = useMemo(() => deriveAlbumArtistRefs(album), [album]); const handleClick = (opts?: { shiftKey?: boolean }) => { if (selectionMode) { onToggleSelect?.(album.id, opts); return; } navigate(`/album/${album.id}`); }; return (
handleClick({ shiftKey: e.shiftKey })} role="button" tabIndex={0} aria-label={`${album.name} von ${album.artist}`} onKeyDown={e => e.key === 'Enter' && handleClick()} onContextMenu={(e) => { e.preventDefault(); if (selectionMode && selectedAlbums.length > 0) { openContextMenu(e.clientX, e.clientY, selectedAlbums, 'multi-album'); } else { openContextMenu(e.clientX, e.clientY, album, 'album'); } }} onMouseDown={e => { if (selectionMode || e.button !== 0) return; e.preventDefault(); const sx = e.clientX, sy = e.clientY; const onMove = (me: MouseEvent) => { if (Math.abs(me.clientX - sx) > 5 || Math.abs(me.clientY - sy) > 5) { document.removeEventListener('mousemove', onMove); document.removeEventListener('mouseup', onUp); psyDrag.startDrag({ data: JSON.stringify({ type: 'album', id: album.id, name: album.name }), label: album.name, coverUrl: coverUrl || undefined }, me.clientX, me.clientY); } }; const onUp = () => { document.removeEventListener('mousemove', onMove); document.removeEventListener('mouseup', onUp); }; document.addEventListener('mousemove', onMove); document.addEventListener('mouseup', onUp); }} >
{!disableArtwork && coverUrl ? ( ) : (
)} {(isNewAlbum || (isOffline && !selectionMode)) && (
{isNewAlbum && (
{t('common.new', 'New')}
)} {isOffline && !selectionMode && (
)}
)} {selectionMode && (
{selected && }
)} {!selectionMode && (
)}

{album.name}

navigate(`/artist/${id}`)} as="none" linkTag="span" linkClassName="track-artist-link" />

{album.year &&

{album.year}

} {showRating && (album.userRating ?? 0) > 0 && (
{'★'.repeat(album.userRating!)}{'☆'.repeat(5 - album.userRating!)}
)}
); } export default memo(AlbumCard);