import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { useQuery, useQueryClient } from '@tanstack/react-query' import type { ColumnSizingState, SortingState, VisibilityState } from '@tanstack/react-table' import { CapabilitiesContext, slotLabeller } from './lib/capabilities' import { removalNote, writeHint } from './lib/platformCopy' import { invalidateTrackLists } from './lib/trackQueries' import { api, type CueBatchResult, type GridBatchResult, type AutoCueSlot, type PlaylistNode, type StemBatchResult, type Track, type TrackOrigin } from './api' import { confirmDiscardUnsaved } from './lib/unsaved' import { COLUMN_MENU, DEFAULT_COLUMN_ORDER, DEFAULT_COLUMN_VISIBILITY, } from './lib/trackColumns' import { Sidebar, type Source } from './components/Sidebar' import { Toolbar, emptyFilters, type Filters } from './components/Toolbar' import { TrackTable } from './components/TrackTable' import { StatusBar } from './components/StatusBar' import { Toast, type ToastMsg } from './components/Toast' import { CollectionPicker } from './components/CollectionPicker' import { ContextMenu, type MenuItem } from './components/ContextMenu' import { EditTagsDialog } from './components/EditTagsDialog' import { AnalyzeGridDialog } from './components/AnalyzeGridDialog' import { AutoCueDialog } from './components/AutoCueDialog' import { ConfirmDialog, type ConfirmRequest } from './components/ConfirmDialog' import { PathMappingDialog } from './components/PathMappingDialog' import { RelocateDialog } from './components/RelocateDialog' import { HistoryPanel } from './components/HistoryPanel' import { PrepStrip } from './components/PrepStrip' import { ImportDialog } from './components/ImportDialog' import { AddFilesDialog, type AddTarget } from './components/AddFilesDialog' import { ConvertStemsDialog } from './components/ConvertStemsDialog' import { StemReportDialog } from './components/StemReportDialog' import { Icon } from './lib/icons' import { usePrefsWriter } from './lib/prefs' function applyFilters(tracks: Track[], f: Filters): Track[] { const q = f.search.trim().toLowerCase() const bpmMin = f.bpmMin ? parseFloat(f.bpmMin) : null const bpmMax = f.bpmMax ? parseFloat(f.bpmMax) : null return tracks.filter((t) => { if (q) { const hay = `${t.artist ?? ''} ${t.title ?? ''} ${t.album ?? ''}`.toLowerCase() if (!hay.includes(q)) return false } if (f.genre && t.genre !== f.genre) return false if (f.key && t.key !== f.key) return false if (bpmMin != null && (t.bpm == null || t.bpm < bpmMin)) return false if (bpmMax != null && (t.bpm == null || t.bpm > bpmMax)) return false if (f.ratingMin > 0 && t.rating < f.ratingMin) return false if (f.hasCues === 'yes' && t.cue_count === 0) return false if (f.hasCues === 'no' && t.cue_count > 0) return false return true }) } export default function App() { const qc = useQueryClient() const [source, setSource] = useState({ kind: 'all' }) const [filters, setFilters] = useState(emptyFilters) const [sorting, setSorting] = useState([]) const [selected, setSelected] = useState>(new Set()) const [toast, setToast] = useState(null) const [forcePicker, setForcePicker] = useState(false) // `ids` is what the menu acts on: the whole selection when the clicked row is // part of it, otherwise just that row. const [menu, setMenu] = useState<{ track: Track; ids: string[]; x: number; y: number } | null>(null) // Right-click on the table header: the column chooser. const [headerMenu, setHeaderMenu] = useState<{ x: number; y: number } | null>(null) const [editing, setEditing] = useState(null) const [showPaths, setShowPaths] = useState(false) const [showHistory, setShowHistory] = useState(false) const [prepTrack, setPrepTrack] = useState(null) const [deckPlaying, setDeckPlaying] = useState(false) // Which library the DECK's track came from — not the view's. Browsing to a // playlist while a device or folder track is loaded must not switch the deck // to endpoints that do not know that track. const [prepOrigin, setPrepOrigin] = useState('collection') // The view's origin, read by the load callbacks below (defined before it). const viewOriginRef = useRef('collection') // Browsed files being added to the collection (and maybe a playlist/export). 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) // 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 // run is placing cues on. const [gridConfirm, setGridConfirm] = useState< { ids: string[]; existing: number; locked: number } | null >(null) const [cueBatch, setCueBatch] = useState<{ ids: string[]; withoutGrid: number } | null>(null) const [batchJob, setBatchJob] = useState<{ id: string; kind: 'grid' | 'cues' | 'stems' } | null>(null) // Convert to Stems: the dialog's selection, a finished run's Details, and a // running engine download (which outlives the dialog — decided). const [stemDialog, setStemDialog] = useState(null) const [stemReport, setStemReport] = useState(null) const [engineJob, setEngineJob] = useState(null) const [batchCancelling, setBatchCancelling] = useState(false) const [cuesRefresh, setCuesRefresh] = useState(0) // bump → deck re-reads its cues // Every Remove ▸ action (and the playlist Delete key) confirms first. const [confirmReq, setConfirmReq] = useState(null) const loadTrack = useCallback((t: Track) => { setPrepTrack(t) setPrepOrigin(viewOriginRef.current) }, []) const playTrack = useCallback( (t: Track) => { loadTrack(t) setPlayRequest((n) => n + 1) }, [loadTrack], ) // ---- configurable library columns (persisted to userprefs.json) ---- const [columnVisibility, setColumnVisibility] = useState(DEFAULT_COLUMN_VISIBILITY) const [columnOrder, setColumnOrder] = useState(DEFAULT_COLUMN_ORDER) const [columnSizing, setColumnSizing] = useState({}) const prefsQuery = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs }) const hydratedRef = useRef(false) const writePrefs = usePrefsWriter() const resetColumns = useCallback(() => { setColumnVisibility(DEFAULT_COLUMN_VISIBILITY) setColumnOrder(DEFAULT_COLUMN_ORDER) setColumnSizing({}) }, []) // Hydrate the saved layout once, tolerating partial / stale (new columns // added since) data by merging against the current defaults. useEffect(() => { if (hydratedRef.current || !prefsQuery.data) return hydratedRef.current = true const cols = (prefsQuery.data as { columns?: Record }).columns if (!cols) return if (cols.visibility) { setColumnVisibility({ ...DEFAULT_COLUMN_VISIBILITY, ...(cols.visibility as VisibilityState) }) } if (Array.isArray(cols.order) && cols.order.length) { const saved = cols.order as string[] const known = new Set(DEFAULT_COLUMN_ORDER) setColumnOrder([ ...saved.filter((id) => known.has(id)), ...DEFAULT_COLUMN_ORDER.filter((id) => !saved.includes(id)), ]) } if (cols.sizing && typeof cols.sizing === 'object') { setColumnSizing(cols.sizing as ColumnSizingState) } }, [prefsQuery.data]) // Persist layout changes (debounced, via the shared prefs writer), but not // before hydration so we never clobber saved prefs with the initial defaults. useEffect(() => { if (!hydratedRef.current) return writePrefs({ columns: { visibility: columnVisibility, order: columnOrder, sizing: columnSizing }, }) }, [columnVisibility, columnOrder, columnSizing, writePrefs]) const collection = useQuery({ queryKey: ['collection'], queryFn: api.collection }) const loaded = collection.data?.loaded ?? false // What the loaded library can persist. Never stale within a session, and the // app does not render until it resolves — so no first frame can offer an edit // the adapter would reject. const capabilities = useQuery({ queryKey: ['capabilities'], queryFn: api.capabilities, enabled: loaded, staleTime: Infinity, }) const save = capabilities.data?.save // The open-time missing-files check. The backend searches as the library // opens and holds the result until it is answered, then serves nothing — so // "ask once per open" is the backend's fact, and refetching cannot re-ask. const relocation = useQuery({ queryKey: ['relocation'], queryFn: api.relocation, enabled: loaded, refetchInterval: (q) => (q.state.data?.scanning ? 1500 : false), refetchOnWindowFocus: false, }) // From the adapter, not a path split: a Serato library is a directory. const libraryName = collection.data?.library?.display_name ?? null const writeHintText = save ? writeHint(save) : 'save to write it to disk' const notify = useCallback((kind: ToastMsg['kind'], text: string, action?: ToastMsg['action']) => { setToast({ id: Date.now(), kind, text, action }) }, []) const onError = useCallback((msg: string) => notify('error', msg), [notify]) // Inline single-field edit from a double-clicked table cell. Only offered when // the library can take an edit: passing `undefined` makes those cells inert // rather than firing a request the adapter will refuse. Defaults to false, so // no edit is ever offered before capabilities have resolved. const canEdit = capabilities.data?.writable ?? false const editField = useCallback( (track: Track, field: keyof Track, value: string | number) => { api .editTrack(track.id, { [field]: value }) .then(() => { invalidateTrackLists(qc) qc.invalidateQueries({ queryKey: ['facets'] }) notify('success', `Updated ${String(field)} — ${writeHintText}`) }) .catch((e) => onError((e as Error).message)) }, [qc, notify, onError, writeHintText], ) // NOTE: all hooks must run on every render (Rules of Hooks). Data queries are // gated with `enabled: loaded` so they don't fire before a collection is open; // the loading/picker early-returns live AFTER every hook below. const allTracks = useQuery({ queryKey: ['tracks', 'all'], queryFn: () => api.tracks({ limit: 20000, sort: 'artist' }), enabled: loaded && source.kind === 'all', }) const playlistTracks = useQuery({ queryKey: ['playlist', source.kind === 'playlist' ? source.id : null], queryFn: () => api.playlistTracks((source as { id: string }).id), enabled: loaded && source.kind === 'playlist', }) // An export set being browsed. Its tracks ARE collection tracks — an export is // a slice of the loaded library, not another library — so these share the // collection's capabilities and stay fully editable, unlike a device. const viewingExport = source.kind === 'export' || source.kind === 'export-playlist' || source.kind === 'export-other' const exportTracks = useQuery({ queryKey: ['export-tracks', source.kind === 'export' ? source.id : null], queryFn: () => api.exportTracks((source as { id: string }).id), enabled: source.kind === 'export', }) const exportPlaylistTracks = useQuery({ queryKey: [ 'export-playlist-tracks', source.kind === 'export-playlist' ? source.exportId : null, source.kind === 'export-playlist' ? source.id : null, ], queryFn: () => api.exportPlaylistTracks( (source as { exportId: string }).exportId, (source as { id: string }).id, ), enabled: source.kind === 'export-playlist', }) // The loose tracks only. A separate query, not a filter on the root view: the // root is everything the export would ship, which is a different answer. const exportLooseTracks = useQuery({ queryKey: ['export-loose', source.kind === 'export-other' ? source.exportId : null], queryFn: () => api.exportLooseTracks((source as { exportId: string }).exportId), enabled: source.kind === 'export-other', }) // A browsed device. Separate queries and separate cache keys from the // collection's: the two libraries can hold tracks with identical ids, and one // shared key would serve a stick's track as if it were a collection track. const viewingDevice = source.kind === 'device' || source.kind === 'device-playlist' const deviceTracks = useQuery({ queryKey: ['source-tracks', 'all'], queryFn: () => api.sourceTracks({ limit: 20000, sort: 'artist' }), enabled: viewingDevice && source.kind === 'device', }) const devicePlaylistTracks = useQuery({ queryKey: ['source-tracks', source.kind === 'device-playlist' ? source.id : null], queryFn: () => api.sourcePlaylistTracks((source as { id: string }).id), enabled: source.kind === 'device-playlist', }) // The device's own capabilities — read-only — provided to the browsing half of // the UI below. That is what makes every existing gate correct here without a // single component asking whether it is looking at a device: inline editing, // Edit Tags, the rating stars and all twelve deck edit handlers already gate // on capabilities, and a device's say `writable: false`. const deviceCaps = useQuery({ queryKey: ['source-capabilities'], queryFn: api.sourceCapabilities, enabled: viewingDevice || prepOrigin === 'device', staleTime: Infinity, }) // A browsed folder of loose files. Read-only like a device, for the same // reason: its track ids (absolute paths) are not collection tracks. const viewingFolder = source.kind === 'folder' const folderPath = source.kind === 'folder' ? source.path : null const folderView = useQuery({ queryKey: ['folder-tracks', folderPath], queryFn: () => api.folderTracks(folderPath!), enabled: loaded && viewingFolder, }) const folderCaps = useQuery({ queryKey: ['folder-capabilities', folderPath], queryFn: () => api.folderCapabilities(folderPath!), enabled: loaded && viewingFolder, staleTime: Infinity, }) const heldInFolder = useMemo( () => new Set(folderView.data?.in_collection ?? []), [folderView.data], ) viewOriginRef.current = viewingDevice ? 'device' : viewingFolder ? 'folder' : 'collection' // Tracks in view belong to ANOTHER library (a device) or to none (a folder), // so no collection command may be offered on them: their ids mean nothing to // the collection's routes. const viewForeign = viewingDevice || viewingFolder const openDevice = useQuery({ queryKey: ['source'], queryFn: api.source }) const exportSets = useQuery({ queryKey: ['exports'], queryFn: api.exports, enabled: loaded }) const playlists = useQuery({ queryKey: ['playlists'], queryFn: api.playlists, enabled: loaded }) // Every playlist the context menu's "Add to" can target, flattened out of // folders. Each node carries its own `can_add_tracks`, so a read-only library // simply offers none. const addablePlaylists = useMemo(() => { const walk = (nodes: PlaylistNode[]): PlaylistNode[] => nodes.flatMap((n) => [...(n.can_add_tracks ? [n] : []), ...walk(n.children)]) return walk(playlists.data ?? []) }, [playlists.data]) // Export membership. Both are no-ops on the library itself, so neither dirties // it nor touches version history — an export set is Konduktor's own data. const refreshExport = useCallback( (id: string) => { qc.invalidateQueries({ queryKey: ['export-contents', id] }) qc.invalidateQueries({ queryKey: ['export-tracks', id] }) qc.invalidateQueries({ queryKey: ['export-loose', id] }) }, [qc], ) const addToExport = useCallback( async (id: string, ids: string[]) => { try { await api.addToExport(id, { track_ids: ids }) refreshExport(id) const name = exportSets.data?.find((s) => s.id === id)?.name ?? 'export' notify('success', `Added ${ids.length} track${ids.length === 1 ? '' : 's'} to ${name}`) } catch (e) { onError((e as Error).message) } }, [refreshExport, exportSets.data, notify, onError], ) const addToPlaylist = useCallback( async (uuid: string, ids: string[]) => { try { const res = await api.addEntries(uuid, ids) qc.invalidateQueries({ queryKey: ['state'] }) qc.invalidateQueries({ queryKey: ['playlists'] }) qc.invalidateQueries({ queryKey: ['playlist', uuid] }) const name = addablePlaylists.find((p) => p.id === uuid)?.name ?? 'playlist' notify('success', `Added ${res.added} track${res.added === 1 ? '' : 's'} to ${name}`) } catch (e) { onError((e as Error).message) } }, [qc, addablePlaylists, notify, onError], ) const removeFromExport = useCallback( async (id: string, ids: string[]) => { try { await api.removeFromExport(id, { track_ids: ids }) refreshExport(id) } catch (e) { onError((e as Error).message) } }, [refreshExport, onError], ) // What the VIEW can do, as opposed to what the loaded collection can do. While // a device is being browsed these are its read-only capabilities, so the // browsing half of the UI gates itself correctly with no new conditions. // Falls back to the collection's until the device's resolve, so no frame // renders without capabilities at all. // Until a device's or folder's own capabilities arrive, assume READ-ONLY // rather than falling back to the collection's. The fallback is the safe // direction on purpose: guessing "writable" for one frame would offer an edit // that, if taken, would be sent to the wrong library entirely. const readOnlyCaps = (cause: 'platform_incomplete' | 'not_in_library') => capabilities.data ? { ...capabilities.data, writable: false, readonly_cause: cause } : capabilities.data! const capsFor = (origin: TrackOrigin) => origin === 'device' ? (deviceCaps.data ?? readOnlyCaps('platform_incomplete')) : origin === 'folder' ? (folderCaps.data ?? readOnlyCaps('not_in_library')) : capabilities.data! const viewCaps = capsFor(viewOriginRef.current) // The deck follows its TRACK's library, not the view's. const deckCaps = capsFor(prepOrigin) const deviceLabel = openDevice.data?.label ?? 'device' // One name for whatever is on screen, so the header and the status bar cannot // disagree about what the user is looking at. const viewName = source.kind === 'all' ? 'All Tracks' : source.kind === 'device' ? deviceLabel : source.name const isAll = source.kind === 'all' const tracks: Track[] = viewingFolder ? (folderView.data?.tracks ?? []) : viewingDevice ? source.kind === 'device' ? (deviceTracks.data?.items ?? []) : (devicePlaylistTracks.data ?? []) : viewingExport ? source.kind === 'export' ? (exportTracks.data ?? []) : source.kind === 'export-other' ? (exportLooseTracks.data ?? []) : (exportPlaylistTracks.data ?? []) : isAll ? (allTracks.data?.items ?? []) : (playlistTracks.data ?? []) const loading = viewingFolder ? folderView.isLoading : viewingDevice ? source.kind === 'device' ? deviceTracks.isLoading : devicePlaylistTracks.isLoading : viewingExport ? source.kind === 'export' ? exportTracks.isLoading : source.kind === 'export-other' ? exportLooseTracks.isLoading : exportPlaylistTracks.isLoading : isAll ? allTracks.isLoading : playlistTracks.isLoading // Search / filters apply to both the library and playlists (the toolbar is // always visible). In a playlist a filtered view disables drag-reorder — see // `canReorder` below — so a partial order can't overwrite the full entry list. const filtered = useMemo(() => applyFilters(tracks, filters), [tracks, filters]) const filtersActive = filters.search !== '' || filters.genre !== '' || filters.key !== '' || filters.bpmMin !== '' || filters.bpmMax !== '' || filters.ratingMin > 0 || filters.hasCues !== 'any' // Playlist reorder / remove: persist the new entry order via setEntries, and // optimistically update the cached playlist so the list stays put after a drag // (the TrackTable re-syncs to this on the next render). const reorderPlaylist = useCallback( (ids: string[]) => { if (source.kind !== 'playlist') return const id = source.id qc.setQueryData(['playlist', id], (prev) => { if (!prev) return prev const byId = new Map(prev.map((t) => [t.id, t])) return ids.map((i) => byId.get(i)).filter((t): t is Track => !!t) }) api .setEntries(id, ids) .then(() => { qc.invalidateQueries({ queryKey: ['state'] }) qc.invalidateQueries({ queryKey: ['playlists'] }) // refresh counts }) .catch((e) => onError((e as Error).message)) }, [source, qc, onError], ) const removeFromPlaylist = useCallback( (ids: string[]) => { const gone = new Set(ids) reorderPlaylist(tracks.filter((t) => !gone.has(t.id)).map((t) => t.id)) }, [reorderPlaylist, tracks], ) // The open playlist's node, for its own flags: a smart playlist has a track // list but no manual order, and each platform says so per node. const playlistNode = useMemo(() => { if (source.kind !== 'playlist') return null const find = (nodes: PlaylistNode[]): PlaylistNode | null => { for (const n of nodes) { if (n.id === source.id) return n const hit = find(n.children) if (hit) return hit } return null } return find(playlists.data ?? []) }, [source, playlists.data]) const playlistEditable = canEdit && !!playlistNode?.can_reorder // A playlist's own 1-based positions, so the # column still means "where in // the playlist" once the view is sorted or filtered. const playlistPositions = useMemo(() => { if (source.kind !== 'playlist') return undefined const m = new Map() tracks.forEach((t, i) => !m.has(t.id) && m.set(t.id, i + 1)) return m }, [source.kind, tracks]) const batchJobStatus = useQuery({ queryKey: ['job', batchJob?.id ?? null], queryFn: () => api.job(batchJob!.id), enabled: !!batchJob, refetchInterval: (q) => (q.state.data && q.state.data.state !== 'running' ? false : 400), }) const batchDone = batchJobStatus.data?.done ?? 0 useEffect(() => { // Each finished track is an unsaved edit; let the Save button know as they land. if (batchDone > 0) qc.invalidateQueries({ queryKey: ['state'] }) }, [batchDone, qc]) useEffect(() => { const job = batchJobStatus.data if (!job || job.state === 'running' || !batchJob || job.id !== batchJob.id) return const kind = batchJob.kind setBatchJob(null) setBatchCancelling(false) if (kind === 'stems') { finishStemBatch(job.state, job.result as unknown as StemBatchResult | null, job.error) return } if (job.state === 'failed') { const what = kind === 'grid' ? 'Grid analysis' : 'Auto Hotcues' onError(`${what} failed: ${job.error ?? 'unknown error'}`) return } if (!job.result) return invalidateTrackLists(qc) qc.invalidateQueries({ queryKey: ['facets'] }) const cancelled = job.state === 'cancelled' const plural = (n: number, w: string) => `${n} ${w}${n === 1 ? '' : 's'}` let touched: string[] let parts: string[] let failures: CueBatchResult['failed'] if (kind === 'grid') { const r = job.result as unknown as GridBatchResult touched = r.analysed failures = r.failed parts = [ `${cancelled ? 'Cancelled — analyzed' : 'Analyzed'} ${plural(r.analysed.length, 'track')}`, r.existing ? `skipped ${r.existing} with a grid` : '', r.locked ? `${r.locked} locked` : '', ] } else { const r = job.result as unknown as CueBatchResult touched = r.tracks failures = r.failed parts = [ `${cancelled ? 'Cancelled — placed' : 'Placed'} ${plural(r.cues_placed, 'cue')} on ${plural(r.tracks.length, 'track')}`, r.grids_created ? `analyzed ${plural(r.grids_created, 'new grid')}` : '', ] } if (failures.length) { const f = failures[0] parts.push(`${failures.length} failed (${f.title}: ${f.reason}${failures.length > 1 ? ', …' : ''})`) } if (prepTrack && touched.includes(prepTrack.id)) setCuesRefresh((n) => n + 1) notify(failures.length && touched.length === 0 ? 'error' : 'success', parts.filter(Boolean).join(' · ')) // Only the job's finish matters here; prepTrack is read, not tracked. // eslint-disable-next-line react-hooks/exhaustive-deps }, [batchJobStatus.data]) const handleOpened = () => { setForcePicker(false) setSource({ kind: 'all' }) setSelected(new Set()) setBatchJob(null) // the backend cancels it; its result is for the old library // The deck starts empty: its track belongs to the old library. setPrepTrack(null) setPlayRequest(0) setFilters(emptyFilters) setSorting([]) qc.invalidateQueries() // refetch everything for the newly-opened collection } // "Open for editing…" on a device: the drive becomes THE library — one library, // one dirty state, one SaveBar (decided 2026-10-01). Unsaved edits to the // current library are confirmed first, as switching library always is, and the // browsing copy is closed so the drive is not open twice. const openForEditing = async (path: string) => { if (!(await confirmDiscardUnsaved())) return try { await api.closeSource() await api.openCollection(path) handleOpened() } catch (e) { onError((e as Error).message) } } // ---- Convert to Stems ------------------------------------------------------ const canStems = canEdit && !viewForeign && !!capabilities.data?.tracks.stem_convertible const finishStemBatch = (state: string, r: StemBatchResult | null, error: string | null) => { qc.invalidateQueries({ queryKey: ['state'] }) if (state === 'failed' || !r) { onError(`Converting to stems failed: ${error ?? 'unknown error'}`) return } if (r.cancelled) { notify('warning', 'Conversion cancelled — nothing was changed') return } invalidateTrackLists(qc) qc.invalidateQueries({ queryKey: ['playlists'] }) qc.invalidateQueries({ queryKey: ['export-contents'] }) qc.invalidateQueries({ queryKey: ['facets'] }) // A repointed track's id IS its location, so it changed: follow it with the // deck and the selection instead of leaving them on an id that is gone. const renamed = r.renamed if (prepTrack && renamed[prepTrack.id]) { setPrepTrack({ ...prepTrack, id: renamed[prepTrack.id], media_kind: 'stem' }) setCuesRefresh((n) => n + 1) } setSelected((sel) => new Set([...sel].map((id) => renamed[id] ?? id))) const n = r.converted.length const parts = [ `${n} converted to stems`, r.skipped.length ? `${r.skipped.length} skipped` : '', r.failed.length ? `${r.failed.length} failed` : '', ].filter(Boolean) const text = parts.join(' · ') + (n ? ' — Save to keep them' : '') const details = r.skipped.length || r.failed.length ? { label: 'Details', onClick: () => setStemReport(r) } : undefined notify(n ? 'success' : 'error', text, details) } // A conversion keeps running if the page reloads; find it again. const editState = useQuery({ queryKey: ['state'], queryFn: api.state, enabled: loaded, retry: false }) useEffect(() => { const id = editState.data?.stem_job if (id && !batchJob) setBatchJob({ id, kind: 'stems' }) // eslint-disable-next-line react-hooks/exhaustive-deps }, [editState.data?.stem_job]) // Tell the user once what crash recovery did about an interrupted conversion. const recovery = editState.data?.stem_recovery const recoveryShown = useRef(null) useEffect(() => { if (!recovery || recoveryShown.current === recovery) return recoveryShown.current = recovery const bits = [ recovery.restored ? `${recovery.restored} original${recovery.restored === 1 ? '' : 's'} put back` : '', recovery.committed ? `${recovery.committed} saved conversion${recovery.committed === 1 ? '' : 's'} finished` : '', recovery.kept ? `${recovery.kept} converted file${recovery.kept === 1 ? '' : 's'} kept for next time` : '', ].filter(Boolean) if (bits.length) notify('warning', `An interrupted stem conversion was tidied up: ${bits.join(' · ')}`) }, [recovery, notify]) const engineJobStatus = useQuery({ queryKey: ['job', engineJob], queryFn: () => api.job(engineJob!), enabled: !!engineJob, refetchInterval: (q) => (q.state.data && q.state.data.state !== 'running' ? false : 500), }) useEffect(() => { const job = engineJobStatus.data if (!job || job.state === 'running' || job.id !== engineJob) return setEngineJob(null) qc.invalidateQueries({ queryKey: ['stem-engine'] }) if (job.state === 'done') notify('success', 'The stem engine is installed') else if (job.state === 'failed') onError(job.error || 'The stem engine download failed') }, [engineJobStatus.data, engineJob, qc, notify, onError]) // Hooks must all run BEFORE the early returns below (picker / loading), // or the first render with a library has more hooks than the last one and // React throws — the whole window goes blank. if (collection.isLoading || (loaded && !capabilities.data)) { return (
Loading…
) } if (!loaded || forcePicker) { return ( setForcePicker(false) : undefined} /> ) } const selectSource = (s: Source) => { setSource(s) setSorting([]) setSelected(new Set()) } // TrackTable retargets the selection to a right-clicked row outside it // before calling this, but that state update has not landed yet — so the // same rule is applied here to decide what the menu acts on. const openMenu = (track: Track, x: number, y: number, selectable: boolean) => setMenu({ track, ids: selectable && selected.has(track.id) ? [...selected] : [track.id], x, y, }) const runGridAnalysis = async (ids: string[], replaceExisting: boolean) => { setGridConfirm(null) try { const job = await api.autoGridBatch(ids, replaceExisting) setBatchCancelling(false) setBatchJob({ id: job.id, kind: 'grid' }) } catch (e) { onError((e as Error).message) } } // Confirm only when the run would meet existing grids. Counted from the // view's tracks, which is where the selection came from. const startGridAnalysis = (ids: string[]) => { const byId = new Map(tracks.map((t) => [t.id, t])) const sel = ids.map((id) => byId.get(id)).filter((t): t is Track => !!t) const locked = sel.filter((t) => t.grid_locked).length const existing = sel.filter((t) => !t.grid_locked && t.grid_marker_count > 0).length if (locked === ids.length) { notify('error', `${locked === 1 ? 'That grid is' : `All ${locked} grids are`} locked — nothing to analyze`) } else if (existing > 0) { setGridConfirm({ ids, existing, locked }) } else { void runGridAnalysis(ids, false) } } const canAnalyzeGrid = canEdit && !viewForeign && !!capabilities.data?.grid.editable const startCueBatch = (ids: string[]) => { const byId = new Map(tracks.map((t) => [t.id, t])) const withoutGrid = ids.filter((id) => (byId.get(id)?.grid_marker_count ?? 0) === 0).length setCueBatch({ ids, withoutGrid }) } // Rejects on failure, so the dialog stays open and shows it can be retried. const runCueBatch = async (ids: string[], slots: AutoCueSlot[]) => { const job = await api.autoCuesBatch(ids, slots) setBatchCancelling(false) setBatchJob({ id: job.id, kind: 'cues' }) } const canAutoCue = canEdit && !viewForeign && (capabilities.data?.cues.hotcue_slots ?? 0) > 0 // ---- Remove ▸ ----------------------------------------------------------- const nTracks = (n: number) => `${n} track${n === 1 ? '' : 's'}` const trackNoun = (ids: string[]) => { if (ids.length > 1) return nTracks(ids.length) const t = tracks.find((x) => x.id === ids[0]) return t?.title ? `“${t.title}”` : 'this track' } // Refetch whatever a track list or the deck could be showing. Throws on // failure (after reporting), so the confirm dialog stays open for a retry. const afterBulk = (ids: string[], cuesChanged: boolean) => { invalidateTrackLists(qc) qc.invalidateQueries({ queryKey: ['playlists'] }) qc.invalidateQueries({ queryKey: ['export-contents'] }) qc.invalidateQueries({ queryKey: ['facets'] }) if (cuesChanged && prepTrack && ids.includes(prepTrack.id)) setCuesRefresh((n) => n + 1) } const guard = async (fn: () => Promise) => { try { await fn() } catch (e) { onError((e as Error).message) throw e } } const confirmRemoveFromPlaylist = (ids: string[]) => setConfirmReq({ title: 'Remove from playlist', body: (

