(track)
| 248 | // ─── Track management ─── |
| 249 | |
| 250 | export function addTrackToLibrary(track) { |
| 251 | // track: { id, title, channel, thumb, stems, status, sourceUrl } |
| 252 | const existingId = findTrackBySource(track.sourceUrl, track.id); |
| 253 | if (existingId) { |
| 254 | const trash = getTrashFolder(); |
| 255 | const inTrash = trash?.items.includes(existingId); |
| 256 | if (inTrash) { |
| 257 | // Old track was trashed — delete it silently so the new import lands |
| 258 | // in the library instead of inheriting the trash placement. |
| 259 | delete tracks[existingId]; |
| 260 | for (const f of folders) f.items = f.items.filter((id) => id !== existingId); |
| 261 | } else { |
| 262 | replaceTrackId(existingId, track.id); |
| 263 | } |
| 264 | } |
| 265 | const existing = tracks[track.id] || {}; |
| 266 | tracks[track.id] = { |
| 267 | ...existing, |
| 268 | ...track, |
| 269 | createdAt: existing.createdAt ?? track.createdAt ?? (Date.now() / 1000), |
| 270 | favorite: existing.favorite ?? false, |
| 271 | }; |
| 272 | const alreadyPlaced = folders.some((folder) => folder.items.includes(track.id)); |
| 273 | if (!alreadyPlaced) { |
| 274 | // Put into first non-trash folder or create an "Unsorted" folder. |
| 275 | let target = folders.find((folder) => folder.id !== TRASH_ID); |
| 276 | if (!target) { |
| 277 | target = makeFolder({ id: "f-unsorted", name: "Unsorted" }); |
| 278 | folders.unshift(target); |
| 279 | } |
| 280 | target.items.unshift(track.id); |
| 281 | } |
| 282 | saveState(); |
| 283 | render(); |
| 284 | } |
| 285 | |
| 286 | export function updateTrackStatus(trackId, status) { |
| 287 | if (tracks[trackId]) { |
no test coverage detected