import type React from 'react'; import { getPlaylist } from '../api/subsonicPlaylists'; import { filterSongsToActiveLibrary } from '../api/subsonicLibrary'; import type { SubsonicPlaylist, SubsonicSong } from '../api/subsonicTypes'; export interface RunPlaylistLoadDeps { id: string; setLoading: (v: boolean) => void; setPlaylist: React.Dispatch>; setSongs: React.Dispatch>; setCustomCoverId: React.Dispatch>; setRatings: React.Dispatch>>; setStarredSongs: React.Dispatch>>; } export async function runPlaylistLoad(deps: RunPlaylistLoadDeps): Promise { const { id, setLoading, setPlaylist, setSongs, setCustomCoverId, setRatings, setStarredSongs } = deps; setLoading(true); try { const { playlist, songs } = await getPlaylist(id); const filteredSongs = await filterSongsToActiveLibrary(songs); setPlaylist(playlist); setSongs(filteredSongs); if (playlist.coverArt) setCustomCoverId(playlist.coverArt); const init: Record = {}; const starred = new Set(); filteredSongs.forEach(s => { if (s.userRating) init[s.id] = s.userRating; if (s.starred) starred.add(s.id); }); setRatings(init); setStarredSongs(starred); } catch { // intentional swallow; load failure leaves loading false + playlist null } finally { setLoading(false); } }