Remove {trackNoun(ids)} from {playlistNode?.name ?? 'this playlist'}? The {ids.length === 1 ? 'track stays' : 'tracks stay'} in the collection.

), confirmLabel: `Remove ${nTracks(ids.length)}`, onConfirm: () => { removeFromPlaylist(ids) setSelected(new Set()) }, }) const confirmRemoveFromExport = (exportId: string, ids: string[]) => { const name = exportSets.data?.find((x) => x.id === exportId)?.name ?? 'this export' setConfirmReq({ title: 'Remove from export', body: (

Remove {trackNoun(ids)} from {name}? Your library is not changed.

), confirmLabel: `Remove ${nTracks(ids.length)}`, onConfirm: () => guard(async () => { await removeFromExport(exportId, ids) setSelected(new Set()) }), }) } const confirmClearGrids = (ids: string[]) => { const locked = tracks.filter((t) => ids.includes(t.id) && t.grid_locked).length setConfirmReq({ title: 'Remove beatgrids', body: ( <>

Delete the beatgrid of {trackNoun(ids)}? Tempo and markers go; the paired beat-1 cue goes with them. Hotcues are kept.

{locked > 0 && (

{locked} locked {locked === 1 ? 'grid is' : 'grids are'} kept.

)} ), confirmLabel: 'Remove grids', onConfirm: () => guard(async () => { const r = await api.clearGrids(ids) afterBulk(ids, true) notify( 'success', [`Removed ${r.cleared} grid${r.cleared === 1 ? '' : 's'}`, r.locked ? `${r.locked} locked, kept` : ''] .filter(Boolean) .join(' · '), ) }), }) } const confirmClearHotcues = (ids: string[]) => setConfirmReq({ title: 'Remove hotcues', body: (

Clear every hotcue on {trackNoun(ids)} — cues, loops and the grid’s beat-1 cue? The beatgrid itself is kept.

), confirmLabel: 'Remove hotcues', onConfirm: () => guard(async () => { const r = await api.clearHotcues(ids) afterBulk(ids, true) notify('success', `Removed ${r.cues} hotcue${r.cues === 1 ? '' : 's'} from ${nTracks(r.tracks)}`) }), }) const confirmRemoveTracks = (ids: string[]) => setConfirmReq({ title: 'Remove from collection', body: ( <>

Remove {trackNoun(ids)} from the collection and from every playlist?

{capabilities.data &&

{removalNote(capabilities.data, ids.length)}

} ), confirmLabel: `Remove ${nTracks(ids.length)}`, onConfirm: () => guard(async () => { const r = await api.removeTracks(ids) setSelected(new Set()) if (prepTrack && ids.includes(prepTrack.id)) setPrepTrack(null) afterBulk(ids, false) notify('success', `Removed ${nTracks(r.removed)} from the collection`) }), }) const canClearGrids = canEdit && !viewForeign && !!capabilities.data?.grid.editable const canClearCues = canAutoCue const canRemoveTracks = canEdit && !viewForeign && !!capabilities.data?.tracks.removable // The COLLECTION's capabilities, deliberately: adding happens while a folder // or device is on screen, whose own capabilities are read-only. const canAddTracks = canEdit && !!capabilities.data?.tracks.addable const exportRootId = source.kind === 'export' ? source.id : source.kind === 'export-other' ? source.exportId : null const removeItems = (ids: string[]): MenuItem[] => [ ...(playlistEditable ? [{ label: 'From this playlist…', onClick: () => confirmRemoveFromPlaylist(ids) }] : []), // Only on the export's ROOT view. Inside a referenced playlist there is // nothing to remove: the reference is live, and per-track removal would // need an exclusion list — hidden state deciding what a future export // contains. Remove the whole playlist, or add tracks instead. ...(exportRootId ? [{ label: 'From this export…', onClick: () => confirmRemoveFromExport(exportRootId, ids) }] : []), // Removing touches cues and grids a running batch is writing; the backend // refuses, so the items say why instead of failing on click. ...(canClearGrids ? [{ label: 'Grids…', hint: batchJob ? 'busy' : undefined, disabled: !!batchJob, onClick: () => confirmClearGrids(ids) }] : []), ...(canClearCues ? [{ label: 'Hotcues…', hint: batchJob ? 'busy' : undefined, disabled: !!batchJob, onClick: () => confirmClearHotcues(ids) }] : []), ...(canRemoveTracks ? [{ label: 'From collection…', danger: true, hint: batchJob ? 'busy' : undefined, disabled: !!batchJob, onClick: () => confirmRemoveTracks(ids) }] : []), ] // The "Add to" submenu. Exports are listed even on a read-only library: // adding to one touches no library data — an export is Konduktor's own // curation. The playlist being viewed is left out; adding a track to the // playlist it came from would only duplicate it. const addToItems = (ids: string[]): MenuItem[] => { const lists = addablePlaylists.filter((p) => !(source.kind === 'playlist' && p.id === source.id)) return [ { heading: 'Playlists', empty: lists.length ? undefined : 'No playlists' }, ...lists.map((p) => ({ label: p.name, hint: String(p.count), onClick: () => addToPlaylist(p.id, ids), })), { heading: 'Exports', empty: exportSets.data?.length ? undefined : 'No exports yet' }, ...(exportSets.data ?? []).map((set) => ({ label: set.name, icon: , onClick: () => addToExport(set.id, ids), })), ] } // The same targets as "Add to", but each opens the add dialog: a loose file // has to join the collection before a playlist or export can hold it. const folderAddToItems = (ids: string[]): MenuItem[] => [ { heading: 'Playlists', empty: addablePlaylists.length ? undefined : 'No playlists' }, ...addablePlaylists.map((p) => ({ label: p.name, hint: String(p.count), onClick: () => setAdding({ ids, target: { kind: 'playlist' as const, id: p.id, name: p.name } }), })), { heading: 'Exports', empty: exportSets.data?.length ? undefined : 'No exports yet' }, ...(exportSets.data ?? []).map((set) => ({ label: set.name, icon: , onClick: () => setAdding({ ids, target: { kind: 'export' as const, id: set.id, name: set.name } }), })), ] return ( {/* Floating glass panels with gaps between them: the ambient backdrop (main.tsx) shows through the gutters as well as through the glass. */}
setToast(null)} /> {menu && ( loadTrack(menu.track) }] : []), // Offering "Edit Tags…" on a read-only library would open a dialog // whose every save is refused, so it is not offered at all. ...(canEdit && !viewForeign && menu.ids.length === 1 ? [{ label: 'Edit Tags…', onClick: () => setEditing(menu.track) }] : []), ...(canAnalyzeGrid ? [ { label: 'Analyze Grid & BPM', hint: batchJob ? 'busy' : menu.ids.length > 1 ? String(menu.ids.length) : undefined, disabled: !!batchJob, onClick: () => startGridAnalysis(menu.ids), }, ] : []), // The deck's ✨ Auto covers a single loaded track, with its // existing cues shown; this is the same template over the selection. ...(canAutoCue ? [ { label: 'Auto Hotcues…', hint: batchJob ? 'busy' : menu.ids.length > 1 ? String(menu.ids.length) : undefined, disabled: !!batchJob, onClick: () => startCueBatch(menu.ids), }, ] : []), // Shown disabled WITH a reason where the platform cannot play stem // files as stems, rather than hidden: a missing item reads as a bug. ...(canEdit && !viewForeign ? [ canStems ? { label: 'Convert to Stems…', hint: batchJob ? 'busy' : menu.ids.length > 1 ? String(menu.ids.length) : undefined, disabled: !!batchJob, onClick: () => setStemDialog(menu.ids), } : { label: 'Convert to Stems…', disabled: true, title: 'This library\'s DJ app does not play native-instruments stem files as stems', onClick: () => {}, }, ] : []), // A device's track ids belong to the stick, not the collection, so // there is nothing they could be added to. A folder's files CAN be // added — through the add dialog, which puts them in the // collection first. ...(viewingFolder ? canAddTracks ? [ { label: 'Add to Collection…', onClick: () => setAdding({ ids: menu.ids, target: null }) }, { label: 'Add to', submenu: folderAddToItems(menu.ids) }, ] : [{ label: 'Add to Collection…', disabled: true, title: 'Tracks cannot be added to this library', onClick: () => {}, }] : viewingDevice ? [] : [{ label: 'Add to', submenu: addToItems(menu.ids) }]), ...(() => { const items = removeItems(menu.ids) return items.length ? [{ label: 'Remove', submenu: items }] : [] })(), ]} onClose={() => setMenu(null)} /> )} {headerMenu && ( ({ label: c.label, checked: columnVisibility[c.id] !== false, onToggle: () => setColumnVisibility((v) => ({ ...v, [c.id]: v[c.id] === false })), })), { separator: true as const }, { label: 'Reset to defaults', onClick: resetColumns }, ]} onClose={() => setHeaderMenu(null)} /> )} {stemDialog && ( setStemDialog(null)} onStarted={(job) => { setBatchCancelling(false) setBatchJob({ id: job.id, kind: 'stems' }) }} onDownloading={(job) => setEngineJob(job.id)} onError={onError} /> )} {stemReport && setStemReport(null)} />} {editing && ( setEditing(null)} onApplied={(msg) => notify('success', msg)} onError={onError} /> )} {confirmReq && setConfirmReq(null)} />} {cueBatch && ( runCueBatch(cueBatch.ids, slots)} onClose={() => setCueBatch(null)} onError={onError} /> )} {gridConfirm && ( runGridAnalysis(gridConfirm.ids, replace)} onClose={() => setGridConfirm(null)} /> )} {!!relocation.data?.volumes.length && ( { qc.setQueryData(['relocation'], { scanning: false, volumes: [] }) if (!applied) return // Every file-backed answer can change: audio, art, the Files // tree's "already in the collection" marks. qc.invalidateQueries() notify( 'success', `Found ${applied.tracks.toLocaleString()} track${applied.tracks === 1 ? '' : 's'} — remapped for this session`, ) }} onOpenManual={() => setShowPaths(true)} onError={onError} /> )} {showPaths && ( setShowPaths(false)} onNotify={notify} onError={onError} /> )} {showHistory && ( setShowHistory(false)} onNotify={notify} onError={onError} /> )} {importing && ( setImporting(false)} onDone={(msg) => { setImporting(false) notify('success', msg) }} onError={onError} /> )} {adding && ( setAdding(null)} onDone={(msg) => { setAdding(null) notify('success', msg) }} onError={onError} /> )} {/* Prep strip spans the top of the window; the library sits below it. */}
setShowHistory(true)} onImport={() => setImporting(true)} onSwitchLibrary={() => setForcePicker(true)} onOpenLibrary={(path) => void openForEditing(path)} onDone={(msg) => notify('success', msg)} onOpenPathMapping={() => setShowPaths(true)} onDiscarded={() => afterBulk(prepTrack ? [prepTrack.id] : [], true)} />
{/* Search / filters — always visible. Columns are chosen by right-clicking the header. */} {(!isAll || viewForeign) && (
{viewName} {viewingFolder ? ( <> {filtersActive ? `${filtered.length} of ${tracks.length} files` : `${tracks.length} file${tracks.length === 1 ? '' : 's'}`} {heldInFolder.size > 0 && ` · ${heldInFolder.size} already in your collection`} {' · this folder only'} Not in collection {tracks.length > 0 && canAddTracks && ( )} ) : viewingExport ? ( <> {filtersActive ? `${filtered.length} of ${tracks.length} tracks` : `${tracks.length} tracks`} {source.kind === 'export' ? ' · everything this export would ship' : source.kind === 'export-other' ? ' · added individually — these become an “Other” playlist' : ' · live — what ships is whatever this playlist holds at export time'} Export ) : viewingDevice ? ( <> {filtersActive ? `${filtered.length} of ${tracks.length} tracks` : `${tracks.length} tracks`} Device · read-only {canAddTracks && } ) : ( {!playlistEditable ? `${filtersActive ? `${filtered.length} of ` : ''}${tracks.length} tracks` : filtersActive || sorting.length > 0 ? `${filtersActive ? `${filtered.length} of ` : ''}${tracks.length} tracks · clear the ${ filtersActive ? 'filter' : 'sort' } to reorder` : `${tracks.length} tracks · drag rows to reorder · Delete to remove`} )}
)}
{loading ? (
Loading…
) : viewingExport ? ( /* An export uses the plain table, NOT the playlist one. The playlist table's × removes a track from the user's REAL playlist — same pixels, opposite meaning — and an export's own removal is offered through the context menu instead, on its root view only. Selection stays on, so right-click → Add to works from inside an export. */ filtered.length === 0 ? (
{tracks.length === 0 ? 'Nothing in this export yet' : 'No tracks match'}
{tracks.length === 0 ? 'Right-click tracks in All Tracks and choose Add to, or add a whole playlist.' : 'Try clearing some filters.'}
) : ( openMenu(track, x, y, true)} onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })} onPlay={playTrack} onEditField={canEdit ? editField : undefined} activeTrackId={prepTrack?.id ?? null} activePlaying={deckPlaying} columnVisibility={columnVisibility} columnOrder={columnOrder} columnSizing={columnSizing} onColumnOrderChange={setColumnOrder} onColumnSizingChange={setColumnSizing} /> ) ) : viewingFolder ? ( /* A folder uses the plain table too, WITH selection: its bulk action is adding the selected files to the collection. The # column checks off the files the collection already holds. */ folderView.isError ? (
Can’t read this folder
{(folderView.error as Error).message}
) : filtered.length === 0 ? (
{tracks.length === 0 ? 'No music here' : 'No tracks match'}
{tracks.length === 0 ? 'This folder has no audio files directly in it — try one of its subfolders.' : 'Try clearing some filters.'}
) : ( openMenu(track, x, y, true)} onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })} onPlay={playTrack} activeTrackId={prepTrack?.id ?? null} activePlaying={deckPlaying} columnVisibility={columnVisibility} columnOrder={columnOrder} columnSizing={columnSizing} onColumnOrderChange={setColumnOrder} onColumnSizingChange={setColumnSizing} /> ) ) : viewingDevice ? ( /* A device uses the plain table, not the playlist one: a stick's playlists cannot be reordered or have entries removed, so the drag handle and the × would exist only to be inert. Selection is omitted too: its bulk action, "Add to", targets the loaded COLLECTION's playlists, and these ids belong to the device. */ filtered.length === 0 ? (
{tracks.length === 0 ? 'Nothing here' : 'No tracks match'}
{tracks.length === 0 ? 'This device has no tracks in view.' : 'Try clearing some filters.'}
) : ( openMenu(track, x, y, false)} onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })} onPlay={playTrack} activeTrackId={prepTrack?.id ?? null} activePlaying={deckPlaying} columnVisibility={columnVisibility} columnOrder={columnOrder} columnSizing={columnSizing} onColumnOrderChange={setColumnOrder} onColumnSizingChange={setColumnSizing} /> ) ) : isAll ? ( filtered.length === 0 ? (
No tracks match
Try clearing some filters.
) : ( openMenu(track, x, y, true)} onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })} onPlay={playTrack} onEditField={canEdit ? editField : undefined} activeTrackId={prepTrack?.id ?? null} activePlaying={deckPlaying} columnVisibility={columnVisibility} columnOrder={columnOrder} columnSizing={columnSizing} onColumnOrderChange={setColumnOrder} onColumnSizingChange={setColumnSizing} /> ) ) : tracks.length === 0 ? (
This playlist is empty
Go to All Tracks, select tracks, and add them here.
) : filtered.length === 0 ? (
No tracks match
Try clearing some filters.
) : ( openMenu(track, x, y, true)} onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })} onPlay={playTrack} onEditField={canEdit ? editField : undefined} activeTrackId={prepTrack?.id ?? null} activePlaying={deckPlaying} columnVisibility={columnVisibility} columnOrder={columnOrder} columnSizing={columnSizing} onColumnOrderChange={setColumnOrder} onColumnSizingChange={setColumnSizing} /> )}
{/* A slim capsule of its own under both columns, as on the deck above. */} { setBatchCancelling(true) api.cancelJob(batchJob.id).catch((e) => onError((e as Error).message)) }, } : engineJob ? { label: 'Downloading the stem engine', unit: 'bytes', done: engineJobStatus.data?.done ?? 0, total: engineJobStatus.data?.total ?? 0, detail: engineJobStatus.data?.message, onCancel: () => { api.cancelJob(engineJob).catch((e) => onError((e as Error).message)) }, } : null } loading={loading} collectionName={capabilities.data ? libraryName : null} onChangeCollection={() => { // The same question the sidebar header asks: this button used to // switch library without it, silently dropping unsaved edits. void confirmDiscardUnsaved().then((ok) => ok && setForcePicker(true)) }} />
) }