Added nml loading

This commit is contained in:
Ben committed 2026-07-03 16:40:40 +02:00
1 parent 3ace90179b
commit cc254e9a9b
10 files changed
+400 -59

No files matched your search

+40 -3
View File
@@ -1,5 +1,5 @@
import { useCallback, useMemo, useState } from 'react'
import { useQuery } from '@tanstack/react-query'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import type { SortingState } from '@tanstack/react-table'
import { api, type Track } from './api'
import { Sidebar, type Source } from './components/Sidebar'
@@ -9,6 +9,7 @@ import { PlaylistEditor } from './components/PlaylistEditor'
import { SelectionBar } from './components/SelectionBar'
import { StatusBar } from './components/StatusBar'
import { Toast, type ToastMsg } from './components/Toast'
import { CollectionPicker } from './components/CollectionPicker'
function applyFilters(tracks: Track[], f: Filters): Track[] {
const q = f.search.trim().toLowerCase()
@@ -31,26 +32,34 @@ function applyFilters(tracks: Track[], f: Filters): Track[] {
}
export default function App() {
const qc = useQueryClient()
const [source, setSource] = useState<Source>({ kind: 'all' })
const [filters, setFilters] = useState<Filters>(emptyFilters)
const [sorting, setSorting] = useState<SortingState>([])
const [selected, setSelected] = useState<Set<string>>(new Set())
const [toast, setToast] = useState<ToastMsg | null>(null)
const [forcePicker, setForcePicker] = useState(false)
const notify = useCallback((kind: ToastMsg['kind'], text: string) => {
setToast({ id: Date.now(), kind, text })
}, [])
const onError = useCallback((msg: string) => notify('error', msg), [notify])
const collection = useQuery({ queryKey: ['collection'], queryFn: api.collection })
const loaded = collection.data?.loaded ?? false
// 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: source.kind === 'all',
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: source.kind === 'playlist',
enabled: loaded && source.kind === 'playlist',
})
const isAll = source.kind === 'all'
@@ -62,6 +71,32 @@ export default function App() {
filters,
])
const handleOpened = () => {
setForcePicker(false)
setSource({ kind: 'all' })
setSelected(new Set())
setFilters(emptyFilters)
setSorting([])
qc.invalidateQueries() // refetch everything for the newly-opened collection
}
if (collection.isLoading) {
return (
<div className="flex h-screen w-screen items-center justify-center bg-ink-950 text-muted">
Loading…
</div>
)
}
if (!loaded || forcePicker) {
return (
<CollectionPicker
onOpened={handleOpened}
onCancel={loaded ? () => setForcePicker(false) : undefined}
/>
)
}
const selectSource = (s: Source) => {
setSource(s)
setSorting([])
@@ -143,6 +178,8 @@ export default function App() {
total={tracks.length}
sourceName={isAll ? 'All Tracks' : source.name}
loading={loading}
collectionName={collection.data?.path?.split('/').pop() ?? null}
onChangeCollection={() => setForcePicker(true)}
/>
</main>
</div>
+27
View File
@@ -101,6 +101,26 @@ export interface EditState {
nml_path: string
}
export interface CollectionStatus {
loaded: boolean
path: string | null
tracks: number | null
playlists: number | null
}
export interface FsEntry {
name: string
path: string
}
export interface FsListing {
path: string
parent: string | null
home: string
dirs: FsEntry[]
files: FsEntry[]
}
export interface SaveResult {
saved: boolean
backup: string | null
@@ -142,6 +162,13 @@ function qs(params: Record<string, unknown>): string {
}
export const api = {
// ---- collection selection ----
collection: () => getJSON<CollectionStatus>('/api/collection'),
openCollection: (path: string) =>
send<CollectionStatus>('POST', '/api/collection/open', { path }),
listDir: (path?: string) =>
getJSON<FsListing>(`/api/fs/list${path ? `?path=${encodeURIComponent(path)}` : ''}`),
stats: () => getJSON<Stats>('/api/stats'),
facets: () => getJSON<Facets>('/api/facets'),
playlists: () => getJSON<PlaylistNode[]>('/api/playlists'),
@@ -0,0 +1,135 @@
import { useState } from 'react'
import { useMutation, useQuery } from '@tanstack/react-query'
import { api, type CollectionStatus } from '../api'
interface Props {
onOpened: (status: CollectionStatus) => void
onCancel?: () => void // shown only when switching from an already-loaded collection
}
export function CollectionPicker({ onOpened, onCancel }: Props) {
const [dir, setDir] = useState<string | undefined>(undefined) // undefined => home
const [manual, setManual] = useState('')
const listing = useQuery({
queryKey: ['fs', dir ?? '~'],
queryFn: () => api.listDir(dir),
})
const open = useMutation({
mutationFn: (path: string) => api.openCollection(path),
onSuccess: (status) => onOpened(status),
})
const data = listing.data
return (
<div className="flex h-screen w-screen items-center justify-center bg-ink-950 p-6">
<div className="flex h-[600px] w-full max-w-2xl flex-col overflow-hidden rounded-xl border border-line bg-ink-900 shadow-2xl">
{/* Header */}
<div className="flex items-center gap-3 border-b border-line px-5 py-4">
<div className="flex h-8 w-8 items-center justify-center rounded-md bg-accent text-sm font-bold text-ink-950">
K
</div>
<div className="flex-1">
<div className="text-[15px] font-semibold tracking-tight">
Select your Traktor collection
</div>
<div className="text-xs text-muted">
Choose a <span className="text-text">collection.nml</span> file to open.
</div>
</div>
{onCancel && (
<button
onClick={onCancel}
className="rounded-md px-2.5 py-1.5 text-sm text-muted hover:bg-ink-800 hover:text-text"
>
Cancel
</button>
)}
</div>
{/* Path bar */}
<div className="flex items-center gap-2 border-b border-line bg-ink-850 px-4 py-2">
<button
title="Home"
onClick={() => setDir(data?.home)}
className="rounded px-2 py-1 text-sm text-muted hover:bg-ink-800 hover:text-text"
>
⌂
</button>
<button
title="Up one level"
disabled={!data?.parent}
onClick={() => data?.parent && setDir(data.parent)}
className="rounded px-2 py-1 text-sm text-muted hover:bg-ink-800 hover:text-text disabled:cursor-not-allowed disabled:opacity-40"
>
↑
</button>
<span className="truncate font-mono text-xs text-faint" dir="rtl">
{data?.path ?? 'Loading…'}
</span>
</div>
{/* Listing */}
<div className="min-h-0 flex-1 overflow-y-auto p-2">
{listing.isLoading && <div className="p-4 text-sm text-faint">Loading…</div>}
{data && data.dirs.length === 0 && data.files.length === 0 && (
<div className="p-4 text-sm text-faint">No folders or .nml files here.</div>
)}
{data?.dirs.map((d) => (
<button
key={d.path}
onClick={() => setDir(d.path)}
className="flex w-full items-center gap-2 rounded-md px-3 py-2 text-left text-sm text-muted hover:bg-ink-800 hover:text-text"
>
<span className="text-faint">📁</span>
<span className="truncate">{d.name}</span>
</button>
))}
{data?.files.map((f) => (
<button
key={f.path}
onClick={() => open.mutate(f.path)}
disabled={open.isPending}
className="flex w-full items-center gap-2 rounded-md px-3 py-2 text-left text-sm text-text hover:bg-accent-soft"
>
<span className="text-accent">♫</span>
<span className="truncate">{f.name}</span>
<span className="ml-auto text-[10px] uppercase tracking-wider text-faint">
open
</span>
</button>
))}
</div>
{/* Manual path + errors */}
<div className="border-t border-line px-4 py-3">
{open.isError && (
<div className="mb-2 rounded-md border border-pink/40 bg-ink-850 px-3 py-2 text-xs text-pink">
{(open.error as Error).message}
</div>
)}
<div className="flex items-center gap-2">
<input
value={manual}
onChange={(e) => setManual(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter' && manual.trim()) open.mutate(manual.trim())
}}
placeholder="…or paste a full path to collection.nml"
className="min-w-0 flex-1 rounded-md border border-line bg-ink-850 px-3 py-2 font-mono text-xs text-text outline-none placeholder:text-faint focus:border-accent"
/>
<button
disabled={!manual.trim() || open.isPending}
onClick={() => open.mutate(manual.trim())}
className="rounded-md bg-accent px-4 py-2 text-sm font-medium text-ink-950 hover:brightness-110 disabled:cursor-not-allowed disabled:opacity-40"
>
{open.isPending ? 'Opening…' : 'Open'}
</button>
</div>
</div>
</div>
</div>
)
}
+20 -2
View File
@@ -3,9 +3,18 @@ interface Props {
total: number
sourceName: string
loading?: boolean
collectionName?: string | null
onChangeCollection?: () => void
}
export function StatusBar({ showing, total, sourceName, loading }: Props) {
export function StatusBar({
showing,
total,
sourceName,
loading,
collectionName,
onChangeCollection,
}: Props) {
return (
<div className="flex items-center gap-3 border-t border-line bg-ink-900 px-4 py-2 text-xs text-muted">
<span className="font-medium text-text">{sourceName}</span>
@@ -23,7 +32,16 @@ export function StatusBar({ showing, total, sourceName, loading }: Props) {
)}
</span>
)}
<span className="ml-auto text-faint">konduktor · phase 3</span>
{collectionName && (
<button
onClick={onChangeCollection}
className="ml-auto flex items-center gap-1.5 rounded px-2 py-0.5 text-faint hover:bg-ink-800 hover:text-text"
title="Change collection"
>
<span className="text-[11px]">⎘</span>
<span className="max-w-[220px] truncate font-mono text-[11px]">{collectionName}</span>
</button>
)}
</div>
)
}