Import now allows user to select copy location

This commit is contained in:
patben8 committed 2026-09-23 09:43:31 +02:00
1 parent 06137f5c99
commit a081a7a44d
2 files changed
+197 -11

No files matched your search

+122
View File
@@ -0,0 +1,122 @@
import { useState } from 'react'
import { useQuery } from '@tanstack/react-query'
import { api } from '../api'
/**
* 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.
*/
interface Props {
value: string
onChange: (path: string) => void
onClose: () => void
}
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('')
const here = data?.path ?? ''
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="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…'}
</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"
>
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"
>
Use this folder
</button>
</div>
</div>
</div>
)
}
+75 -11
View File
@@ -1,6 +1,7 @@
import { useEffect, useRef, useState } from 'react'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { api, type ImportPreview, type JobStatus } from '../api'
import { FolderPicker } from './FolderPicker'
/**
* Import a browsed device into the loaded collection.
@@ -32,15 +33,36 @@ interface Props {
export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError }: Props) {
const qc = useQueryClient()
const [destination, setDestination] = useState('')
// The BASE folder, and whether to put this device's audio in its own
// subfolder of it. Kept apart rather than as one path because they are two
// different decisions: where your music lives, and how one import is filed
// inside it. Joining them for display would make the toggle look like it was
// editing the folder you picked.
const [baseFolder, setBaseFolder] = useState('')
const [useSubfolder, setUseSubfolder] = useState(true)
const [browsing, setBrowsing] = useState(false)
const [job, setJob] = useState<JobStatus | null>(null)
const [starting, setStarting] = useState(false)
const pollRef = useRef<number | null>(null)
// A sensible default the user can override. `~` is expanded server-side.
// Both come from prefs, so the choice survives the dialog closing. `~` is
// expanded server-side, so the default needs no knowledge of the home path.
const prefs = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs })
const prefsLoaded = prefs.isSuccess
useEffect(() => {
if (!destination) setDestination(`~/Music/Konduktor Imports/${deviceLabel}`)
}, [deviceLabel, destination])
if (!prefsLoaded || baseFolder) return
const p = prefs.data as { importFolder?: string; importSubfolder?: boolean }
setBaseFolder(p?.importFolder || '~/Music/Konduktor Imports')
if (typeof p?.importSubfolder === 'boolean') setUseSubfolder(p.importSubfolder)
}, [prefsLoaded, prefs.data, baseFolder])
const remember = (patch: Record<string, unknown>) => {
api.patchPrefs(patch).catch(() => {
/* best-effort: a failed pref write must not block an import */
})
}
const destination = useSubfolder && baseFolder ? `${baseFolder}/${deviceLabel}` : baseFolder
const body = {
destination,
@@ -120,6 +142,17 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
const pct = job && job.total > 0 ? Math.min(100, (job.done / job.total) * 100) : null
return (
<>
{browsing && (
<FolderPicker
value={baseFolder}
onChange={(path) => {
setBaseFolder(path)
remember({ importFolder: path })
}}
onClose={() => setBrowsing(false)}
/>
)}
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4">
<div className="w-full max-w-lg rounded-lg border border-line bg-ink-900 shadow-xl">
<div className="border-b border-line px-5 py-3">
@@ -132,14 +165,44 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
<div className="space-y-4 px-5 py-4 text-sm">
{!job && (
<>
<label className="block">
<div>
<span className="mb-1 block text-xs text-muted">Copy audio into</span>
<input
value={destination}
onChange={(e) => setDestination(e.target.value)}
className="w-full rounded-md border border-line bg-ink-950 px-2 py-1.5 text-sm text-text outline-none focus:border-accent"
/>
</label>
<div className="flex items-center gap-2">
<span
title={baseFolder}
className="min-w-0 flex-1 truncate rounded-md border border-line bg-ink-950 px-2 py-1.5 font-mono text-xs text-text"
dir="rtl"
>
{baseFolder || 'Loading…'}
</span>
<button
onClick={() => setBrowsing(true)}
className="shrink-0 rounded-md border border-line px-3 py-1.5 text-sm text-muted hover:text-text"
>
Browse…
</button>
</div>
<label className="mt-2 flex cursor-pointer items-center gap-2 text-xs text-muted">
<input
type="checkbox"
checked={useSubfolder}
onChange={(e) => {
setUseSubfolder(e.target.checked)
remember({ importSubfolder: e.target.checked })
}}
className="accent-accent"
/>
Put them in a “{deviceLabel}” subfolder
</label>
{/* The resolved path, always visible: the toggle changes where
files actually land, and that should never have to be
inferred from a checkbox. */}
<div className="mt-1 truncate font-mono text-[11px] text-faint" dir="rtl">
{destination}
</div>
</div>
{preview.isLoading && <div className="text-faint">Checking…</div>}
{preview.isError && (
@@ -259,5 +322,6 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
</div>
</div>
</div>
</>
)
}