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 updatedPlaylists = playlists.map(node => {
if (node.name === playlistName && node.type === 'playlist') {
return {
...node,
tracks: [...new Set([...(node.tracks || []), ...songIds])]
};
}
if (node.type === 'folder' && node.children) {
return {
...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;
});
await api.savePlaylists(updatedPlaylists);
// Always normalize state to structure for consistent counters
// Fetch FULL playlists to avoid losing tracks (structure view strips them)
const fullTree = await api.getPlaylists();
const applyAdd = (nodes: PlaylistNode[]): PlaylistNode[] => {
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) {
return { ...node, children: applyAdd(node.children) };
}
return node;
});
};
const updatedFullTree = applyAdd(fullTree);
await api.savePlaylists(updatedFullTree);
// Reload structure for UI counters
const structure = await api.getPlaylistStructure();
setPlaylists(structure);
};