mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Tracks can be added to playlists via drag
This commit is contained in:
1 parent
b154df430a
commit
89c7dade25
8 files changed
+246
-23
No files matched your search
@@ -0,0 +1,69 @@
|
||||
import type { QueryClient } from '@tanstack/react-query'
|
||||
import { api } from '../api'
|
||||
import { askChoice } from './confirm'
|
||||
|
||||
const plural = (n: number) => `${n} track${n === 1 ? '' : 's'}`
|
||||
|
||||
/**
|
||||
* Add tracks to a playlist — the context menu's "Add to" and a drop on a
|
||||
* sidebar playlist alike, so the two cannot drift.
|
||||
*
|
||||
* Tracks the playlist already holds are ASKED about (decided 2026-10-06), but
|
||||
* only where the platform can hold a track twice (`playlists.duplicates`);
|
||||
* elsewhere they are skipped and the message says so. Nothing is asked when
|
||||
* there are none.
|
||||
*
|
||||
* `moveFrom` is an Option-drag out of another (editable) playlist: once the
|
||||
* tracks are in the target they leave the source. Cancelling the duplicates
|
||||
* question cancels the move too.
|
||||
*
|
||||
* Resolves the message to show, or null when the user cancelled.
|
||||
*/
|
||||
export async function addTracksToPlaylist(opts: {
|
||||
qc: QueryClient
|
||||
playlist: { id: string; name: string }
|
||||
ids: string[]
|
||||
duplicatesAllowed: boolean
|
||||
moveFrom?: string
|
||||
}): Promise<string | null> {
|
||||
const { qc, playlist, ids, duplicatesAllowed, moveFrom } = opts
|
||||
let res = await api.addEntries(playlist.id, ids, duplicatesAllowed ? 'ask' : 'skip')
|
||||
if (res.status === 'duplicates') {
|
||||
const fresh = ids.length - res.duplicates
|
||||
const all = fresh === 0
|
||||
const choice = await askChoice({
|
||||
title: all
|
||||
? `${ids.length === 1 ? 'This track is' : `All ${ids.length} tracks are`} already in “${playlist.name}”`
|
||||
: `${plural(res.duplicates)} ${res.duplicates === 1 ? 'is' : 'are'} already in “${playlist.name}”`,
|
||||
body: all
|
||||
? ids.length === 1
|
||||
? 'Add it again, so the playlist holds it twice?'
|
||||
: 'Add them again, so the playlist holds them twice?'
|
||||
: `Add only the ${plural(fresh)} not there yet, or add every track — the ${res.duplicates === 1 ? 'one already there' : `${res.duplicates} already there`} a second time?`,
|
||||
// All of them there: "skip" would add nothing, which is just Cancel —
|
||||
// so the question is only whether to add them again.
|
||||
confirmLabel: all ? 'Add again' : `Add ${plural(fresh)}`,
|
||||
altLabel: all ? undefined : `Add all ${ids.length}`,
|
||||
tone: 'primary',
|
||||
})
|
||||
if (choice === null) return null
|
||||
res = await api.addEntries(playlist.id, ids, choice === 'alt' || all ? 'add' : 'skip')
|
||||
}
|
||||
|
||||
let moved = false
|
||||
if (moveFrom && moveFrom !== playlist.id) {
|
||||
const gone = new Set(ids)
|
||||
const current = await api.playlistTracks(moveFrom)
|
||||
await api.setEntries(moveFrom, current.map((t) => t.id).filter((id) => !gone.has(id)))
|
||||
qc.invalidateQueries({ queryKey: ['playlist', moveFrom] })
|
||||
moved = true
|
||||
}
|
||||
qc.invalidateQueries({ queryKey: ['state'] })
|
||||
qc.invalidateQueries({ queryKey: ['playlists'] })
|
||||
qc.invalidateQueries({ queryKey: ['playlist', playlist.id] })
|
||||
|
||||
const skipped = ids.length - res.added
|
||||
if (moved) return `Moved ${plural(ids.length)} to ${playlist.name}`
|
||||
if (res.added === 0) return `Nothing added — ${ids.length === 1 ? 'it is' : 'they are all'} already in ${playlist.name}`
|
||||
return `Added ${plural(res.added)} to ${playlist.name}${skipped > 0 ? ` · ${skipped} already there` : ''}`
|
||||
}
|
||||
@@ -12,19 +12,26 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { ConfirmDialog, type ConfirmRequest } from '../components/ConfirmDialog'
|
||||
|
||||
type Ask = Omit<ConfirmRequest, 'onConfirm'>
|
||||
type Pending = { req: Ask; resolve: (ok: boolean) => void }
|
||||
type Ask = Omit<ConfirmRequest, 'onConfirm' | 'onAlt' | 'altLabel'>
|
||||
type Choice = 'confirm' | 'alt' | null
|
||||
type Pending = { req: Ask & { altLabel?: string }; resolve: (choice: Choice) => void }
|
||||
|
||||
let open: ((p: Pending) => void) | null = null
|
||||
|
||||
/** Show a confirmation and resolve true (confirmed) or false (cancelled). */
|
||||
export function askConfirm(req: Ask): Promise<boolean> {
|
||||
return askChoice(req).then((c) => c === 'confirm')
|
||||
}
|
||||
|
||||
/** A confirmation with a SECOND way to go ahead (`altLabel`): resolves
|
||||
* 'confirm', 'alt', or null when cancelled. */
|
||||
export function askChoice(req: Ask & { altLabel?: string }): Promise<Choice> {
|
||||
return new Promise((resolve) => {
|
||||
if (open) open({ req, resolve })
|
||||
// No host mounted (should not happen outside tests): refuse rather than
|
||||
// fall back to a native dialog, since a destructive action must not go
|
||||
// ahead unconfirmed.
|
||||
else resolve(false)
|
||||
else resolve(null)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -34,7 +41,7 @@ export function ConfirmHost() {
|
||||
useEffect(() => {
|
||||
open = (p) =>
|
||||
setPending((prev) => {
|
||||
prev?.resolve(false) // a second ask supersedes an unanswered one
|
||||
prev?.resolve(null) // a second ask supersedes an unanswered one
|
||||
return p
|
||||
})
|
||||
return () => {
|
||||
@@ -45,10 +52,11 @@ export function ConfirmHost() {
|
||||
return (
|
||||
<ConfirmDialog
|
||||
{...pending.req}
|
||||
// A promise settles once, so confirm-then-close resolves `true`.
|
||||
onConfirm={() => pending.resolve(true)}
|
||||
// A promise settles once, so confirm-then-close resolves 'confirm'.
|
||||
onConfirm={() => pending.resolve('confirm')}
|
||||
onAlt={() => pending.resolve('alt')}
|
||||
onClose={() => {
|
||||
pending.resolve(false)
|
||||
pending.resolve(null)
|
||||
setPending(null)
|
||||
}}
|
||||
/>
|
||||
|
||||
@@ -204,6 +204,34 @@ export function beginDrag(payload: DragPayload, at: { x: number; y: number; alt:
|
||||
evaluate()
|
||||
}
|
||||
|
||||
/** A pointerdown handler that starts a drag once the press moves 6 px. A plain
|
||||
* click never moves that far, so the element keeps its click; a press on a
|
||||
* field inside it (a rename input) never drags. `make` is asked at the moment
|
||||
* the drag starts and may return null to refuse. */
|
||||
export function usePressToDrag(make: () => DragPayload | null): (e: React.PointerEvent) => void {
|
||||
const makeRef = useRef(make)
|
||||
makeRef.current = make
|
||||
return useCallback((e: React.PointerEvent) => {
|
||||
if (e.button !== 0 || e.shiftKey || e.metaKey || e.ctrlKey) return
|
||||
if ((e.target as HTMLElement).closest('input, textarea, select')) return
|
||||
const start = { x: e.clientX, y: e.clientY }
|
||||
const onMove = (m: PointerEvent) => {
|
||||
if (Math.hypot(m.clientX - start.x, m.clientY - start.y) < 6) return
|
||||
stop()
|
||||
const payload = makeRef.current()
|
||||
if (payload) beginDrag(payload, { x: m.clientX, y: m.clientY, alt: m.altKey })
|
||||
}
|
||||
const stop = () => {
|
||||
window.removeEventListener('pointermove', onMove)
|
||||
window.removeEventListener('pointerup', stop)
|
||||
window.removeEventListener('pointercancel', stop)
|
||||
}
|
||||
window.addEventListener('pointermove', onMove)
|
||||
window.addEventListener('pointerup', stop)
|
||||
window.addEventListener('pointercancel', stop)
|
||||
}, [])
|
||||
}
|
||||
|
||||
/** The drag in progress, or null. Changes only when a drag starts or ends. */
|
||||
export function useDragPayload(): DragPayload | null {
|
||||
return useSyncExternalStore(subscribe, () => state.payload)
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import type { PlaylistNode } from '../api'
|
||||
|
||||
/** Every selectable playlist at or under a node, in tree order. A folder
|
||||
* contributes its nested playlists, a playlist itself — which is what adding a
|
||||
* node to an export means: the export stores playlist ids, and a folder is a
|
||||
* shape in the tree, not a thing with tracks. */
|
||||
export function playlistIdsUnder(node: PlaylistNode): string[] {
|
||||
const here = node.kind === 'folder' ? [] : [node.id]
|
||||
return [...here, ...(node.children ?? []).flatMap(playlistIdsUnder)]
|
||||
}
|
||||
Reference in new issue
Block a user