Files
Psychotoxical-psysonic/src/utils/playlist/runPlaylistLoad.ts
T
Psychotoxical 7c724a642f chore(eslint): add ESLint toolchain and clean src to strict 0/0 (#1165)
* chore(eslint): add eslint toolchain and configs

* fix(eslint): resolve gradual-config errors (rules-of-hooks, no-empty, prefer-const, …)

* chore(eslint): clear unused vars in config, contexts, app and test

* chore(eslint): clear unused vars in api and music-network

* chore(eslint): clear unused vars in utils

* chore(eslint): clear unused vars in store

* chore(eslint): clear unused vars in cover and hooks

* chore(eslint): clear unused vars in components

* chore(eslint): clear unused vars in pages

* chore(eslint): remove explicit any in src

* chore(eslint): align react-hooks exhaustive-deps

* chore(eslint): zero gradual config on src

* chore(eslint): strict hook rules in store and utils

* chore(eslint): strict hook rules in hooks and cover

* chore(eslint): strict hook rules in components

* chore(eslint): strict hook rules in pages and contexts

* chore(eslint): document scripts ignore in eslint config

* chore(eslint): add lint script and zero strict findings

* chore(eslint): address review round 1 (gradual 0/0, per-site disable reasons)

* chore(eslint): tighten two set-state disable comments (review round 2 LOW)

* chore(nix): sync npmDepsHash with package-lock.json

* docs(changelog): add Under the Hood entry for ESLint setup (PR #1165)

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-06-24 01:33:34 +02:00

65 lines
2.3 KiB
TypeScript

import type React from 'react';
import { getPlaylist } from '../../api/subsonicPlaylists';
import { filterSongsToActiveLibrary } from '../../api/subsonicLibrary';
import type { SubsonicPlaylist, SubsonicSong } from '../../api/subsonicTypes';
import { useAuthStore } from '../../store/authStore';
import { usePlaylistStore } from '../../store/playlistStore';
import { isOfflineBrowseActive } from '../offline/offlineBrowseMode';
import { resolvePlaylist } from '../offline/offlineMediaResolve';
export interface RunPlaylistLoadDeps {
id: string;
setLoading: (v: boolean) => void;
setPlaylist: React.Dispatch<React.SetStateAction<SubsonicPlaylist | null>>;
setSongs: React.Dispatch<React.SetStateAction<SubsonicSong[]>>;
setCustomCoverId: React.Dispatch<React.SetStateAction<string | null>>;
setRatings: React.Dispatch<React.SetStateAction<Record<string, number>>>;
setStarredSongs: React.Dispatch<React.SetStateAction<Set<string>>>;
}
function applyLoadedPlaylist(
deps: RunPlaylistLoadDeps,
playlist: SubsonicPlaylist,
songs: SubsonicSong[],
): void {
const { setPlaylist, setSongs, setCustomCoverId, setRatings, setStarredSongs } = deps;
setPlaylist(playlist);
setSongs(songs);
if (playlist.coverArt) setCustomCoverId(playlist.coverArt);
const init: Record<string, number> = {};
const starred = new Set<string>();
songs.forEach(s => {
if (s.userRating) init[s.id] = s.userRating;
if (s.starred) starred.add(s.id);
});
setRatings(init);
setStarredSongs(starred);
}
export async function runPlaylistLoad(deps: RunPlaylistLoadDeps): Promise<void> {
const { id, setLoading, setPlaylist, setSongs } = deps;
setLoading(true);
try {
const serverId = useAuthStore.getState().activeServerId ?? '';
if (isOfflineBrowseActive() && serverId) {
const loaded = await resolvePlaylist(serverId, id);
if (loaded) {
applyLoadedPlaylist(deps, loaded.playlist, loaded.songs);
return;
}
}
const { playlist, songs } = await getPlaylist(id);
const filteredSongs = await filterSongsToActiveLibrary(songs);
applyLoadedPlaylist(deps, playlist, filteredSongs);
} catch {
const stub = usePlaylistStore.getState().playlists.find(p => p.id === id);
if (stub) {
setPlaylist(stub);
setSongs([]);
}
} finally {
setLoading(false);
}
}