From 0c8e00389b13464c908eed9f4ce9e637aa3eae2b Mon Sep 17 00:00:00 2001 From: Geert Rademakes Date: Fri, 8 Aug 2025 13:29:58 +0200 Subject: [PATCH] fix(playlists): modify full playlist tree when adding tracks (avoid structure-only overwrite); then reload structure for counters --- packages/frontend/src/App.tsx | 46 ++++++++++++++++------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/packages/frontend/src/App.tsx b/packages/frontend/src/App.tsx index 194b27c..3b54475 100644 --- a/packages/frontend/src/App.tsx +++ b/packages/frontend/src/App.tsx @@ -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); };