Implemented subtractive playlist editing

This commit is contained in:
patben8 committed 2026-10-22 09:58:57 +02:00
1 parent 1a28ad6102
commit 068e4da08b
7 files changed
+138 -4

No files matched your search

+67
View File
@@ -368,6 +368,13 @@ export default function App() {
nodes.flatMap((n) => [...(n.can_add_tracks ? [n] : []), ...walk(n.children)])
return walk(playlists.data ?? [])
}, [playlists.data])
// Every playlist Remove ▸ From playlist can take tracks out of: the same
// gate as removing from the open one (its entry list is rewritten).
const removablePlaylists = useMemo(() => {
const walk = (nodes: PlaylistNode[]): PlaylistNode[] =>
nodes.flatMap((n) => [...(n.can_reorder ? [n] : []), ...walk(n.children)])
return canEdit ? walk(playlists.data ?? []) : []
}, [playlists.data, canEdit])
// Export membership. Both are no-ops on the library itself, so neither dirties
// it nor touches version history — an export set is Konduktor's own data.
@@ -1094,6 +1101,50 @@ export default function App() {
},
})
// Remove ▸ From playlist ▸ <name>: tracks picked anywhere leave a playlist
// that is not open — every copy of each. Counted first, so the dialog says
// how many of the selection it actually holds (and still answers at 0).
const confirmRemoveFromOther = async (p: PlaylistNode, ids: string[]) => {
let r
try {
r = await api.removeEntries(p.id, ids, true)
} catch (e) {
onError((e as Error).message)
return
}
const name = <span className="text-text">{p.name}</span>
const held = r.tracks
setConfirmReq({
title: 'Remove from playlist',
body: !held ? (
<p>
{ids.length === 1 ? <>{trackNoun(ids)} is not</> : <>None of these {ids.length} tracks are</>} in {name}.
</p>
) : (
<>
<p>
Remove {held === ids.length ? trackNoun(ids) : `${held} of the ${ids.length} selected tracks`} from {name}?
The {held === 1 ? 'track stays' : 'tracks stay'} in the collection.
</p>
{held < ids.length && (
<p className="text-faint">The other {nTracks(ids.length - held)} {ids.length - held === 1 ? 'is' : 'are'} not in it.</p>
)}
{r.removed > held && <p className="text-faint">Some are in it more than once; every copy goes.</p>}
</>
),
confirmLabel: held ? `Remove ${nTracks(held)}` : 'Remove',
confirmDisabled: !held,
onConfirm: () =>
guard(async () => {
const done = await api.removeEntries(p.id, ids)
qc.invalidateQueries({ queryKey: ['playlist', p.id] })
qc.invalidateQueries({ queryKey: ['playlists'] })
qc.invalidateQueries({ queryKey: ['state'] })
notify('success', `Removed ${nTracks(done.tracks)} from ${p.name}`)
}),
})
}
const confirmRemoveFromExport = (exportId: string, ids: string[]) => {
const name = exportSets.data?.find((x) => x.id === exportId)?.name ?? 'this export'
setConfirmReq({
@@ -1231,6 +1282,22 @@ export default function App() {
...(playlistEditable
? [{ label: 'From this playlist…', onClick: () => confirmRemoveFromPlaylist(ids) }]
: []),
// Any other editable playlist — "A minus B" is: open B, select all, here A.
...(() => {
// A device's ids mean nothing to the collection's playlists.
if (viewingDevice) return []
const lists = removablePlaylists.filter((p) => !(source.kind === 'playlist' && p.id === source.id))
return lists.length
? [{
label: 'From playlist',
submenu: lists.map((p) => ({
label: p.name,
hint: String(p.count),
onClick: () => void confirmRemoveFromOther(p, ids),
})),
}]
: []
})(),
// Only on the export's ROOT view. Inside a referenced playlist there is
// nothing to remove: the reference is live, and per-track removal would
// need an exclusion list — hidden state deciding what a future export
+8
View File
@@ -1272,6 +1272,14 @@ export const api = {
`/api/playlists/${encodeURIComponent(nodeId)}/add`,
{ track_ids: trackIds, duplicates, index },
),
/** Remove every occurrence of these tracks. `dryRun` changes nothing and
* counts: `tracks` = how many of them it holds, `removed` = entries. */
removeEntries: (nodeId: string, trackIds: string[], dryRun = false) =>
send<{ status: 'removed' | 'counted'; tracks: number; removed: number; count: number }>(
'POST',
`/api/playlists/${encodeURIComponent(nodeId)}/remove`,
{ track_ids: trackIds, dry_run: dryRun },
),
/** Move a tree node to `index` among `parentId`'s children (null = top
* level), counted without it. Answers its id AFTER the move. */
movePlaylist: (nodeId: string, parentId: string | null, index: number) =>
+6 -3
View File
@@ -14,6 +14,9 @@ export interface ConfirmRequest {
* (e.g. "Add them again" next to "Add 6 new"). Enter still means confirm. */
altLabel?: string
onAlt?: () => void
/** Nothing to confirm (e.g. none of the tracks are there): the dialog
* still answers, with the confirm button — and Enter — off. */
confirmDisabled?: boolean
onConfirm: () => Promise<void> | void
}
@@ -23,11 +26,11 @@ interface Props extends ConfirmRequest {
/** A yes/no confirmation for destructive actions. Enter confirms and Esc
* cancels, so a confirmed Delete key stays a two-keystroke action. */
export function ConfirmDialog({ title, body, confirmLabel, tone = 'danger', altLabel, onAlt, onConfirm, onClose }: Props) {
export function ConfirmDialog({ title, body, confirmLabel, tone = 'danger', altLabel, onAlt, confirmDisabled, onConfirm, onClose }: Props) {
const [busy, setBusy] = useState(false)
const confirm = async () => {
if (busy) return
if (busy || confirmDisabled) return
setBusy(true)
try {
await onConfirm()
@@ -87,7 +90,7 @@ export function ConfirmDialog({ title, body, confirmLabel, tone = 'danger', altL
)}
<button
onClick={() => void confirm()}
disabled={busy}
disabled={busy || confirmDisabled}
className={`rounded-full px-4 py-1.5 text-sm font-semibold disabled:opacity-50 ${
tone === 'primary'
? 'btn-primary'