From a081a7a44d6c8df5c0bfafb1a91efcdb918c1e48 Mon Sep 17 00:00:00 2001 From: Benjamin Hardy Date: Wed, 23 Sep 2026 09:43:31 +0200 Subject: [PATCH] Import now allows user to select copy location --- frontend/src/components/FolderPicker.tsx | 122 +++++++++++++++++++++++ frontend/src/components/ImportDialog.tsx | 86 ++++++++++++++-- 2 files changed, 197 insertions(+), 11 deletions(-) create mode 100644 frontend/src/components/FolderPicker.tsx diff --git a/frontend/src/components/FolderPicker.tsx b/frontend/src/components/FolderPicker.tsx new file mode 100644 index 0000000..1d42526 --- /dev/null +++ b/frontend/src/components/FolderPicker.tsx @@ -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(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 ( +
+
+
+

Choose a folder

+
+ +
+ + + + {here || 'Loading…'} + +
+ +
+ {listing.isLoading &&
Loading…
} + {data && data.dirs.length === 0 && ( +
No folders here.
+ )} + {data?.dirs.map((d) => ( + + ))} +
+ + {/* 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. */} +
+ New folder + 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" + /> +
+ +
+ + +
+
+
+ ) +} diff --git a/frontend/src/components/ImportDialog.tsx b/frontend/src/components/ImportDialog.tsx index 176f0c7..cb32af0 100644 --- a/frontend/src/components/ImportDialog.tsx +++ b/frontend/src/components/ImportDialog.tsx @@ -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(null) const [starting, setStarting] = useState(false) const pollRef = useRef(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) => { + 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 && ( + { + setBaseFolder(path) + remember({ importFolder: path }) + }} + onClose={() => setBrowsing(false)} + /> + )}
@@ -132,14 +165,44 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
{!job && ( <> -
+ ) }