mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented path remapping
This commit is contained in:
1 parent
1e8c53e693
commit
b130f1469e
12 files changed
+968
-16
No files matched your search
@@ -0,0 +1,311 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { api, type RemapPreview } from '../api'
|
||||
import type { ToastMsg } from './Toast'
|
||||
|
||||
interface Props {
|
||||
onClose: () => void
|
||||
onNotify: (kind: ToastMsg['kind'], text: string) => void
|
||||
onError: (msg: string) => void
|
||||
}
|
||||
|
||||
const inputCls =
|
||||
'w-full rounded-md border border-line bg-ink-850 px-2.5 py-1.5 text-sm text-text outline-none focus:border-accent placeholder:text-faint'
|
||||
|
||||
/**
|
||||
* Per-collection OS-path remapping editor. Lets the user translate the stored
|
||||
* audio-file path prefix to a locally-reachable one (portable USB / relocated
|
||||
* library). Saving the mapping is non-destructive — the .nml is never touched;
|
||||
* only how Konduktor resolves files for playback/analysis/tags changes.
|
||||
*/
|
||||
export function PathMappingDialog({ onClose, onNotify, onError }: Props) {
|
||||
const qc = useQueryClient()
|
||||
const saved = useQuery({ queryKey: ['pathMapping'], queryFn: api.getPathMapping })
|
||||
const suggest = useQuery({ queryKey: ['prefixSuggest'], queryFn: api.suggestPrefix })
|
||||
|
||||
const [from, setFrom] = useState('')
|
||||
const [to, setTo] = useState('')
|
||||
const hydrated = useRef(false)
|
||||
const [preview, setPreview] = useState<RemapPreview | null>(null)
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [browsing, setBrowsing] = useState(false)
|
||||
const [confirming, setConfirming] = useState(false)
|
||||
const [committing, setCommitting] = useState(false)
|
||||
|
||||
// Hydrate once: prefer the saved mapping; otherwise prefill `from` with the
|
||||
// auto-detected prefix. Defer until the suggestion resolves when we'd need it.
|
||||
useEffect(() => {
|
||||
if (hydrated.current || !saved.data) return
|
||||
if (!saved.data.from && suggest.isLoading) return
|
||||
hydrated.current = true
|
||||
setTo(saved.data.to)
|
||||
setFrom(saved.data.from || suggest.data?.primary || '')
|
||||
}, [saved.data, suggest.isLoading, suggest.data])
|
||||
|
||||
// Live validation preview (debounced) — how many tracks match / exist.
|
||||
useEffect(() => {
|
||||
if (!from.trim() || !to.trim()) {
|
||||
setPreview(null)
|
||||
return
|
||||
}
|
||||
const t = window.setTimeout(() => {
|
||||
api
|
||||
.previewRemap(from.trim(), to.trim())
|
||||
.then(setPreview)
|
||||
.catch(() => setPreview(null))
|
||||
}, 350)
|
||||
return () => window.clearTimeout(t)
|
||||
}, [from, to])
|
||||
|
||||
const saveMapping = async () => {
|
||||
setSaving(true)
|
||||
try {
|
||||
await api.putPathMapping({ from: from.trim(), to: to.trim() })
|
||||
// Re-resolve everything that depends on the file path.
|
||||
qc.invalidateQueries({ queryKey: ['pathMapping'] })
|
||||
qc.invalidateQueries({ queryKey: ['tracks'] })
|
||||
const cleared = !from.trim() || !to.trim()
|
||||
onNotify('success', cleared ? 'Path mapping cleared.' : 'Path mapping saved.')
|
||||
onClose()
|
||||
} catch (e) {
|
||||
onError(e instanceof Error ? e.message : 'Failed to save mapping')
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
const commit = async () => {
|
||||
setCommitting(true)
|
||||
try {
|
||||
const res = await api.remapPaths(from.trim(), to.trim())
|
||||
// Locations changed, so track ids changed — refetch library + state.
|
||||
qc.invalidateQueries({ queryKey: ['tracks'] })
|
||||
qc.invalidateQueries({ queryKey: ['state'] })
|
||||
if (res.rewritten === 0) {
|
||||
onNotify('warning', 'No tracks matched — nothing was rewritten.')
|
||||
} else {
|
||||
onNotify(
|
||||
'success',
|
||||
`Rewrote ${res.rewritten} track path${res.rewritten === 1 ? '' : 's'}.` +
|
||||
(res.backup ? ` Backup: ${res.backup.split('/').pop()}` : ''),
|
||||
)
|
||||
onClose()
|
||||
}
|
||||
} catch (e) {
|
||||
onError(e instanceof Error ? e.message : 'Failed to rewrite paths')
|
||||
} finally {
|
||||
setCommitting(false)
|
||||
setConfirming(false)
|
||||
}
|
||||
}
|
||||
|
||||
const canCommit = !!from.trim() && !!to.trim()
|
||||
|
||||
return (
|
||||
<div
|
||||
className="fixed inset-0 z-40 flex items-center justify-center bg-black/60 p-6"
|
||||
onClick={onClose}
|
||||
>
|
||||
<div
|
||||
className="flex max-h-[85vh] w-full max-w-xl flex-col overflow-hidden rounded-xl border border-line bg-ink-900 shadow-2xl"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<div className="border-b border-line px-5 py-4">
|
||||
<div className="text-[15px] font-semibold tracking-tight">Path Remapping</div>
|
||||
<div className="text-xs text-muted">
|
||||
Translate the collection's stored file paths to where they live on this machine.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="min-h-0 flex-1 space-y-4 overflow-y-auto px-5 py-4">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-muted">Stored prefix (from)</label>
|
||||
<input
|
||||
value={from}
|
||||
onChange={(e) => setFrom(e.target.value)}
|
||||
placeholder="/Volumes/OLD_USB/Music"
|
||||
className={inputCls}
|
||||
/>
|
||||
<div className="text-[11px] text-faint">
|
||||
The path prefix as saved in the collection (may not exist on this machine).
|
||||
</div>
|
||||
{suggest.data && suggest.data.groups.length > 0 && (
|
||||
<div className="flex flex-wrap items-center gap-1.5 pt-0.5">
|
||||
<span className="text-[11px] text-faint">Detected:</span>
|
||||
{suggest.data.groups
|
||||
.filter((g) => g.prefix && g.prefix !== from)
|
||||
.map((g) => (
|
||||
<button
|
||||
key={g.prefix}
|
||||
onClick={() => setFrom(g.prefix)}
|
||||
title={`${g.count} tracks`}
|
||||
className="max-w-full truncate rounded border border-line px-1.5 py-0.5 text-[11px] text-accent hover:bg-ink-800"
|
||||
>
|
||||
{g.prefix} · {g.count}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-muted">Local prefix (to)</label>
|
||||
<div className="flex gap-2">
|
||||
<input
|
||||
value={to}
|
||||
onChange={(e) => setTo(e.target.value)}
|
||||
placeholder="/Users/you/Music"
|
||||
className={inputCls}
|
||||
/>
|
||||
<button
|
||||
onClick={() => setBrowsing((b) => !b)}
|
||||
className="shrink-0 rounded-md border border-line px-3 py-1.5 text-sm text-muted hover:bg-ink-800 hover:text-text"
|
||||
>
|
||||
{browsing ? 'Close' : 'Browse…'}
|
||||
</button>
|
||||
</div>
|
||||
{browsing && (
|
||||
<FolderBrowser
|
||||
initial={to.trim() || undefined}
|
||||
onPick={(path) => {
|
||||
setTo(path)
|
||||
setBrowsing(false)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Validation preview */}
|
||||
{preview && (
|
||||
<div className="rounded-md border border-line bg-ink-850 px-3 py-2 text-xs">
|
||||
{preview.matched === 0 ? (
|
||||
<span className="text-gold">
|
||||
No tracks match “{from.trim()}”. Check the stored prefix.
|
||||
</span>
|
||||
) : (
|
||||
<span className={preview.existing === preview.matched ? 'text-mint' : 'text-gold'}>
|
||||
{preview.matched} of {preview.total} tracks match; {preview.existing} of{' '}
|
||||
{preview.matched} exist at the target.
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Write-back: permanent, destructive move. Clearly separated. */}
|
||||
<div className="mt-2 space-y-2 rounded-md border border-gold/40 bg-gold/5 px-3 py-3">
|
||||
<div className="text-xs font-semibold text-gold">Make permanent</div>
|
||||
<p className="text-[11px] leading-relaxed text-muted">
|
||||
Rewrites the matching file paths directly in the collection (a backup is
|
||||
written first). Use this only when you've moved the library for good — it
|
||||
bakes in <span className="text-text">this machine's</span> paths and will
|
||||
break the collection on other machines/OSes.
|
||||
</p>
|
||||
{confirming ? (
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-[11px] text-gold">Rewrite {preview?.matched ?? 0} paths?</span>
|
||||
<button
|
||||
onClick={commit}
|
||||
disabled={committing || !canCommit}
|
||||
className="rounded-md bg-gold px-2.5 py-1 text-xs font-medium text-ink-950 hover:brightness-110 disabled:opacity-50"
|
||||
>
|
||||
{committing ? 'Rewriting…' : 'Yes, rewrite'}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setConfirming(false)}
|
||||
className="rounded-md px-2.5 py-1 text-xs text-muted hover:bg-ink-800 hover:text-text"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<button
|
||||
onClick={() => setConfirming(true)}
|
||||
disabled={!canCommit}
|
||||
className="rounded-md border border-gold/50 px-2.5 py-1 text-xs text-gold hover:bg-gold/10 disabled:opacity-40"
|
||||
>
|
||||
Rewrite collection paths…
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-end gap-2 border-t border-line px-5 py-3">
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="rounded-md px-3 py-1.5 text-sm text-muted hover:bg-ink-800 hover:text-text"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
onClick={saveMapping}
|
||||
disabled={saving}
|
||||
className="rounded-md bg-accent px-3 py-1.5 text-sm font-medium text-ink-950 hover:brightness-110 disabled:opacity-50"
|
||||
>
|
||||
{saving ? 'Saving…' : 'Save mapping'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** A compact directory-only browser for choosing the local prefix. */
|
||||
function FolderBrowser({
|
||||
initial,
|
||||
onPick,
|
||||
}: {
|
||||
initial?: string
|
||||
onPick: (path: string) => void
|
||||
}) {
|
||||
const [dir, setDir] = useState<string | undefined>(initial)
|
||||
const listing = useQuery({ queryKey: ['fs', dir ?? '~'], queryFn: () => api.listDir(dir) })
|
||||
const data = listing.data
|
||||
|
||||
return (
|
||||
<div className="mt-2 overflow-hidden rounded-md border border-line bg-ink-850">
|
||||
<div className="flex items-center gap-1 border-b border-line px-2 py-1.5">
|
||||
<button
|
||||
onClick={() => setDir(data?.home)}
|
||||
className="rounded px-2 py-0.5 text-xs text-muted hover:bg-ink-800 hover:text-text"
|
||||
>
|
||||
Home
|
||||
</button>
|
||||
<button
|
||||
onClick={() => data?.parent && setDir(data.parent)}
|
||||
disabled={!data?.parent}
|
||||
className="rounded px-2 py-0.5 text-xs text-muted hover:bg-ink-800 hover:text-text disabled:opacity-40"
|
||||
>
|
||||
Up
|
||||
</button>
|
||||
<span className="ml-1 flex-1 truncate text-right text-[11px] text-faint" dir="rtl">
|
||||
{data?.path ?? '…'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="max-h-48 overflow-y-auto">
|
||||
{data?.dirs.length ? (
|
||||
data.dirs.map((d) => (
|
||||
<button
|
||||
key={d.path}
|
||||
onClick={() => setDir(d.path)}
|
||||
className="flex w-full items-center gap-2 px-3 py-1.5 text-left text-sm text-text hover:bg-ink-800"
|
||||
>
|
||||
<span className="text-faint">📁</span>
|
||||
<span className="truncate">{d.name}</span>
|
||||
</button>
|
||||
))
|
||||
) : (
|
||||
<div className="px-3 py-2 text-xs text-faint">No subfolders.</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="border-t border-line px-2 py-1.5">
|
||||
<button
|
||||
onClick={() => data?.path && onPick(data.path)}
|
||||
disabled={!data?.path}
|
||||
className="w-full rounded-md bg-ink-800 px-3 py-1.5 text-sm text-text hover:bg-ink-700 disabled:opacity-50"
|
||||
>
|
||||
Use this folder
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
|
||||
interface Props {
|
||||
onOpenPathMapping: () => void
|
||||
}
|
||||
|
||||
/** Overflow menu for low-frequency, collection-level settings. Currently holds
|
||||
* path remapping; the natural home for future advanced settings. */
|
||||
export function SettingsMenu({ onOpenPathMapping }: Props) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
const onDoc = (e: MouseEvent) => {
|
||||
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false)
|
||||
}
|
||||
document.addEventListener('mousedown', onDoc)
|
||||
return () => document.removeEventListener('mousedown', onDoc)
|
||||
}, [open])
|
||||
|
||||
const item =
|
||||
'flex w-full flex-col items-start rounded px-2 py-1.5 text-left hover:bg-ink-800'
|
||||
|
||||
return (
|
||||
<div ref={ref} className="relative">
|
||||
<button
|
||||
onClick={() => setOpen((o) => !o)}
|
||||
className="flex items-center justify-center rounded-md border border-line bg-ink-850 px-2 py-1.5 text-muted transition-colors hover:border-ink-600 hover:text-text"
|
||||
title="Settings"
|
||||
aria-label="Settings"
|
||||
>
|
||||
<svg
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
className="h-5 w-5"
|
||||
>
|
||||
<circle cx="12" cy="12" r="3" />
|
||||
<path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z" />
|
||||
</svg>
|
||||
</button>
|
||||
{open && (
|
||||
<div className="absolute right-0 z-30 mt-1 w-56 rounded-md border border-line bg-ink-850 p-1 shadow-2xl">
|
||||
<button
|
||||
onClick={() => {
|
||||
onOpenPathMapping()
|
||||
setOpen(false)
|
||||
}}
|
||||
className={item}
|
||||
>
|
||||
<span className="text-sm text-text">Path remapping…</span>
|
||||
<span className="text-[11px] text-faint">
|
||||
Point the collection at moved or relocated files
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -2,14 +2,20 @@ import { useEffect } from 'react'
|
||||
|
||||
export interface ToastMsg {
|
||||
id: number
|
||||
kind: 'success' | 'error'
|
||||
kind: 'success' | 'error' | 'warning'
|
||||
text: string
|
||||
}
|
||||
|
||||
const KIND_CLS: Record<ToastMsg['kind'], string> = {
|
||||
error: 'border-pink/40 bg-ink-800 text-pink',
|
||||
warning: 'border-gold/40 bg-ink-800 text-gold',
|
||||
success: 'border-mint/40 bg-ink-800 text-mint',
|
||||
}
|
||||
|
||||
export function Toast({ toast, onClose }: { toast: ToastMsg | null; onClose: () => void }) {
|
||||
useEffect(() => {
|
||||
if (!toast) return
|
||||
const t = setTimeout(onClose, toast.kind === 'error' ? 6000 : 4000)
|
||||
const t = setTimeout(onClose, toast.kind === 'success' ? 4000 : 6000)
|
||||
return () => clearTimeout(t)
|
||||
}, [toast, onClose])
|
||||
|
||||
@@ -17,11 +23,7 @@ export function Toast({ toast, onClose }: { toast: ToastMsg | null; onClose: ()
|
||||
return (
|
||||
<div className="pointer-events-none fixed inset-x-0 top-4 z-50 flex justify-center">
|
||||
<div
|
||||
className={`pointer-events-auto max-w-lg rounded-lg border px-4 py-2.5 text-sm shadow-xl ${
|
||||
toast.kind === 'error'
|
||||
? 'border-pink/40 bg-ink-800 text-pink'
|
||||
: 'border-mint/40 bg-ink-800 text-mint'
|
||||
}`}
|
||||
className={`pointer-events-auto max-w-lg rounded-lg border px-4 py-2.5 text-sm shadow-xl ${KIND_CLS[toast.kind]}`}
|
||||
>
|
||||
{toast.text}
|
||||
</div>
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useQuery } from '@tanstack/react-query'
|
||||
import type { VisibilityState } from '@tanstack/react-table'
|
||||
import { api } from '../api'
|
||||
import { ColumnsMenu } from './ColumnsMenu'
|
||||
import { SettingsMenu } from './SettingsMenu'
|
||||
|
||||
export interface Filters {
|
||||
search: string
|
||||
@@ -29,6 +30,7 @@ interface Props {
|
||||
columnVisibility: VisibilityState
|
||||
onColumnVisibilityChange: (v: VisibilityState) => void
|
||||
onResetColumns: () => void
|
||||
onOpenPathMapping: () => void
|
||||
}
|
||||
|
||||
const selectCls =
|
||||
@@ -40,6 +42,7 @@ export function Toolbar({
|
||||
columnVisibility,
|
||||
onColumnVisibilityChange,
|
||||
onResetColumns,
|
||||
onOpenPathMapping,
|
||||
}: Props) {
|
||||
const { data: facets } = useQuery({ queryKey: ['facets'], queryFn: api.facets })
|
||||
const set = (patch: Partial<Filters>) => onChange({ ...filters, ...patch })
|
||||
@@ -145,6 +148,7 @@ export function Toolbar({
|
||||
onChange={onColumnVisibilityChange}
|
||||
onReset={onResetColumns}
|
||||
/>
|
||||
<SettingsMenu onOpenPathMapping={onOpenPathMapping} />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user