fix(playlists): modify full playlist tree when adding tracks (avoid structure-only overwrite); then reload structure for counters

This commit is contained in:
Geert Rademakes 2025-08-08 13:29:58 +02:00
parent 5a6710b0eb
commit 0c8e00389b

View File

@ -232,31 +232,27 @@ const RekordboxReader: React.FC = () => {
}; };
const handleAddSongsToPlaylist = async (songIds: string[], playlistName: string) => { const handleAddSongsToPlaylist = async (songIds: string[], playlistName: string) => {
const updatedPlaylists = playlists.map(node => { // Fetch FULL playlists to avoid losing tracks (structure view strips them)
if (node.name === playlistName && node.type === 'playlist') { const fullTree = await api.getPlaylists();
return {
...node, const applyAdd = (nodes: PlaylistNode[]): PlaylistNode[] => {
tracks: [...new Set([...(node.tracks || []), ...songIds])] return nodes.map(node => {
}; if (node.type === 'playlist' && node.name === playlistName) {
const current = Array.isArray(node.tracks) ? node.tracks : [];
const merged = Array.from(new Set([...current, ...songIds]));
return { ...node, tracks: merged };
} }
if (node.type === 'folder' && node.children) { if (node.type === 'folder' && node.children) {
return { return { ...node, children: applyAdd(node.children) };
...node,
children: node.children.map(child => {
if (child.name === playlistName && child.type === 'playlist') {
return {
...child,
tracks: [...new Set([...(child.tracks || []), ...songIds])]
};
}
return child;
})
};
} }
return node; return node;
}); });
await api.savePlaylists(updatedPlaylists); };
// Always normalize state to structure for consistent counters
const updatedFullTree = applyAdd(fullTree);
await api.savePlaylists(updatedFullTree);
// Reload structure for UI counters
const structure = await api.getPlaylistStructure(); const structure = await api.getPlaylistStructure();
setPlaylists(structure); setPlaylists(structure);
}; };