mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented drag and drop loading
This commit is contained in:
1 parent
7ba71981c8
commit
c87444bda5
12 files changed
+267
-34
No files matched your search
@@ -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 })
|
||||
|
||||
@@ -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>
|
||||
)}
|
||||
</>
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user