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 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 : [];
|
||||||
if (node.type === 'folder' && node.children) {
|
const merged = Array.from(new Set([...current, ...songIds]));
|
||||||
return {
|
return { ...node, tracks: merged };
|
||||||
...node,
|
}
|
||||||
children: node.children.map(child => {
|
if (node.type === 'folder' && node.children) {
|
||||||
if (child.name === playlistName && child.type === 'playlist') {
|
return { ...node, children: applyAdd(node.children) };
|
||||||
return {
|
}
|
||||||
...child,
|
return node;
|
||||||
tracks: [...new Set([...(child.tracks || []), ...songIds])]
|
});
|
||||||
};
|
};
|
||||||
}
|
|
||||||
return child;
|
const updatedFullTree = applyAdd(fullTree);
|
||||||
})
|
await api.savePlaylists(updatedFullTree);
|
||||||
};
|
|
||||||
}
|
// Reload structure for UI counters
|
||||||
return node;
|
|
||||||
});
|
|
||||||
await api.savePlaylists(updatedPlaylists);
|
|
||||||
// Always normalize state to structure for consistent counters
|
|
||||||
const structure = await api.getPlaylistStructure();
|
const structure = await api.getPlaylistStructure();
|
||||||
setPlaylists(structure);
|
setPlaylists(structure);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user