Implemented OneLibrary track addition and removal

This commit is contained in:
patben8 committed 2026-10-01 16:43:37 +02:00
1 parent c5280a95f2
commit 7960a5a68f
20 files changed
+822 -85

No files matched your search

+34 -13
View File
@@ -2,12 +2,18 @@ import { createPortal } from 'react-dom'
import { useEffect, useRef, useState, type ReactNode } from 'react'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { api, type FolderAddPreview, type FolderAddRequest, type JobStatus } from '../api'
import { useCaps } from '../lib/capabilities'
import { placedAudioSummary } from '../lib/platformCopy'
import { FolderPicker } from './FolderPicker'
/**
* Add browsed files to the collection — and, optionally, to a playlist or export.
*
* Copy vs. leave-in-place is ASKED every time, with nothing preselected: the
* A library that places its own audio (`tracks.places_audio`, a OneLibrary
* stick) is the exception: only files already on it can stay in place, so the
* dialog STATES what will happen instead of asking (decided 2026-10-01).
*
* Otherwise copy vs. leave-in-place is ASKED every time, with nothing preselected: the
* right answer depends on the drive (a stick is about to be unplugged) and on
* how the DJ keeps their music, and a remembered default would quietly apply
* last week's answer to a different drive.
@@ -42,6 +48,11 @@ interface Props {
export function AddFilesDialog({ trackIds, target, onClose, onDone, onError }: Props) {
const qc = useQueryClient()
// The LIBRARY's capabilities (this dialog is mounted outside the browsed
// view's swapped context). A library that places its own audio — a stick —
// decides copy vs in place itself, so there is nothing to ask.
const caps = useCaps()
const places = caps.tracks.places_audio
const [mode, setMode] = useState<'copy' | 'reference' | null>(null)
const [folder, setFolder] = useState('')
const [browsing, setBrowsing] = useState(false)
@@ -62,16 +73,16 @@ export function AddFilesDialog({ trackIds, target, onClose, onDone, onError }: P
track_ids: trackIds,
// The preview needs A mode to size anything; "reference" costs nothing to
// ask about and is replaced as soon as the user picks.
mode: mode ?? 'reference',
destination: mode === 'copy' ? folder : null,
mode: places ? 'copy' : (mode ?? 'reference'),
destination: !places && mode === 'copy' ? folder : null,
playlist_id: target?.kind === 'playlist' ? target.id : null,
export_id: target?.kind === 'export' ? target.id : null,
}
const preview = useQuery<FolderAddPreview>({
queryKey: ['folder-add-preview', trackIds, mode, folder, target?.id],
queryKey: ['folder-add-preview', trackIds, places ? 'placed' : mode, folder, target?.id],
queryFn: () => api.folderAddPreview(body),
enabled: !job && (mode !== 'copy' || !!folder),
enabled: !job && (places || mode !== 'copy' || !!folder),
staleTime: 0,
gcTime: 0,
})
@@ -146,13 +157,15 @@ export function AddFilesDialog({ trackIds, target, onClose, onDone, onError }: P
const pct = job && job.total > 0 ? Math.min(100, (job.done / job.total) * 100) : null
const n = trackIds.length
const newCount = p?.importable ?? 0
const shown = places ? 'placed' : mode
const libraryName = places ? caps.save.app_name : 'your collection'
const canAdd =
!starting && mode !== null && !!p && (newCount > 0 || (!!target && p.existing.length > 0)) &&
!starting && shown !== null && !!p && (newCount > 0 || (!!target && p.existing.length > 0)) &&
p.enough_space !== false
const title = target
? `Add ${n} track${n === 1 ? '' : 's'} to ${target.name}`
: `Add ${n} track${n === 1 ? '' : 's'} to your collection`
: `Add ${n} track${n === 1 ? '' : 's'} to ${libraryName}`
const option = (value: 'copy' | 'reference', label: string, detail: ReactNode) => (
<label
@@ -205,6 +218,13 @@ export function AddFilesDialog({ trackIds, target, onClose, onDone, onError }: P
</div>
)}
{places && p && (
<div className="rounded well px-3 py-2 text-xs text-muted">
{placedAudioSummary(caps.save, p.copied ?? 0, p.in_place ?? 0)}
</div>
)}
{!places && (
<div className="space-y-1">
{option(
'copy',
@@ -232,6 +252,7 @@ export function AddFilesDialog({ trackIds, target, onClose, onDone, onError }: P
'Your collection points at these files. Nothing is copied.',
)}
</div>
)}
{mode === 'reference' && p?.removable && (
<div className="rounded well px-3 py-2 text-xs text-gold">
@@ -244,15 +265,15 @@ export function AddFilesDialog({ trackIds, target, onClose, onDone, onError }: P
<div className="text-pink">{(preview.error as Error).message}</div>
)}
{mode && p && (
{shown && p && (
<div className="space-y-2">
<div className="text-text">
{newCount} new to your collection
{mode === 'copy' && (
{newCount} new to {libraryName}
{shown !== 'reference' && p.total_bytes > 0 && (
<span className="text-muted"> · {gb(p.total_bytes)} to copy</span>
)}
</div>
{mode === 'copy' && p.free_bytes != null && (
{shown !== 'reference' && p.free_bytes != null && (
<div className={p.enough_space === false ? 'text-pink' : 'text-faint'}>
{gb(p.free_bytes)} free
{p.enough_space === false && ' — not enough space'}
@@ -261,7 +282,7 @@ export function AddFilesDialog({ trackIds, target, onClose, onDone, onError }: P
{p.existing.length > 0 && (
<div className="rounded well px-3 py-2 text-xs text-muted">
{p.existing.length} {p.existing.length === 1 ? 'is' : 'are'} already in
your collection and will not be added again
{' '}{libraryName} and will not be added again
{target ? ` — but will still go into ${target.name}` : ''}.
</div>
)}
@@ -271,7 +292,7 @@ export function AddFilesDialog({ trackIds, target, onClose, onDone, onError }: P
there and will be skipped.
</div>
)}
{mode === 'copy' && p.duplicates.length > 0 && (
{shown !== 'reference' && p.duplicates.length > 0 && (
<div className="rounded well px-3 py-2 text-xs text-gold">
{p.duplicates.length} of these look like tracks your collection already
has. The copies will be added as new entries.
+42 -1
View File
@@ -3,6 +3,7 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { api, type Drive, type PlaylistNode } from '../api'
import type { Source } from './Sidebar'
import { Icon } from '../lib/icons'
import { ContextMenu, type MenuItem } from './ContextMenu'
/**
* Every drive, in the sidebar: plugged-in ones under Devices, and the computer's
@@ -17,6 +18,11 @@ import { Icon } from '../lib/icons'
*
* The list genuinely changes while the app is open, so unlike everything else in
* the sidebar this POLLS. Folder levels are fetched only when expanded.
*
* A device here is BROWSED, read-only, beside the loaded library. Editing one
* means opening it as THE library ("Open for editing…": the drive row's
* right-click menu, and a button while a device that can be edited is being
* browsed) — one library, one dirty state, one SaveBar (decided 2026-10-01).
*/
const POLL_MS = 4000
@@ -26,9 +32,11 @@ interface Props {
onSelect: (s: Source) => void
onError: (msg: string) => void
onImport: () => void
/** Open a drive's library as THE library, to edit it. */
onOpenLibrary: (path: string) => void
}
export function DevicesSection({ source, onSelect, onError, onImport }: Props) {
export function DevicesSection({ source, onSelect, onError, onImport, onOpenLibrary }: Props) {
const qc = useQueryClient()
// Which rows are expanded, by a key per row kind. Not persisted: a tree
// reopening deep inside a drive that is no longer plugged in helps nobody.
@@ -101,6 +109,16 @@ export function DevicesSection({ source, onSelect, onError, onImport }: Props) {
}, [folderGone])
const viewingDevice = source.kind === 'device' || source.kind === 'device-playlist'
const [menu, setMenu] = useState<{ x: number; y: number; items: MenuItem[] } | null>(null)
// Whether the browsed device could be edited if opened as the library: a
// FACT from its capabilities (`browsing`), never the platform's name.
const deviceCaps = useQuery({
queryKey: ['source-capabilities'],
queryFn: api.sourceCapabilities,
enabled: !!openPath,
staleTime: Infinity,
})
const editable = deviceCaps.data?.readonly_cause === 'browsing'
const driveRow = (d: Drive, depth: number) => {
const key = `drive:${d.path}`
@@ -125,6 +143,16 @@ export function DevicesSection({ source, onSelect, onError, onImport }: Props) {
expanded={expandedHere}
onToggle={() => setRow(key, !expandedHere)}
onClick={onClick}
onContextMenu={
d.library
? (e) => {
e.preventDefault()
const path = d.library!.path
setMenu({ x: e.clientX, y: e.clientY,
items: [{ label: 'Open for editing…', onClick: () => onOpenLibrary(path) }] })
}
: undefined
}
selected={isOpen && source.kind === 'device'}
trailing={
isOpen && open.data?.tracks != null ? (
@@ -211,6 +239,16 @@ export function DevicesSection({ source, onSelect, onError, onImport }: Props) {
Import to collection…
</button>
)}
{viewingDevice && openPath && editable && (
<button
onClick={() => onOpenLibrary(openPath)}
title="Open this drive as the library, so its tracks, playlists and cues can be edited"
className="mb-1 w-full rounded-full btn-glass px-2 py-1.5 text-sm text-muted hover:text-text"
>
Open for editing…
</button>
)}
{menu && <ContextMenu {...menu} onClose={() => setMenu(null)} />}
</div>
</div>
)
@@ -235,6 +273,7 @@ function TreeRow({
expanded,
onToggle,
onClick,
onContextMenu,
selected = false,
trailing,
}: {
@@ -245,11 +284,13 @@ function TreeRow({
expanded: boolean
onToggle: () => void
onClick: () => void
onContextMenu?: (e: React.MouseEvent) => void
selected?: boolean
trailing?: ReactNode
}) {
return (
<div
onContextMenu={onContextMenu}
style={{ paddingLeft: indent(depth) }}
className={`flex w-full items-center gap-1 rounded-md py-1 pr-2 text-sm transition-colors ${
selected ? 'is-selected text-text' : 'text-muted hover:bg-ink-800 hover:text-text'
+15 -2
View File
@@ -2,6 +2,8 @@ import { createPortal } from 'react-dom'
import { useEffect, useRef, useState } from 'react'
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 { FolderPicker } from './FolderPicker'
/**
@@ -39,6 +41,10 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
// different decisions: where your music lives, and how one import is filed
// inside it. Joining them for display would make the toggle look like it was
// editing the folder you picked.
// The LIBRARY's capabilities (mounted outside the browsed device's context).
// One that places its own audio — a stick — needs no destination.
const caps = useCaps()
const places = caps.tracks.places_audio
const [baseFolder, setBaseFolder] = useState('')
const [useSubfolder, setUseSubfolder] = useState(true)
const [browsing, setBrowsing] = useState(false)
@@ -166,6 +172,12 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
<div className="space-y-4 px-5 py-4 text-sm">
{!job && (
<>
{places && p && (
<div className="rounded well px-3 py-2 text-xs text-muted">
{placedAudioSummary(caps.save, p.copied ?? 0, p.in_place ?? 0)}
</div>
)}
{!places && (
<div>
<span className="mb-1 block text-xs text-muted">Copy audio into</span>
<div className="flex items-center gap-2">
@@ -204,6 +216,7 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
{destination}
</div>
</div>
)}
{preview.isLoading && <div className="text-faint">Checking…</div>}
{preview.isError && (
@@ -216,7 +229,7 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
{p.importable} track{p.importable === 1 ? '' : 's'}
{p.playlists.length > 0 &&
` · ${p.playlists.length} playlist${p.playlists.length === 1 ? '' : 's'}`}
<span className="text-muted"> · {gb(p.total_bytes)} to copy</span>
{p.total_bytes > 0 && <span className="text-muted"> · {gb(p.total_bytes)} to copy</span>}
</div>
{p.free_bytes != null && (
@@ -240,7 +253,7 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
entry, so a re-import legitimately duplicates. */}
{p.duplicates.length > 0 && (
<div className="rounded well px-3 py-2 text-xs text-gold">
{p.duplicates.length} of these look like tracks your collection already has.
{p.duplicates.length} of these look like tracks {places ? caps.save.app_name : 'your collection'} already has.
They will be added again as new entries.
</div>
)}
+8 -1
View File
@@ -2,7 +2,7 @@ import { useState, type ReactNode } from 'react'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { api } from '../api'
import { useCaps } from '../lib/capabilities'
import { overwriteWarning, readOnlyNotice, saveLabel } from '../lib/platformCopy'
import { exportManagedWarning, overwriteWarning, readOnlyNotice, saveLabel } from '../lib/platformCopy'
import { Icon } from '../lib/icons'
import { askConfirm } from '../lib/confirm'
@@ -55,6 +55,7 @@ export function SaveBar({ onError, trailing, onDiscarded }: Props) {
const caps = useCaps()
const warning = overwriteWarning(caps.save)
const exportWarning = exportManagedWarning(caps.save)
const dirty = state?.dirty ?? false
const pending = state?.pending_stems
const readOnly = readOnlyNotice(caps)
@@ -81,6 +82,12 @@ export function SaveBar({ onError, trailing, onDiscarded }: Props) {
<span>{warning}</span>
</div>
)}
{dirty && exportWarning && (
<div className="mb-2 flex gap-1.5 rounded-xl bg-gold/10 px-2.5 py-1.5 text-[11px] leading-snug text-gold">
<Icon name="warning" size={13} className="mt-px shrink-0" />
<span>{exportWarning}</span>
</div>
)}
{/* Decided: the consequence is shown where Save is pressed, not only in
the Stems panel — Save deletes what a Replace conversion parked. */}
{pending && pending.parked > 0 && (
+4
View File
@@ -47,6 +47,8 @@ interface Props {
onOpenHistory: () => void
onImport: () => void
onSwitchLibrary: () => void
/** Open a drive's library as THE library (Devices' "Open for editing…"). */
onOpenLibrary: (path: string) => void
onDone: (msg: string) => void
onOpenPathMapping: () => void
/** After unsaved edits were discarded (SaveBar). */
@@ -319,6 +321,7 @@ export function Sidebar({
onOpenHistory,
onImport,
onSwitchLibrary,
onOpenLibrary,
onDone,
onOpenPathMapping,
onDiscarded,
@@ -591,6 +594,7 @@ export function Sidebar({
onSelect={onSelect}
onError={onError}
onImport={onImport}
onOpenLibrary={onOpenLibrary}
/>
</div>