import React, { useEffect, useState, useCallback, useRef } from 'react'; import { RefreshCw } from 'lucide-react'; import { getAlbumList, getAlbumsByGenre, SubsonicAlbum } from '../api/subsonic'; import AlbumCard from '../components/AlbumCard'; import GenreFilterBar from '../components/GenreFilterBar'; import { useTranslation } from 'react-i18next'; const ALBUM_COUNT = 30; async function fetchByGenres(genres: string[]): Promise { const results = await Promise.all(genres.map(g => getAlbumsByGenre(g, 500, 0))); const seen = new Set(); const union = results.flat().filter(a => { if (seen.has(a.id)) return false; seen.add(a.id); return true; }); // Fisher-Yates shuffle for (let i = union.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [union[i], union[j]] = [union[j], union[i]]; } return union.slice(0, ALBUM_COUNT); } export default function RandomAlbums() { const { t } = useTranslation(); const [albums, setAlbums] = useState([]); const [loading, setLoading] = useState(true); const [selectedGenres, setSelectedGenres] = useState([]); const loadingRef = useRef(false); const filtered = selectedGenres.length > 0; const load = useCallback(async (genres: string[]) => { if (loadingRef.current) return; loadingRef.current = true; setLoading(true); try { const data = genres.length > 0 ? await fetchByGenres(genres) : await getAlbumList('random', ALBUM_COUNT); setAlbums(data); } catch (e) { console.error(e); } finally { loadingRef.current = false; setLoading(false); } }, []); useEffect(() => { load(selectedGenres); }, [selectedGenres, load]); return (

{t('randomAlbums.title')}

{loading && albums.length === 0 ? (
) : (
{albums.map(a => )}
)}
); }