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 } function formatWhen(sec: number | null): string { if (!sec) return 'unknown' return new Date(sec * 1000).toLocaleString(undefined, { dateStyle: 'medium', timeStyle: 'short', }) } function OptionCard({ icon, title, subtitle, disabled, busy, onClick, }: { icon: string title: string subtitle: string disabled?: boolean busy?: boolean onClick: () => void }) { return ( ) } export function CollectionPicker({ onOpened, onCancel }: Props) { const [mode, setMode] = useState<'choose' | 'browse'>('choose') const [dir, setDir] = useState(undefined) // undefined => home const [manual, setManual] = useState('') const options = useQuery({ queryKey: ['collectionOptions'], queryFn: api.collectionOptions }) const listing = useQuery({ queryKey: ['fs', dir ?? '~'], queryFn: () => api.listDir(dir), enabled: mode === 'browse', }) const open = useMutation({ mutationFn: (path: string) => api.openCollection(path), onSuccess: (status) => onOpened(status), }) const data = listing.data const opt = options.data const auto = opt?.auto ?? null const recent = opt?.recent ?? null const recentAvailable = !!recent && recent.exists return (
{/* Header */}
Select your Traktor collection
{mode === 'choose' ? 'Open automatically, resume your last one, or browse.' : 'Choose a collection.nml file to open.'}
{mode === 'browse' && ( )} {onCancel && ( )}
{mode === 'choose' ? ( /* ---- Chooser ---- */
auto && open.mutate(auto.path)} /> recent && open.mutate(recent.path)} /> setMode('browse')} /> {open.isError && (
{(open.error as Error).message}
)}
) : ( /* ---- Manual browser ---- */ <> {/* Path bar */}
{data?.path ?? 'Loading…'}
{/* Listing */}
{listing.isLoading &&
Loading…
} {data && data.dirs.length === 0 && data.files.length === 0 && (
No folders or .nml files here.
)} {data?.dirs.map((d) => ( ))} {data?.files.map((f) => ( ))}
{/* Manual path + errors */}
{open.isError && (
{(open.error as Error).message}
)}
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" />
)}
) }