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 { 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 * 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. * * A playlist or export target still needs the files in the collection first — * both hold collection track ids — so the dialog says so rather than doing it * silently. Files the collection already holds are not added again, but still * go into the target. */ const POLL_MS = 300 function gb(bytes: number): string { if (bytes >= 1e9) return `${(bytes / 1e9).toFixed(1)} GB` if (bytes >= 1e6) return `${(bytes / 1e6).toFixed(0)} MB` return `${(bytes / 1e3).toFixed(0)} kB` } export interface AddTarget { kind: 'playlist' | 'export' id: string name: string } interface Props { trackIds: string[] target: AddTarget | null onClose: () => void onDone: (msg: string) => void onError: (msg: string) => void } export function AddFilesDialog({ trackIds, target, onClose, onDone, onError }: Props) { const qc = useQueryClient() const [mode, setMode] = useState<'copy' | 'reference' | null>(null) const [folder, setFolder] = useState('') const [browsing, setBrowsing] = useState(false) const [job, setJob] = useState(null) const [starting, setStarting] = useState(false) const pollRef = useRef(null) // The same import folder as a device import: one place your copied music goes. const prefs = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs }) const prefsLoaded = prefs.isSuccess useEffect(() => { if (!prefsLoaded || folder) return const p = prefs.data as { importFolder?: string } setFolder(p?.importFolder || '~/Music/Konduktor Imports') }, [prefsLoaded, prefs.data, folder]) const body: FolderAddRequest = { 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, playlist_id: target?.kind === 'playlist' ? target.id : null, export_id: target?.kind === 'export' ? target.id : null, } const preview = useQuery({ queryKey: ['folder-add-preview', trackIds, mode, folder, target?.id], queryFn: () => api.folderAddPreview(body), enabled: !job && (mode !== 'copy' || !!folder), staleTime: 0, gcTime: 0, }) useEffect(() => { if (!job || job.state !== 'running') return pollRef.current = window.setInterval(async () => { try { const next = await api.job(job.id) setJob(next) if (next.state === 'running') return if (pollRef.current) window.clearInterval(pollRef.current) if (next.state === 'done') { const r = next.result as { tracks?: number; track_ids?: string[] } | null qc.invalidateQueries({ queryKey: ['tracks'] }) qc.invalidateQueries({ queryKey: ['playlists'] }) qc.invalidateQueries({ queryKey: ['playlist'] }) qc.invalidateQueries({ queryKey: ['collection'] }) qc.invalidateQueries({ queryKey: ['state'] }) qc.invalidateQueries({ queryKey: ['facets'] }) qc.invalidateQueries({ queryKey: ['folder-tracks'] }) if (target?.kind === 'export') { qc.invalidateQueries({ queryKey: ['exports'] }) qc.invalidateQueries({ queryKey: ['export-contents', target.id] }) qc.invalidateQueries({ queryKey: ['export-tracks', target.id] }) qc.invalidateQueries({ queryKey: ['export-loose', target.id] }) } const added = r?.tracks ?? 0 const total = r?.track_ids?.length ?? added onDone( target ? `Added ${total} track${total === 1 ? '' : 's'} to ${target.name}` + (added ? ` (${added} new to your collection)` : '') : `Added ${added} track${added === 1 ? '' : 's'} to your collection`, ) } else if (next.state === 'failed') { onError(next.error || 'Adding failed') } } catch (e) { onError((e as Error).message) if (pollRef.current) window.clearInterval(pollRef.current) } }, POLL_MS) return () => { if (pollRef.current) window.clearInterval(pollRef.current) } }, [job, qc, onDone, onError, target]) const start = async () => { setStarting(true) try { setJob(await api.folderAdd(body)) } catch (e) { onError((e as Error).message) } finally { setStarting(false) } } const cancel = async () => { if (!job) return try { await api.cancelJob(job.id) } catch (e) { onError((e as Error).message) } } const p = preview.data const running = job?.state === 'running' const finished = job != null && job.state !== 'running' 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 canAdd = !starting && mode !== 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` const option = (value: 'copy' | 'reference', label: string, detail: ReactNode) => ( ) return createPortal( <> {browsing && ( { setFolder(path) api.patchPrefs({ importFolder: path }).catch(() => {}) }} onClose={() => setBrowsing(false)} /> )}

{title}

{!job && ( <> {target && (
{target.kind === 'playlist' ? 'Playlists' : 'Exports'} hold tracks from your collection, so these are added to the collection first.
)}
{option( 'copy', 'Copy into your import folder', {/* LRM marks: in an rtl box (for left-truncation) a leading "~" would otherwise be moved to the end. */} {folder ? `\u200E${folder}\u200E` : 'Loading…'} , )} {option( 'reference', 'Leave them where they are', 'Your collection points at these files. Nothing is copied.', )}
{mode === 'reference' && p?.removable && (
These files are on an external drive. They will go missing from your collection whenever the drive is unplugged.
)} {preview.isError && (
{(preview.error as Error).message}
)} {mode && p && (
{newCount} new to your collection {mode === 'copy' && ( · {gb(p.total_bytes)} to copy )}
{mode === 'copy' && p.free_bytes != null && (
{gb(p.free_bytes)} free {p.enough_space === false && ' — not enough space'}
)} {p.existing.length > 0 && (
{p.existing.length} {p.existing.length === 1 ? 'is' : 'are'} already in your collection and will not be added again {target ? ` — but will still go into ${target.name}` : ''}.
)} {p.missing.length > 0 && (
{p.missing.length} file{p.missing.length === 1 ? ' is' : 's are'} no longer there and will be skipped.
)} {mode === 'copy' && p.duplicates.length > 0 && (
{p.duplicates.length} of these look like tracks your collection already has. The copies will be added as new entries.
)}
)} )} {job && (
{job.message || 'Working…'}
{job.state === 'cancelled' && (
Cancelled. Nothing was added, and any copied files were removed.
)} {job.state === 'failed' &&
{job.error}
} {job.state === 'done' &&
Done.
}
)}
{running ? ( <> ) : ( <> {!finished && ( )} )}
, document.body, ) }