Improved file browser

This commit is contained in:
patben8 committed 2026-09-23 11:19:58 +02:00
1 parent 8d11acd783
commit 93ec2ef425
10 files changed
+540 -204

No files matched your search

+9
View File
@@ -324,6 +324,13 @@ export interface SourceStatus {
playlists: number | null
}
export interface FsPlace {
/** Groups the row and picks its icon. Never a finished sentence. */
kind: 'home' | 'music' | 'desktop' | 'documents' | 'downloads' | 'volume' | 'library'
name: string
path: string
}
export interface FsEntry {
name: string
path: string
@@ -432,6 +439,8 @@ export const api = {
getJSON<CollectionOptions>(`/api/library/options${qs({ platform })}`),
listDir: (path?: string, platform?: string) =>
getJSON<FsListing>(`/api/fs/list${qs({ path, platform })}`),
// Re-asked rather than cached: drives come and go while a dialog is open.
places: (platform?: string) => getJSON<FsPlace[]>(`/api/fs/places${qs({ platform })}`),
// ---- path remapping (per-collection OS-path prefix translation) ----
getPathMapping: () => getJSON<PathMapping>('/api/library/path-mapping'),
+31 -82
View File
@@ -1,6 +1,7 @@
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'
/**
@@ -79,18 +80,15 @@ export function CollectionPicker({ onOpened, onCancel }: Props) {
enabled: !!platform,
})
const listing = useQuery({
queryKey: ['fs', dir ?? '~', platform?.platform],
queryFn: () => api.listDir(dir, platform?.platform),
enabled: step === 'browse',
})
// 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 data = listing.data
const opt = options.data
const auto = opt?.auto ?? null
const recent = opt?.recent ?? null
@@ -257,82 +255,33 @@ export function CollectionPicker({ onOpened, onCancel }: Props) {
) : (
/* ---- Step 3: browse for it ---- */
<>
{/* 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. Files are filtered server-side to this platform's own,
so a Traktor browse never offers a master.db it cannot open — and
a drive-selecting platform gets no files at all. */}
<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">
{picksDirectory ? 'No folders here.' : 'Nothing openable 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>
{/* 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 && (
<div className="flex items-center gap-2 border-t border-line px-4 py-2">
<span className="min-w-0 flex-1 truncate text-xs text-faint">
Open <span className="font-mono text-muted">{data?.path ?? '…'}</span> as a{' '}
{platform!.name} library
</span>
<button
disabled={!data?.path || open.isPending}
onClick={() => data?.path && open.mutate(data.path)}
className="shrink-0 rounded-md bg-accent px-3 py-1.5 text-sm font-medium text-ink-950 hover:brightness-110 disabled:cursor-not-allowed disabled:opacity-40"
>
{open.isPending ? 'Opening…' : 'Open this folder'}
</button>
</div>
)}
<FileBrowser
mode={picksDirectory ? 'directory' : 'file'}
platform={platform!.platform}
path={dir}
onNavigate={setDir}
onPickFile={(p) => 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 ? (
<div className="flex items-center gap-2 border-t border-line px-4 py-2">
<span className="min-w-0 flex-1 truncate text-xs text-faint">
Open <span className="font-mono text-muted">{here || '…'}</span> as a{' '}
{platform!.name} library
</span>
<button
disabled={!here || open.isPending}
onClick={() => here && open.mutate(here)}
className="shrink-0 rounded-md bg-accent px-3 py-1.5 text-sm font-medium text-ink-950 hover:brightness-110 disabled:cursor-not-allowed disabled:opacity-40"
>
{open.isPending ? 'Opening…' : 'Open this folder'}
</button>
</div>
) : null
}
/>
{/* Manual path + errors */}
<div className="border-t border-line px-4 py-3">
+179
View File
@@ -0,0 +1,179 @@
import type { ReactNode } from 'react'
import { useQuery } from '@tanstack/react-query'
import { api, type FsListing, type FsPlace } from '../api'
/**
* 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<FsPlace['kind'], string> = {
home: '⌂',
music: '♫',
desktop: '🖥',
documents: '🗎',
downloads: '⬇',
volume: '⬒',
library: '◈',
}
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 (
<div className="flex min-h-0 flex-1">
{/* ---- 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. ---- */}
<div className="w-44 shrink-0 overflow-y-auto border-r border-line bg-ink-950/40 py-2">
{groups.map((g) => (
<div key={g.label} className="mb-2">
<div className="px-3 pb-1 text-[10px] font-semibold uppercase tracking-wider text-faint">
{g.label}
</div>
{g.rows.map((p) => {
const active = here === p.path
return (
<button
key={p.path}
onClick={() => onNavigate(p.path)}
title={p.path}
className={`flex w-full items-center gap-2 px-3 py-1.5 text-left text-xs transition-colors ${
active
? 'bg-accent-soft text-text'
: 'text-muted hover:bg-ink-800 hover:text-text'
}`}
>
<span className="w-3 shrink-0 text-center text-faint">{ICONS[p.kind]}</span>
<span className="truncate">{p.name}</span>
</button>
)
})}
</div>
))}
</div>
{/* ---- Path bar + listing ---- */}
<div className="flex min-w-0 flex-1 flex-col">
<div className="flex items-center gap-2 border-b border-line bg-ink-850 px-4 py-2">
<button
title="Home"
onClick={() => data?.home && onNavigate(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 && onNavigate(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">
{here || 'Loading…'}
</span>
</div>
<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">
{mode === 'directory' ? 'No folders here.' : 'Nothing openable here.'}
</div>
)}
{data?.dirs.map((d) => (
<button
key={d.path}
onClick={() => onNavigate(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>
))}
{/* 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) => (
<button
key={f.path}
onClick={() => onPickFile?.(f.path)}
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>
{footer}
</div>
</div>
)
}
+43 -80
View File
@@ -1,19 +1,13 @@
import { useState } from 'react'
import { useQuery } from '@tanstack/react-query'
import { api } from '../api'
import { FileBrowser, useFsListing } from './FileBrowser'
/**
* Pick a FOLDER, by browsing.
*
* `/api/fs/list` already returns both directories and openable library files —
* it was built for the collection picker — so this reuses it and simply ignores
* the files. A folder browser that listed `.nml` files would be offering
* something that cannot be chosen.
*
* Navigating into a folder also SELECTS it. There is no separate "choose"
* click, because the thing being picked is the place you are standing: a picker
* that made you navigate in and then confirm would read as two decisions when
* there is one.
* The browsing itself is `FileBrowser` — the same one the collection picker
* uses. All this adds is the modal frame and the confirm row, which is the part
* that genuinely differs: here you are choosing somewhere to PUT files, so the
* target can be a folder that does not exist yet.
*/
interface Props {
@@ -26,92 +20,61 @@ export function FolderPicker({ value, onChange, onClose }: Props) {
// `undefined` means "wherever the server thinks home is" — the picker does not
// need to know that path to open on it.
const [dir, setDir] = useState<string | undefined>(value || undefined)
const listing = useQuery({
queryKey: ['fs', dir ?? 'home'],
queryFn: () => api.listDir(dir),
})
const data = listing.data
const [newFolder, setNewFolder] = useState('')
// Shares FileBrowser's query, so "where am I" cannot disagree with what is
// on screen — and the confirm row below needs it to name its target.
const here = useFsListing(dir).data?.path ?? ''
const here = data?.path ?? ''
const confirm = () => {
onChange(newFolder.trim() ? `${here}/${newFolder.trim()}` : here)
onClose()
}
return (
<div className="fixed inset-0 z-[60] flex items-center justify-center bg-black/60 p-4">
<div className="flex h-[70vh] w-full max-w-lg flex-col rounded-lg border border-line bg-ink-900 shadow-xl">
<div className="flex h-[70vh] w-full max-w-2xl flex-col overflow-hidden rounded-lg border border-line bg-ink-900 shadow-xl">
<div className="border-b border-line px-5 py-3">
<h2 className="text-sm font-semibold text-text">Choose a folder</h2>
</div>
<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">
{here || 'Loading…'}
<FileBrowser
mode="directory"
path={dir}
onNavigate={setDir}
footer={
/* A folder that does not exist yet is a normal thing to want — the
import creates it, so it only has to be named, not made here. */
<div className="flex items-center gap-2 border-t border-line px-4 py-2">
<span className="shrink-0 text-xs text-faint">New folder</span>
<input
value={newFolder}
placeholder="name…"
onChange={(e) => setNewFolder(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter' && newFolder.trim() && here) confirm()
}}
className="min-w-0 flex-1 rounded-md border border-line bg-ink-950 px-2 py-1 text-xs text-text outline-none focus:border-accent"
/>
</div>
}
/>
<div className="flex items-center gap-2 border-t border-line px-5 py-3">
{/* Always show where the files will actually land, so the effect of
typing a new-folder name never has to be inferred. */}
<span className="min-w-0 flex-1 truncate font-mono text-[11px] text-faint" dir="rtl">
{here ? (newFolder.trim() ? `${here}/${newFolder.trim()}` : here) : ''}
</span>
</div>
<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 && (
<div className="p-4 text-sm text-faint">No folders 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>
))}
</div>
{/* A folder that does not exist yet is a normal thing to want — the
import creates it, so it only has to be named, not made here. */}
<div className="flex items-center gap-2 border-t border-line px-4 py-2">
<span className="text-xs text-faint">New folder</span>
<input
value={newFolder}
placeholder="name…"
onChange={(e) => setNewFolder(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter' && newFolder.trim() && here) {
onChange(`${here}/${newFolder.trim()}`)
onClose()
}
}}
className="min-w-0 flex-1 rounded-md border border-line bg-ink-950 px-2 py-1 text-xs text-text outline-none focus:border-accent"
/>
</div>
<div className="flex justify-end gap-2 border-t border-line px-5 py-3">
<button
onClick={onClose}
className="rounded-md border border-line px-3 py-1.5 text-sm text-muted hover:text-text"
className="shrink-0 rounded-md border border-line px-3 py-1.5 text-sm text-muted hover:text-text"
>
Cancel
</button>
<button
disabled={!here}
onClick={() => {
onChange(newFolder.trim() ? `${here}/${newFolder.trim()}` : here)
onClose()
}}
className="rounded-md bg-accent px-3 py-1.5 text-sm font-medium text-ink-950 hover:brightness-110 disabled:opacity-40"
onClick={confirm}
className="shrink-0 rounded-md bg-accent px-3 py-1.5 text-sm font-medium text-ink-950 hover:brightness-110 disabled:opacity-40"
>
Use this folder
</button>