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' import { Icon } from '../lib/icons' import { PlatformIcon } from '../lib/platformIcons' import { OptionCard } from './OptionCard' import { RemotePicker } from './RemotePicker' /** * 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', }) } 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 }) // A remote is not found on this computer; its step lists saved servers. const remote = platform?.selects === 'remote' const options = useQuery({ queryKey: ['collectionOptions', platform?.platform], queryFn: () => api.collectionOptions(platform!.platform), enabled: !!platform && !remote, }) // 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' && remote ? 'A library held by a Konduktor server, played and prepared from here.' : 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 && ( )}
{remote ? 'Open a remote library' : 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) => ( } lit={p.found > 0} title={p.name} subtitle={platformAvailability(p)} onClick={() => { setPlatform(p) setStep('library') }} /> ))}
) : step === 'library' && remote ? ( /* ---- Step 2, remote: which server ---- */ ) : step === 'library' ? ( /* ---- Step 2: which library of that platform ---- */
} title="Automatic" subtitle={ auto ? `${auto.label} · modified ${formatWhen(auto.modified)}` : options.isLoading ? `Searching for a ${platform!.name} library…` : platform!.removable ? `No ${platform!.name} drive is connected` : `No ${platform!.name} library in the default location` } disabled={!auto} busy={open.isPending} onClick={() => auto && open.mutate(auto.path)} /> } title="Open last library" subtitle={ recent ? recent.exists ? recent.path : `${recent.path} · no longer exists` : `No previously opened ${platform!.name} library` } disabled={!recentAvailable} busy={open.isPending} onClick={() => recent && open.mutate(recent.path)} /> } title="Find library manually" subtitle={ picksDirectory ? 'Browse for the drive' : `Browse for a ${platform!.library_label}` } busy={open.isPending} onClick={() => 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-lg well px-3 py-2 font-mono text-xs text-text outline-none placeholder:text-faint focus:ring-1 focus:ring-accent" />
)}
) }