fix(playlists): modify full playlist tree when adding tracks (avoid structure-only overwrite); then reload structure for counters
This commit is contained in:
parent
5a6710b0eb
commit
0c8e00389b
@ -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);
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user