Tracks can be added to playlists via drag

This commit is contained in:
patben8 committed 2026-10-06 22:41:29 +02:00
1 parent b154df430a
commit 89c7dade25
8 files changed
+246 -23

No files matched your search

+17 -1
View File
@@ -10,6 +10,10 @@ export interface ConfirmRequest {
/** `danger` (default) for destructive actions — a pink button; `primary`
* for a caution that is not destruction, e.g. switching library. */
tone?: 'danger' | 'primary'
/** A second way to go ahead, as a plain button beside the confirm one
* (e.g. "Add them again" next to "Add 6 new"). Enter still means confirm. */
altLabel?: string
onAlt?: () => void
onConfirm: () => Promise<void> | void
}
@@ -19,7 +23,7 @@ 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', onConfirm, onClose }: Props) {
export function ConfirmDialog({ title, body, confirmLabel, tone = 'danger', altLabel, onAlt, onConfirm, onClose }: Props) {
const [busy, setBusy] = useState(false)
const confirm = async () => {
@@ -69,6 +73,18 @@ export function ConfirmDialog({ title, body, confirmLabel, tone = 'danger', onCo
>
Cancel
</button>
{altLabel && onAlt && (
<button
onClick={() => {
onAlt()
onClose()
}}
disabled={busy}
className="btn-glass rounded-full px-4 py-1.5 text-sm text-text disabled:opacity-40"
>
{altLabel}
</button>
)}
<button
onClick={() => void confirm()}
disabled={busy}
+46 -1
View File
@@ -6,6 +6,8 @@ import { ExportRunDialog } from './ExportRunDialog'
import type { Source } from './Sidebar'
import { askConfirm } from '../lib/confirm'
import { Icon } from '../lib/icons'
import { useDropTarget, type DragPayload } from '../lib/drag'
import { playlistIdsUnder } from '../lib/playlistTree'
/**
* Exports in the sidebar: each one a named, persisted slice of the library
@@ -105,6 +107,7 @@ export function ExportsSection({ source, onSelect, onDone, onError }: Props) {
onSelect={onSelect}
onEdit={() => setDialog({ editing: set })}
onRun={() => setRunning(set)}
onDone={onDone}
onDelete={async () => {
// Deleting a set never touches its destination folder — the files
// already exported are the user's, not Konduktor's to clean up.
@@ -132,6 +135,7 @@ function ExportRow({
onEdit,
onRun,
onDelete,
onDone,
onError,
}: {
set: ExportSet
@@ -140,6 +144,7 @@ function ExportRow({
onEdit: () => void
onRun: () => void
onDelete: () => void
onDone: (msg: string) => void
onError: (msg: string) => void
}) {
const qc = useQueryClient()
@@ -170,11 +175,51 @@ function ExportRow({
const data = contents.data
const expanded = open || inside
// A drop target for the collection's tracks (they become loose tracks) and
// for playlists or folders dragged from the tree (referenced, live, as
// "Add to export" does). Not gated on the library being writable: an export
// set is Konduktor's own data.
const refresh = () => {
qc.invalidateQueries({ queryKey: ['export-contents', set.id] })
qc.invalidateQueries({ queryKey: ['export-tracks', set.id] })
qc.invalidateQueries({ queryKey: ['export-loose', set.id] })
}
const accepts = (p: DragPayload) =>
p.kind === 'tracks' ? p.origin === 'collection' : playlistIdsUnder(p.node).length > 0
const [dropRef, over] = useDropTarget<true>({
hint: (p) => (accepts(p) ? true : null),
label: () => `Add to export ${set.name}`,
onDrop: async (p) => {
try {
if (p.kind === 'tracks') {
await api.addToExport(set.id, { track_ids: p.ids })
onDone(`Added ${p.ids.length} track${p.ids.length === 1 ? '' : 's'} to ${set.name}`)
} else {
const ids = playlistIdsUnder(p.node)
await api.addToExport(set.id, { playlist_ids: ids })
onDone(
p.node.kind === 'folder'
? `Added ${ids.length} playlist${ids.length === 1 ? '' : 's'} from “${p.node.name}” to ${set.name}`
: `Added “${p.node.name}” to ${set.name}`,
)
}
refresh()
} catch (e) {
onError((e as Error).message)
}
},
})
return (
<div>
<div
ref={dropRef}
className={`group flex w-full items-center gap-1 rounded-md pr-1 transition-colors ${
selected ? 'is-selected text-text' : 'text-muted hover:bg-ink-800 hover:text-text'
over
? 'bg-gold/10 text-text ring-1 ring-gold'
: selected
? 'is-selected text-text'
: 'text-muted hover:bg-ink-800 hover:text-text'
}`}
>
<button
+54 -8
View File
@@ -11,6 +11,9 @@ import { ConfirmDialog, type ConfirmRequest } from './ConfirmDialog'
import { ContextMenu, type MenuItem } from './ContextMenu'
import { Icon, type IconName } from '../lib/icons'
import { PlatformIcon } from '../lib/platformIcons'
import { addTracksToPlaylist } from '../lib/addTracks'
import { usePressToDrag, useDropTarget, type DragPayload } from '../lib/drag'
import { playlistIdsUnder } from '../lib/playlistTree'
/**
* Which view the main table is showing.
@@ -57,11 +60,6 @@ interface Props {
// Kind picks the icon; every behavioural question is answered by the node's own
// flags, so nothing here infers what a node can do from what it is called.
/** Every selectable playlist at or under a node, in tree order. */
function playlistIdsUnder(node: PlaylistNode): string[] {
const here = node.kind === 'folder' ? [] : [node.id]
return [...here, ...(node.children ?? []).flatMap(playlistIdsUnder)]
}
/** The confirm wording for deleting a node. A folder says what goes with it,
* since the whole subtree is deleted and the row only shows the folder. */
@@ -105,6 +103,11 @@ interface RowActions {
onPickExport: (e: React.MouseEvent<HTMLElement>, node: PlaylistNode) => void
onCommitDraft: (name: string) => void
onCancelDraft: () => void
/** Whether a drag of tracks may drop on this playlist. */
acceptsTracks: (node: PlaylistNode, p: Extract<DragPayload, { kind: 'tracks' }>) => boolean
/** Tracks dropped on a playlist; `alt` (Option) moves them out of the
* playlist they came from. */
onDropTracks: (node: PlaylistNode, p: Extract<DragPayload, { kind: 'tracks' }>, alt: boolean) => void
}
/** The inline name field for a node being created. Enter or clicking away
@@ -156,6 +159,7 @@ function DraftRow({
function NodeRow({
node,
parentId,
depth,
source,
onSelect,
@@ -166,6 +170,8 @@ function NodeRow({
actions,
}: {
node: PlaylistNode
/** The folder this node sits in; null = the top level. */
parentId: string | null
depth: number
source: Source
onSelect: (s: Source) => void
@@ -191,12 +197,30 @@ function NodeRow({
setRenamingId(node.id)
}
// A row is a drag SOURCE (onto an export, or elsewhere in the tree) and a
// drop TARGET for tracks. Whether it takes them is the node's own
// `can_add_tracks` plus the library being writable, decided by the sidebar.
const startDrag = usePressToDrag(() => (renaming ? null : { kind: 'node', node, parentId }))
const moves = (p: DragPayload, alt: boolean) =>
p.kind === 'tracks' && alt && !!p.fromPlaylist && p.fromPlaylist !== node.id
const [dropRef, over] = useDropTarget<true>({
hint: (p) => (p.kind === 'tracks' && actions.acceptsTracks(node, p) ? true : null),
label: (p, _h, at) => `${moves(p, at.alt) ? 'Move' : 'Add'} to ${node.name}`,
onDrop: (p, _h, at) => p.kind === 'tracks' && actions.onDropTracks(node, p, at.alt),
})
return (
<div>
<div
ref={dropRef}
onPointerDown={startDrag}
onContextMenu={(e) => actions.onContextMenu(e, node)}
className={`group flex items-center gap-1 rounded-[10px] pr-1 text-sm transition-colors ${
selected ? 'is-selected font-medium text-text' : 'text-muted hover:bg-ink-800 hover:text-text'
over
? 'bg-accent-soft/40 text-text ring-1 ring-accent'
: selected
? 'is-selected font-medium text-text'
: 'text-muted hover:bg-ink-800 hover:text-text'
}`}
>
<button
@@ -298,6 +322,7 @@ function NodeRow({
<NodeRow
key={c.id}
node={c}
parentId={node.id}
depth={depth + 1}
source={source}
onSelect={onSelect}
@@ -339,8 +364,9 @@ export function Sidebar({
}
// There is no per-node flag for "you may create a NEW playlist" — the node
// flags describe existing nodes — so this is the library-level gate.
const canCreate = useCaps().writable
const foldersSupported = useCaps().playlists.folders
const caps = useCaps()
const canCreate = caps.writable
const foldersSupported = caps.playlists.folders
const { data: playlists, isLoading } = useQuery({
queryKey: ['playlists'],
queryFn: api.playlists,
@@ -496,6 +522,24 @@ export function Sidebar({
setDraft(null)
},
onCancelDraft: () => setDraft(null),
// Only the collection's own tracks: a device's or a folder's ids mean
// nothing to the collection's playlists.
acceptsTracks: (node, p) =>
caps.writable && node.can_add_tracks && p.origin === 'collection' && p.fromPlaylist !== node.id,
onDropTracks: async (node, p, alt) => {
try {
const msg = await addTracksToPlaylist({
qc,
playlist: node,
ids: p.ids,
duplicatesAllowed: caps.playlists.duplicates,
moveFrom: alt ? p.fromPlaylist : undefined,
})
if (msg) onDone(msg)
} catch (e) {
onError((e as Error).message)
}
},
}
return (
@@ -563,6 +607,7 @@ export function Sidebar({
{/* Right-click here (below the rows) creates at the top level; rows
stop the event so they get their own menu. */}
<div
data-drag-scroll=""
className="mt-1 min-h-24 flex-1 overflow-y-auto px-2 pb-4"
onContextMenu={(e) => openMenu(e, createItems(null))}
>
@@ -572,6 +617,7 @@ export function Sidebar({
<NodeRow
key={n.id}
node={n}
parentId={null}
depth={0}
source={source}
onSelect={onSelect}