Add confirmation imodal when adding duplicate tracks to a playlist

Use similar copy as official iOS client
This commit is contained in:
Joel C committed 2026-02-09 21:26:40 +01:00
1 parent e48bbb99df
commit 512a411292
6 files changed
+248 -6

No files matched your search

@@ -0,0 +1,88 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import Modal from './Modal.svelte';
import type { PlaylistDuplicateResult } from '$lib/types/index';
export let isOpen = false;
export let duplicateResult: PlaylistDuplicateResult | null = null;
export let loading = false;
const dispatch = createEventDispatcher();
function handleAddAll() {
dispatch('addAll');
}
function handleSkipDuplicates() {
dispatch('skipDuplicates');
}
function handleCancel() {
dispatch('cancel');
}
function handleClose() {
if (!loading) {
dispatch('cancel');
}
}
</script>
<Modal {isOpen} onClose={handleClose} title={'You\'ve added some of these tracks before'}>
{#if duplicateResult}
<div class="duplicate-info">
<p>
This playlist already contains <strong>{duplicateResult.duplicate_count}</strong> of the
track{duplicateResult.duplicate_count !== 1 ? 's' : ''} you're adding.
Add only the new ones, or add everything including duplicates ({duplicateResult.total_tracks} track{duplicateResult.total_tracks !== 1 ? 's' : ''}).
</p>
</div>
{/if}
{#snippet footer()}
<div class="footer-right">
<button
class="btn btn-secondary"
on:click={handleAddAll}
disabled={loading}
>
{#if loading}
Adding...
{:else}
Add all tracks
{/if}
</button>
<button
class="btn btn-primary"
on:click={handleSkipDuplicates}
disabled={loading || duplicateResult?.duplicate_count === 0}
>
{#if loading}
Adding...
{:else}
Add only new tracks
{/if}
</button>
</div>
{/snippet}
</Modal>
<style>
.duplicate-info {
margin-bottom: 24px;
}
.duplicate-info p {
margin: 0;
color: var(--text-secondary);
line-height: 1.4;
}
.footer-right {
display: flex;
align-items: center;
gap: 12px;
margin-left: auto;
}
</style>
+100 -5
View File
@@ -5,6 +5,8 @@
import { subscribe as subscribeOffline, getStatus, createPendingPlaylist } from '$lib/stores/offlineStore';
import { showToast } from '$lib/stores/toastStore';
import { t } from '$lib/i18n';
import PlaylistDuplicateConfirmModal from './PlaylistDuplicateConfirmModal.svelte';
import type { PlaylistDuplicateResult } from '$lib/types/index';
import {
subscribe as subscribeFolders,
getVisibleFolders,
@@ -76,6 +78,10 @@
let localTrackCounts = $state<Map<number, number>>(new Map());
let showDeleteConfirm = $state(false);
// State for duplicate checking
let showDuplicateConfirm = $state(false);
let duplicateResult = $state<PlaylistDuplicateResult | null>(null);
// Load local track counts
async function loadLocalTrackCounts() {
try {
@@ -340,6 +346,48 @@
return;
}
// For pending playlists and local tracks add directly
if (selectedPlaylistId < 0 || isLocalTracks) {
await handleDirectAddToPlaylist();
return;
}
loading = true;
error = null;
try {
// Check for duplicates in regular playlists with Qobuz tracks
const result = await invoke<PlaylistDuplicateResult>('check_playlist_duplicates', {
playlistId: selectedPlaylistId,
trackIds
});
duplicateResult = {
...result,
duplicate_track_ids: new Set(result.duplicate_track_ids)
};
if (duplicateResult.duplicate_count > 0) {
// There are duplicates. Show confirmation dialog
// to allow the user to decide on what action to take
showDuplicateConfirm = true;
} else {
await executeAddTracks(false);
}
} catch (err) {
console.error('Failed to check playlist duplicates:', err);
error = String(err);
} finally {
loading = false;
}
}
async function handleDirectAddToPlaylist() {
if (!selectedPlaylistId || trackIds.length === 0) {
error = 'Please select a playlist';
return;
}
loading = true;
error = null;
@@ -382,11 +430,7 @@
}
} else {
// Regular playlist with Qobuz tracks
await invoke('add_tracks_to_playlist', {
playlistId: selectedPlaylistId,
trackIds
});
void logPlaylistAdd(trackIds, selectedPlaylistId);
await executeAddTracks(false);
}
onSuccess?.();
onClose();
@@ -398,6 +442,48 @@
}
}
async function executeAddTracks(skipDuplicates: boolean) {
try {
let tracksToAdd = trackIds;
if (skipDuplicates) {
tracksToAdd = trackIds.filter(id => !duplicateResult?.duplicate_track_ids.has(id));
}
if (tracksToAdd.length === 0) {
onSuccess?.();
onClose();
showDuplicateConfirm = false;
return;
}
await invoke('add_tracks_to_playlist', {
playlistId: selectedPlaylistId!,
trackIds: tracksToAdd
});
void logPlaylistAdd(trackIds, selectedPlaylistId!);
onSuccess?.();
onClose();
showDuplicateConfirm = false;
} catch (err) {
console.error('Failed to add tracks to playlist:', err);
error = String(err);
}
}
function handleDuplicateAddAll() {
executeAddTracks(false);
}
function handleDuplicateSkipDuplicates() {
executeAddTracks(true);
}
function handleDuplicateCancel() {
showDuplicateConfirm = false;
duplicateResult = null;
}
async function handleCreateAndAdd() {
if (!name.trim()) {
error = 'Please enter a playlist name';
@@ -733,6 +819,15 @@
</div>
{/if}
<PlaylistDuplicateConfirmModal
bind:isOpen={showDuplicateConfirm}
bind:duplicateResult={duplicateResult}
bind:loading={loading}
on:addAll={handleDuplicateAddAll}
on:skipDuplicates={handleDuplicateSkipDuplicates}
on:cancel={handleDuplicateCancel}
/>
<style>
.modal-overlay {
position: fixed;
+6
View File
@@ -113,6 +113,12 @@ export interface QobuzPlaylist {
duration?: number;
}
export interface PlaylistDuplicateResult {
total_tracks: number;
duplicate_count: number;
duplicate_track_ids: Set<number>;
}
export interface QobuzArtist {
id: number;
name: string;