Update script.js
This commit is contained in:
parent
5d02ee16b0
commit
3c8e728cd6
1 changed files with 16 additions and 3 deletions
|
|
@ -2622,10 +2622,13 @@ function startModalDownloadPolling(playlistId) {
|
||||||
(data.tasks || []).forEach(task => {
|
(data.tasks || []).forEach(task => {
|
||||||
const row = document.querySelector(`#download-missing-modal-${playlistId} tr[data-track-index="${task.track_index}"]`);
|
const row = document.querySelector(`#download-missing-modal-${playlistId} tr[data-track-index="${task.track_index}"]`);
|
||||||
if (!row) return;
|
if (!row) return;
|
||||||
|
|
||||||
|
// Stronger protection: Don't override locally cancelled tracks with any backend updates
|
||||||
if (row.dataset.locallyCancelled === 'true') {
|
if (row.dataset.locallyCancelled === 'true') {
|
||||||
failedOrCancelledCount++;
|
failedOrCancelledCount++;
|
||||||
return;
|
return; // Completely skip processing this task to avoid any UI conflicts
|
||||||
}
|
}
|
||||||
|
|
||||||
row.dataset.taskId = task.task_id;
|
row.dataset.taskId = task.task_id;
|
||||||
const statusEl = document.getElementById(`download-${playlistId}-${task.track_index}`);
|
const statusEl = document.getElementById(`download-${playlistId}-${task.track_index}`);
|
||||||
const actionsEl = document.getElementById(`actions-${playlistId}-${task.track_index}`);
|
const actionsEl = document.getElementById(`actions-${playlistId}-${task.track_index}`);
|
||||||
|
|
@ -2834,15 +2837,20 @@ async function cancelTrackDownload(playlistId, trackIndex) {
|
||||||
const row = document.querySelector(`#download-missing-modal-${playlistId} tr[data-track-index="${trackIndex}"]`);
|
const row = document.querySelector(`#download-missing-modal-${playlistId} tr[data-track-index="${trackIndex}"]`);
|
||||||
if (!row) return;
|
if (!row) return;
|
||||||
|
|
||||||
|
// Prevent double cancellation
|
||||||
|
if (row.dataset.locallyCancelled === 'true') {
|
||||||
|
return; // Already cancelled locally
|
||||||
|
}
|
||||||
|
|
||||||
const taskId = row.dataset.taskId;
|
const taskId = row.dataset.taskId;
|
||||||
if (!taskId) {
|
if (!taskId) {
|
||||||
showToast('Task not started yet, cannot cancel.', 'warning');
|
showToast('Task not started yet, cannot cancel.', 'warning');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// UI update for immediate feedback
|
// UI update for immediate feedback - mark as cancelled FIRST to prevent race conditions
|
||||||
row.dataset.locallyCancelled = 'true';
|
row.dataset.locallyCancelled = 'true';
|
||||||
document.getElementById(`download-${playlistId}-${trackIndex}`).textContent = '🚫 Cancelled';
|
document.getElementById(`download-${playlistId}-${trackIndex}`).textContent = '🚫 Cancelling...';
|
||||||
document.getElementById(`actions-${playlistId}-${trackIndex}`).innerHTML = '-';
|
document.getElementById(`actions-${playlistId}-${trackIndex}`).innerHTML = '-';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
@ -2853,11 +2861,16 @@ async function cancelTrackDownload(playlistId, trackIndex) {
|
||||||
});
|
});
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
|
// Update final UI state after successful cancellation
|
||||||
|
document.getElementById(`download-${playlistId}-${trackIndex}`).textContent = '🚫 Cancelled';
|
||||||
showToast('Download cancelled and added to wishlist.', 'info');
|
showToast('Download cancelled and added to wishlist.', 'info');
|
||||||
} else {
|
} else {
|
||||||
throw new Error(data.error);
|
throw new Error(data.error);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// Reset UI state if cancellation failed
|
||||||
|
row.dataset.locallyCancelled = 'false';
|
||||||
|
document.getElementById(`download-${playlistId}-${trackIndex}`).textContent = '❌ Cancel Failed';
|
||||||
showToast(`Could not cancel task: ${error.message}`, 'error');
|
showToast(`Could not cancel task: ${error.message}`, 'error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue