Implemented drag and drop loading

This commit is contained in:
patben8 committed 2026-10-06 23:08:34 +02:00
1 parent 7ba71981c8
commit c87444bda5
12 files changed
+267 -34

No files matched your search

+45 -3
View File
@@ -5,6 +5,7 @@ import { CapabilitiesContext, slotLabeller } from './lib/capabilities'
import { removalNote, writeHint } from './lib/platformCopy'
import { invalidateTrackLists } from './lib/trackQueries'
import { addTracksToPlaylist } from './lib/addTracks'
import { useDropTarget } from './lib/drag'
import { api, type AnalyzeOptions, type AnalyzeResult, type CueBatchResult, type AutoCueSlot, type PlaylistNode, type StemBatchResult, type Track, type TrackOrigin } from './api'
import { confirmDiscardUnsaved } from './lib/unsaved'
import {
@@ -83,6 +84,9 @@ export default function App() {
const [adding, setAdding] = useState<{ ids: string[]; target: AddTarget | null } | null>(null)
const [playRequest, setPlayRequest] = useState(0) // bump → deck loads & auto-plays
const [importing, setImporting] = useState(false)
// Device tracks dropped on a collection playlist / export: Import, with
// just those tracks and that target.
const [importDrop, setImportDrop] = useState<{ ids: string[]; target: AddTarget } | null>(null)
// Batch analysis (grid, or Auto Hotcues): a confirm step, then the running
// job, polled into the status bar. ONE at a time across both kinds — the
// backend refuses a second, since a grid run would move the beats a hotcue
@@ -102,9 +106,9 @@ export default function App() {
// Every Remove ▸ action (and the playlist Delete key) confirms first.
const [confirmReq, setConfirmReq] = useState<ConfirmRequest | null>(null)
const loadTrack = useCallback((t: Track) => {
const loadTrack = useCallback((t: Track, origin: TrackOrigin = viewOriginRef.current) => {
setPrepTrack(t)
setPrepOrigin(viewOriginRef.current)
setPrepOrigin(origin)
}, [])
const playTrack = useCallback(
(t: Track) => {
@@ -358,6 +362,22 @@ export default function App() {
},
[qc, addablePlaylists, duplicatesAllowed, notify, onError],
)
// Tracks from a device or a Files folder dropped on a collection playlist or
// export: they are not in the collection yet, so the drop opens the dialog
// that adds them — with that target filled in, and copy-vs-reference still
// asked (decided 2026-10-06).
const addForeign = useCallback(
(origin: TrackOrigin, ids: string[], target: AddTarget) => {
if (origin === 'folder') setAdding({ ids, target })
else if (origin === 'device') setImportDrop({ ids, target })
},
[],
)
const [deckDropRef, deckOver] = useDropTarget<true>({
hint: (p) => (p.kind === 'tracks' ? true : null),
label: (p) => (p.kind === 'tracks' && p.ids.length > 1 ? `Load “${p.lead.title || 'Untitled'}” into the deck` : 'Load into the deck'),
onDrop: (p) => p.kind === 'tracks' && loadTrack(p.lead, p.origin),
})
const removeFromExport = useCallback(
async (id: string, ids: string[]) => {
try {
@@ -1208,7 +1228,27 @@ export default function App() {
/>
)}
{/* Prep strip spans the top of the window; the library sits below it. */}
{importDrop && (
<ImportDialog
playlistId={null}
trackIds={importDrop.ids}
target={importDrop.target}
deviceLabel={deviceLabel}
onClose={() => setImportDrop(null)}
onDone={(msg) => {
setImportDrop(null)
notify('success', msg)
}}
onError={onError}
/>
)}
{/* Prep strip spans the top of the window; the library sits below it.
A track dropped on it loads (paused) — the row the drag started on. */}
<div ref={deckDropRef} className="relative shrink-0">
{deckOver && (
<div aria-hidden className="pointer-events-none absolute inset-0 z-10 rounded-[22px] ring-2 ring-accent" />
)}
<CapabilitiesContext.Provider value={deckCaps}>
<PrepStrip
track={prepTrack}
@@ -1220,6 +1260,7 @@ export default function App() {
onPlayingChange={setDeckPlaying}
/>
</CapabilitiesContext.Provider>
</div>
<div className="flex min-h-0 flex-1 gap-3">
<Sidebar
@@ -1231,6 +1272,7 @@ export default function App() {
onSwitchLibrary={() => setForcePicker(true)}
onOpenLibrary={(path) => void openForEditing(path)}
onDone={(msg) => notify('success', msg)}
onAddForeign={addForeign}
onOpenPathMapping={() => setShowPaths(true)}
onDiscarded={() => afterBulk(prepTrack ? [prepTrack.id] : [], true)}
/>
+4
View File
@@ -441,6 +441,10 @@ export interface ImportRequest {
playlist_ids?: string[]
/** Playlists land in a folder named this; null keeps them at the root. */
folder_name?: string | null
/** Every imported track (and any already held) also goes into this
* collection playlist / export — a drop of device tracks on one. */
into_playlist?: string | null
export_id?: string | null
}
export interface ImportPreview {
+22 -4
View File
@@ -1,6 +1,8 @@
import { useState } from 'react'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { api, type ExportSet } from '../api'
import { api, type ExportSet, type TrackOrigin } from '../api'
import type { AddTarget } from './AddFilesDialog'
import { useCaps } from '../lib/capabilities'
import { ExportDialog } from './ExportDialog'
import { ExportRunDialog } from './ExportRunDialog'
import type { Source } from './Sidebar'
@@ -29,9 +31,10 @@ interface Props {
onSelect: (s: Source) => void
onDone: (msg: string) => void
onError: (msg: string) => void
onAddForeign: (origin: TrackOrigin, ids: string[], target: AddTarget) => void
}
export function ExportsSection({ source, onSelect, onDone, onError }: Props) {
export function ExportsSection({ source, onSelect, onDone, onError, onAddForeign }: Props) {
const qc = useQueryClient()
const [dialog, setDialog] = useState<{ editing: ExportSet | null } | null>(null)
const [running, setRunning] = useState<ExportSet | null>(null)
@@ -108,6 +111,7 @@ export function ExportsSection({ source, onSelect, onDone, onError }: Props) {
onEdit={() => setDialog({ editing: set })}
onRun={() => setRunning(set)}
onDone={onDone}
onAddForeign={onAddForeign}
onDelete={async () => {
// Deleting a set never touches its destination folder — the files
// already exported are the user's, not Konduktor's to clean up.
@@ -137,6 +141,7 @@ function ExportRow({
onDelete,
onDone,
onError,
onAddForeign,
}: {
set: ExportSet
source: Source
@@ -146,8 +151,12 @@ function ExportRow({
onDelete: () => void
onDone: (msg: string) => void
onError: (msg: string) => void
onAddForeign: (origin: TrackOrigin, ids: string[], target: AddTarget) => void
}) {
const qc = useQueryClient()
// The COLLECTION's capabilities (the sidebar sits outside the browsed view's
// swap): a device's or folder's tracks must be added to it first.
const caps = useCaps()
const selected = source.kind === 'export' && source.id === set.id
const inside =
selected ||
@@ -185,11 +194,20 @@ function ExportRow({
qc.invalidateQueries({ queryKey: ['export-loose', set.id] })
}
const accepts = (p: DragPayload) =>
p.kind === 'tracks' ? p.origin === 'collection' : playlistIdsUnder(p.node).length > 0
p.kind === 'tracks'
? p.origin === 'collection' || (caps.writable && caps.tracks.addable)
: playlistIdsUnder(p.node).length > 0
const [dropRef, over] = useDropTarget<true>({
hint: (p) => (accepts(p) ? true : null),
label: () => `Add to export ${set.name}`,
label: (p) =>
p.kind === 'tracks' && p.origin === 'device'
? `Import into export ${set.name}…`
: `Add to export ${set.name}${p.kind === 'tracks' && p.origin === 'folder' ? '…' : ''}`,
onDrop: async (p) => {
if (p.kind === 'tracks' && p.origin !== 'collection') {
onAddForeign(p.origin, p.ids, { kind: 'export', id: set.id, name: set.name })
return
}
try {
if (p.kind === 'tracks') {
await api.addToExport(set.id, { track_ids: p.ids })
+34 -11
View File
@@ -4,6 +4,7 @@ import { useQuery, useQueryClient } from '@tanstack/react-query'
import { api, type ImportPreview, type JobStatus } from '../api'
import { useCaps } from '../lib/capabilities'
import { placedAudioSummary } from '../lib/platformCopy'
import type { AddTarget } from './AddFilesDialog'
import { FolderPicker } from './FolderPicker'
import { useServerImportFolder } from './AddFilesDialog'
@@ -29,13 +30,18 @@ function gb(bytes: number): string {
interface Props {
/** Playlist to import, or null for the whole device. */
playlistId: string | null
/** Instead: just these device tracks (dragged out of the device's view). */
trackIds?: string[]
/** Where they go once imported — the playlist or export they were dropped
* on. They are in the collection first either way. */
target?: AddTarget | null
deviceLabel: string
onClose: () => void
onDone: (msg: string) => void
onError: (msg: string) => void
}
export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError }: Props) {
export function ImportDialog({ playlistId, trackIds, target, deviceLabel, onClose, onDone, onError }: Props) {
const qc = useQueryClient()
// The BASE folder, and whether to put this device's audio in its own
// subfolder of it. Kept apart rather than as one path because they are two
@@ -81,13 +87,15 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
const body = {
destination,
playlist_ids: playlistId ? [playlistId] : [],
track_ids: [],
playlist_ids: trackIds ? [] : playlistId ? [playlistId] : [],
track_ids: trackIds ?? [],
folder_name: deviceLabel,
into_playlist: target?.kind === 'playlist' ? target.id : null,
export_id: target?.kind === 'export' ? target.id : null,
}
const preview = useQuery<ImportPreview>({
queryKey: ['import-preview', destination, playlistId],
queryKey: ['import-preview', destination, playlistId, trackIds?.join('\n'), target?.id],
queryFn: () => api.importPreview(body),
enabled: !!destination && !job,
staleTime: 0,
@@ -113,9 +121,16 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
qc.invalidateQueries({ queryKey: ['collection'] })
qc.invalidateQueries({ queryKey: ['state'] })
qc.invalidateQueries({ queryKey: ['facets'] })
if (target) {
qc.invalidateQueries({ queryKey: ['playlist', target.id] })
qc.invalidateQueries({ queryKey: ['export-contents', target.id] })
qc.invalidateQueries({ queryKey: ['export-tracks', target.id] })
qc.invalidateQueries({ queryKey: ['export-loose', target.id] })
}
onDone(
`Imported ${r?.tracks ?? 0} track${r?.tracks === 1 ? '' : 's'}` +
(r?.playlists ? ` and ${r.playlists} playlist${r.playlists === 1 ? '' : 's'}` : ''),
(r?.playlists ? ` and ${r.playlists} playlist${r.playlists === 1 ? '' : 's'}` : '') +
(target ? ` into ${target.name}` : ''),
)
} else if (next.state === 'failed') {
onError(next.error || 'Import failed')
@@ -129,7 +144,7 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
return () => {
if (pollRef.current) window.clearInterval(pollRef.current)
}
}, [job, qc, onDone, onError])
}, [job, qc, onDone, onError, target])
const start = async () => {
setStarting(true)
@@ -174,9 +189,15 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
<div className="w-full max-w-lg glass-overlay">
<div className="border-b border-line px-5 py-3">
<h2 className="text-sm font-semibold text-text">
Import from {deviceLabel}
{playlistId ? '' : ' — everything'}
{trackIds
? `Import ${trackIds.length} track${trackIds.length === 1 ? '' : 's'} from ${deviceLabel}`
: `Import from ${deviceLabel}${playlistId ? '' : ' — everything'}`}
</h2>
{target && (
<p className="mt-0.5 text-xs text-muted">
Into your collection, then into {target.kind === 'export' ? 'the export ' : ''}“{target.name}”.
</p>
)}
</div>
<div className="space-y-4 px-5 py-4 text-sm">
@@ -270,9 +291,11 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
</div>
)}
<div className="text-xs text-faint">
Playlists land in a folder called “{deviceLabel}”.
</div>
{p.playlists.length > 0 && (
<div className="text-xs text-faint">
Playlists land in a folder called “{deviceLabel}”.
</div>
)}
</div>
)}
</>
+7 -1
View File
@@ -58,11 +58,17 @@ function sections(slots: number): Section[] {
{ keys: [[MOD, 'Click']], action: 'Add to / remove from the selection' },
{ keys: [['Double-click']], action: 'Edit a cell' },
{ keys: [['Right-click']], action: 'Track menu (header: choose columns)' },
{ keys: [['Drag rows']], action: 'Onto a playlist or export to add, onto the deck to load, within a playlist to reorder' },
{ keys: [[ALT_KEY, 'Drag']], action: 'Move tracks to another playlist (out of this one)' },
{ keys: [['Esc']], action: 'While dragging: cancel the drag' },
],
},
{
title: 'Sidebar',
rows: [{ keys: [['Right-click']], action: 'New playlist or folder, rename, delete' }],
rows: [
{ keys: [['Right-click']], action: 'New playlist or folder, rename, delete' },
{ keys: [['Drag']], action: 'Rearrange playlists and folders; onto an export to add one' },
],
},
{
title: 'Fields & dialogs',
+27 -6
View File
@@ -1,6 +1,7 @@
import { useEffect, useRef, useState } from 'react'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { api, type PlaylistKind, type PlaylistNode } from '../api'
import { api, type PlaylistKind, type PlaylistNode, type TrackOrigin } from '../api'
import type { AddTarget } from './AddFilesDialog'
import { useCaps } from '../lib/capabilities'
import { SaveBar } from './SaveBar'
import { confirmDiscardUnsaved } from '../lib/unsaved'
@@ -53,6 +54,9 @@ interface Props {
/** Open a drive's library as THE library (Devices' "Open for editing…"). */
onOpenLibrary: (path: string) => void
onDone: (msg: string) => void
/** Tracks not in the collection yet (a device's, a folder's) dropped on a
* playlist or export: open the dialog that adds them, target filled in. */
onAddForeign: (origin: TrackOrigin, ids: string[], target: AddTarget) => void
onOpenPathMapping: () => void
/** After unsaved edits were discarded (SaveBar). */
onDiscarded?: () => void
@@ -255,7 +259,11 @@ function NodeRow({
return isFolder && !expanded ? { zone: 'peek' } : null
},
label: (p, h, at) => {
if (h.zone === 'tracks') return `${moves(p, at.alt) ? 'Move' : 'Add'} to ${node.name}`
if (h.zone === 'tracks') {
if (p.kind === 'tracks' && p.origin === 'device') return `Import into ${node.name}…`
if (p.kind === 'tracks' && p.origin === 'folder') return `Add to ${node.name}…`
return `${moves(p, at.alt) ? 'Move' : 'Add'} to ${node.name}`
}
if (h.zone === 'peek') return null
return h.zone === 'into' || h.zone === 'first' ? `Move into ${node.name}` : `Move ${h.zone} ${node.name}`
},
@@ -430,6 +438,7 @@ export function Sidebar({
onSwitchLibrary,
onOpenLibrary,
onDone,
onAddForeign,
onOpenPathMapping,
onDiscarded,
}: Props) {
@@ -604,10 +613,12 @@ 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.
// A device's or a folder's tracks are not in the collection yet, so they
// can only come if the collection can ADD tracks — via the add dialog.
acceptsTracks: (node, p) =>
caps.writable && node.can_add_tracks && p.origin === 'collection' && p.fromPlaylist !== node.id,
caps.writable &&
node.can_add_tracks &&
(p.origin === 'collection' ? p.fromPlaylist !== node.id : caps.tracks.addable),
canMove: (node) => caps.writable && node.can_move,
onMoveNode: async (node, parentId, index, into) => {
try {
@@ -620,6 +631,10 @@ export function Sidebar({
}
},
onDropTracks: async (node, p, alt) => {
if (p.origin !== 'collection') {
onAddForeign(p.origin, p.ids, { kind: 'playlist', id: node.id, name: node.name })
return
}
try {
const msg = await addTracksToPlaylist({
qc,
@@ -745,7 +760,13 @@ export function Sidebar({
{menu && <ContextMenu {...menu} onClose={() => setMenu(null)} />}
{confirmReq && <ConfirmDialog {...confirmReq} onClose={() => setConfirmReq(null)} />}
<ExportsSection source={source} onSelect={onSelect} onDone={onDone} onError={onError} />
<ExportsSection
source={source}
onSelect={onSelect}
onDone={onDone}
onError={onError}
onAddForeign={onAddForeign}
/>
<DevicesSection
source={source}
+1
View File
@@ -423,6 +423,7 @@ export function TrackTable({
kind: 'tracks',
ids: [...new Set(carried.map((t) => t.id))],
tracks: carried,
lead: pressed,
origin: from?.origin ?? 'collection',
fromPlaylist: from?.playlistId,
source: tableKey,
+2
View File
@@ -28,6 +28,8 @@ export type DragPayload =
kind: 'tracks'
ids: string[]
tracks: Track[]
/** The row the drag started on — what a drop on the deck loads. */
lead: Track
/** Which library the ids belong to — a device's or a folder's are not
* the collection's, so collection targets must not take them as such. */
origin: TrackOrigin