mirror of
https://github.com/Psychotoxical/psysonic.git
synced 2026-07-22 07:15:47 +00:00
7a7a9f5e6b
111 of 122 top-level src/utils/ files move into 16 topic folders (audio, cache, cover, share, server, playback, playlist, deviceSync, waveform, mix, format, export, changelog, ui, perf, componentHelpers). True singletons with no cluster stay at the utils/ root. Pure file-move: a path-aware codemod rewrote 539 relative-import specifiers across 275 files; no logic touched. The hot-path coverage gate list (.github/frontend-hot-path-files.txt) is updated to the new paths for the 11 gated utils files — a mechanical consequence of the move, not a CI change. tsc is green.
58 lines
2.0 KiB
TypeScript
58 lines
2.0 KiB
TypeScript
import { useEffect, useState } from 'react';
|
||
import { filterSongsToActiveLibrary } from '../api/subsonicLibrary';
|
||
import { getPlaylist } from '../api/subsonicPlaylists';
|
||
import type { SubsonicPlaylist } from '../api/subsonicTypes';
|
||
import { isSmartPlaylistName } from '../utils/playlist/playlistsSmart';
|
||
|
||
/**
|
||
* Build the 2×2 cover collage for each smart playlist. Pulls each smart
|
||
* playlist's tracks (filtered to the active library scope) and collects up
|
||
* to four unique cover-art IDs. Re-runs when the playlist list changes or
|
||
* when the active library filter version bumps.
|
||
*/
|
||
export function useSmartCoverCollage(
|
||
playlists: SubsonicPlaylist[],
|
||
musicLibraryFilterVersion: number,
|
||
): Record<string, string[]> {
|
||
const [smartCoverIdsByPlaylist, setSmartCoverIdsByPlaylist] = useState<Record<string, string[]>>({});
|
||
|
||
useEffect(() => {
|
||
let cancelled = false;
|
||
const run = async () => {
|
||
const smart = playlists.filter(pl => isSmartPlaylistName(pl.name));
|
||
if (smart.length === 0) {
|
||
if (!cancelled) setSmartCoverIdsByPlaylist({});
|
||
return;
|
||
}
|
||
const rows = await Promise.all(
|
||
smart.map(async (pl) => {
|
||
try {
|
||
const { songs } = await getPlaylist(pl.id);
|
||
const filtered = await filterSongsToActiveLibrary(songs);
|
||
const ids: string[] = [];
|
||
const seen = new Set<string>();
|
||
for (const s of filtered) {
|
||
const cid = s.coverArt;
|
||
if (!cid || seen.has(cid)) continue;
|
||
seen.add(cid);
|
||
ids.push(cid);
|
||
if (ids.length >= 4) break;
|
||
}
|
||
return [pl.id, ids] as const;
|
||
} catch {
|
||
return [pl.id, [] as string[]] as const;
|
||
}
|
||
}),
|
||
);
|
||
if (cancelled) return;
|
||
const next: Record<string, string[]> = {};
|
||
for (const [id, ids] of rows) next[id] = ids;
|
||
setSmartCoverIdsByPlaylist(next);
|
||
};
|
||
run();
|
||
return () => { cancelled = true; };
|
||
}, [playlists, musicLibraryFilterVersion]);
|
||
|
||
return smartCoverIdsByPlaylist;
|
||
}
|