import { useState } from 'react' import { useMutation, useQuery } from '@tanstack/react-query' import { api, type CollectionCandidate, type CollectionStatus, type PlatformOption } from '../api' import { FileBrowser, useFsListing } from './FileBrowser' import { browsePrompt, platformAvailability } from '../lib/platformCopy' /** * Choose a library to open, in two steps: WHICH PLATFORM, then which library. * * The platform comes first because it is the question every later answer * depends on. Asked the other way round — one flat list of everything found — * "Automatic" had to guess across platforms, and it guessed by adapter * REGISTRATION ORDER: a plugged-in USB stick could be offered as the user's * collection because its driver happened to be imported first. Scoping the * shortcuts to a chosen platform removes that class of mistake by construction * rather than by ranking candidates more cleverly. * * Nothing is remembered between launches. The platform list is where every * session starts, so the choice is always visible and never inherited from a * decision made days ago. */ interface Props { onOpened: (status: CollectionStatus) => void onCancel?: () => void // shown only when switching from an already-loaded library } 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 [step, setStep] = useState<'platform' | 'library' | 'browse'>('platform') const [platform, setPlatform] = useState(null) const [dir, setDir] = useState(undefined) // undefined => home const [manual, setManual] = useState('') // The picker runs BEFORE a library is open, so capabilities do not exist yet — // /api/platforms is the pre-load equivalent, and it is the whole of step one. const platforms = useQuery({ queryKey: ['platforms'], queryFn: api.platforms }) const options = useQuery({ queryKey: ['collectionOptions', platform?.platform], queryFn: () => api.collectionOptions(platform!.platform), enabled: !!platform, }) // Shares FileBrowser's query, so the confirm row below cannot disagree with // what is on screen about where the user is standing. const here = useFsListing(dir, platform?.platform).data?.path ?? '' const open = useMutation({ mutationFn: (path: string) => api.openCollection(path), onSuccess: (status) => onOpened(status), }) const opt = options.data const auto = opt?.auto ?? null const recent = opt?.recent ?? null const recentAvailable = !!recent && recent.exists // `auto` is the first of these, so the rest are the ones a single "Automatic" // card would have thrown away. Two Traktor versions, or two sticks, is normal. const alsoFound = (opt?.detected ?? []).slice(1) // A drive is not a file: its browse lists folders and confirms the one you are // standing in, the way the import folder picker does. const picksDirectory = platform?.selects === 'directory' const goPlatform = () => { setStep('platform') setPlatform(null) setDir(undefined) setManual('') open.reset() } const subtitle = step === 'platform' ? 'Konduktor opens libraries from each of these.' : step === 'library' ? 'Open automatically, resume your last one, or browse.' : platform ? browsePrompt(platform) : '' const openError = open.isError ? (open.error as Error).message : null return (
{/* Header */}
{platform ? `Select your ${platform.name} library` : 'Select your DJ platform'}
{subtitle}
{step === 'browse' && ( )} {step === 'library' && ( )} {onCancel && ( )}
{step === 'platform' ? ( /* ---- Step 1: which platform ---- */
{platforms.isLoading &&
Loading…
} {/* Every platform stays clickable, including the ones with nothing found: a library kept somewhere unusual is still openable by browsing, and a disabled row would say otherwise. */} {platforms.data?.map((p) => ( 0 ? '●' : '○'} title={p.name} subtitle={platformAvailability(p)} onClick={() => { setPlatform(p) setStep('library') }} /> ))}
) : step === 'library' ? ( /* ---- Step 2: which library of that platform ---- */
auto && open.mutate(auto.path)} /> recent && open.mutate(recent.path)} /> setStep('browse')} /> {/* Anything else detected. Listed rather than ranked away, because "the newest one" is a guess and two of these are a real setup. */} {alsoFound.length > 0 && (
Also found
{alsoFound.map((c: CollectionCandidate) => ( ))}
)} {openError && (
{openError}
)}
) : ( /* ---- Step 3: browse for it ---- */ <> open.mutate(p)} footer={ /* Confirm-the-folder bar, for a platform whose library IS a folder. There is nothing in the listing to click, so the target is the place you have navigated to. */ picksDirectory ? (
Open {here || '…'} as a{' '} {platform!.name} library
) : null } /> {/* Manual path + errors */}
{openError && (
{openError}
)}
setManual(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' && manual.trim()) open.mutate(manual.trim()) }} placeholder={ picksDirectory ? '…or paste a full path to the drive' : `…or paste a full path to ${platform!.library_label}` } 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" />
)}
) }