import type { ReactNode } from 'react' import { useQuery } from '@tanstack/react-query' import { api, type FsListing, type FsPlace } from '../api' import { Icon, type IconName } from '../lib/icons' /** * The one file browser. A places rail, a path bar, a listing. * * Deliberately NOT the surrounding chrome. Its two callers frame it very * differently — the collection picker shows it as a step inside an existing * full-screen card, the folder picker as a modal stacked on the import dialog — * and a shared wrapper would put a modal inside a modal. So each caller keeps * its own frame and passes its own confirm affordance as `footer`. * * `path` is controlled for the same reason: the caller has to know where you * are standing to label that footer ("Open this folder", "Use this folder"), so * the current directory belongs to the caller and not to this component. */ /** * The listing, keyed once. * * Exported because a caller needs the RESOLVED path — the server turns * `undefined` into home — and reading it through the same query is honest * sharing: TanStack dedupes the request, and there is exactly one definition of * the cache key. Passing it back up through a callback would mean an effect * firing on every navigation to tell the parent what it already asked for. */ export function useFsListing(path: string | undefined, platform?: string) { return useQuery({ queryKey: ['fs', path ?? '~', platform ?? null], queryFn: () => api.listDir(path, platform), }) } // Volumes come and go while a dialog is open, so the rail polls. Slower than // the sidebar's device poll: this is a shortcut list, not the thing you came for. const PLACES_POLL_MS = 5000 const ICONS: Record = { home: 'home', music: 'music', desktop: 'desktop', documents: 'file', downloads: 'download', volume: 'drive', library: 'playlist', } interface Props { /** Whether files are listed at all. `directory` shows folders only. */ mode: 'file' | 'directory' /** Narrows which files are openable, and adds that platform's own location. */ platform?: string /** Current directory; `undefined` means "wherever the server calls home". */ path: string | undefined onNavigate: (path: string) => void /** Called when a listed FILE is chosen. Unused in `directory` mode. */ onPickFile?: (path: string) => void /** The caller's confirm row, pinned under the listing. */ footer?: ReactNode } export function FileBrowser({ mode, platform, path, onNavigate, onPickFile, footer }: Props) { const listing = useFsListing(path, platform) const places = useQuery({ queryKey: ['fs-places', platform ?? null], queryFn: () => api.places(platform), refetchInterval: PLACES_POLL_MS, }) const data: FsListing | undefined = listing.data const here = data?.path ?? '' const rows = places.data ?? [] // Volatile section LAST: a drive appearing or vanishing must not shift the // rows above it under a cursor that is already moving towards one. const groups: { label: string; rows: FsPlace[] }[] = [ { label: 'Places', rows: rows.filter((p) => p.kind !== 'volume' && p.kind !== 'library') }, { label: 'Library', rows: rows.filter((p) => p.kind === 'library') }, { label: 'Drives', rows: rows.filter((p) => p.kind === 'volume') }, ].filter((g) => g.rows.length > 0) return (
{/* ---- Places rail. Navigates, never selects: one click here must not be able to open a library or commit an import destination, because a sidebar does not look like something that does that. ---- */}
{groups.map((g) => (
{g.label}
{g.rows.map((p) => { const active = here === p.path return ( ) })}
))}
{/* ---- Path bar + listing ---- */}
{here || 'Loading…'}
{listing.isLoading &&
Loading…
} {data && data.dirs.length === 0 && data.files.length === 0 && (
{mode === 'directory' ? 'No folders here.' : 'Nothing openable here.'}
)} {data?.dirs.map((d) => ( ))} {/* The server already filtered these to the chosen platform's own, so a Traktor browse never offers a master.db it cannot open — and a directory-selecting platform is sent no files at all. */} {mode === 'file' && data?.files.map((f) => ( ))}
{footer}
) }