mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented metadata editing
This commit is contained in:
1 parent
cc254e9a9b
commit
279e5ac34f
13 files changed
+943
-35
No files matched your search
@@ -10,6 +10,8 @@ import { SelectionBar } from './components/SelectionBar'
|
||||
import { StatusBar } from './components/StatusBar'
|
||||
import { Toast, type ToastMsg } from './components/Toast'
|
||||
import { CollectionPicker } from './components/CollectionPicker'
|
||||
import { ContextMenu } from './components/ContextMenu'
|
||||
import { EditTagsDialog } from './components/EditTagsDialog'
|
||||
|
||||
function applyFilters(tracks: Track[], f: Filters): Track[] {
|
||||
const q = f.search.trim().toLowerCase()
|
||||
@@ -39,6 +41,8 @@ export default function App() {
|
||||
const [selected, setSelected] = useState<Set<string>>(new Set())
|
||||
const [toast, setToast] = useState<ToastMsg | null>(null)
|
||||
const [forcePicker, setForcePicker] = useState(false)
|
||||
const [menu, setMenu] = useState<{ track: Track; x: number; y: number } | null>(null)
|
||||
const [editing, setEditing] = useState<Track | null>(null)
|
||||
|
||||
const notify = useCallback((kind: ToastMsg['kind'], text: string) => {
|
||||
setToast({ id: Date.now(), kind, text })
|
||||
@@ -117,6 +121,22 @@ export default function App() {
|
||||
return (
|
||||
<div className="flex h-screen w-screen overflow-hidden bg-ink-950">
|
||||
<Toast toast={toast} onClose={() => setToast(null)} />
|
||||
{menu && (
|
||||
<ContextMenu
|
||||
x={menu.x}
|
||||
y={menu.y}
|
||||
items={[{ label: 'Edit Tags…', onClick: () => setEditing(menu.track) }]}
|
||||
onClose={() => setMenu(null)}
|
||||
/>
|
||||
)}
|
||||
{editing && (
|
||||
<EditTagsDialog
|
||||
track={editing}
|
||||
onClose={() => setEditing(null)}
|
||||
onApplied={(msg) => notify('success', msg)}
|
||||
onError={onError}
|
||||
/>
|
||||
)}
|
||||
<Sidebar source={source} onSelect={selectSource} onError={onError} />
|
||||
|
||||
<main className="relative flex min-w-0 flex-1 flex-col">
|
||||
@@ -152,6 +172,7 @@ export default function App() {
|
||||
onToggleAll: toggleAll,
|
||||
allSelected: selected.size > 0 && selected.size === filtered.length,
|
||||
}}
|
||||
onRowContextMenu={(track, x, y) => setMenu({ track, x, y })}
|
||||
/>
|
||||
)
|
||||
) : (
|
||||
@@ -159,6 +180,7 @@ export default function App() {
|
||||
uuid={(source as { id: string }).id}
|
||||
tracks={tracks}
|
||||
onError={onError}
|
||||
onRowContextMenu={(track, x, y) => setMenu({ track, x, y })}
|
||||
/>
|
||||
)}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ export interface Track {
|
||||
label: string | null
|
||||
remixer: string | null
|
||||
producer: string | null
|
||||
mix: string | null
|
||||
comment: string | null
|
||||
bpm: number | null
|
||||
key: string | null
|
||||
@@ -196,4 +197,15 @@ export const api = {
|
||||
{ track_ids: trackIds },
|
||||
),
|
||||
save: () => send<SaveResult>('POST', '/api/save'),
|
||||
editTrack: (trackId: string, fields: Record<string, string | number | null>) =>
|
||||
send<{ status: string }>('PATCH', '/api/tracks', { track_id: trackId, fields }),
|
||||
artUrl: (trackId: string) => `/api/tracks/art?track_id=${encodeURIComponent(trackId)}`,
|
||||
uploadArt: async (trackId: string, file: File) => {
|
||||
const fd = new FormData()
|
||||
fd.append('track_id', trackId)
|
||||
fd.append('file', file)
|
||||
const res = await fetch('/api/tracks/art', { method: 'PUT', body: fd })
|
||||
if (!res.ok) throw new Error(`Cover upload failed (${res.status})`)
|
||||
return res.json()
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { useEffect } from 'react'
|
||||
|
||||
export interface MenuItem {
|
||||
label: string
|
||||
onClick: () => void
|
||||
danger?: boolean
|
||||
}
|
||||
|
||||
interface Props {
|
||||
x: number
|
||||
y: number
|
||||
items: MenuItem[]
|
||||
onClose: () => void
|
||||
}
|
||||
|
||||
// Lightweight fixed-position menu. Closes on outside click, scroll, or Escape.
|
||||
export function ContextMenu({ x, y, items, onClose }: Props) {
|
||||
useEffect(() => {
|
||||
const close = () => onClose()
|
||||
const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose()
|
||||
// Defer attaching outside-dismiss listeners by a tick so the very
|
||||
// right-click/click that opened the menu can't immediately close it.
|
||||
const id = setTimeout(() => {
|
||||
window.addEventListener('click', close)
|
||||
window.addEventListener('contextmenu', close)
|
||||
window.addEventListener('scroll', close, true)
|
||||
window.addEventListener('keydown', onKey)
|
||||
}, 0)
|
||||
return () => {
|
||||
clearTimeout(id)
|
||||
window.removeEventListener('click', close)
|
||||
window.removeEventListener('contextmenu', close)
|
||||
window.removeEventListener('scroll', close, true)
|
||||
window.removeEventListener('keydown', onKey)
|
||||
}
|
||||
}, [onClose])
|
||||
|
||||
return (
|
||||
<div
|
||||
className="fixed z-50 min-w-[160px] overflow-hidden rounded-lg border border-line bg-ink-850 py-1 shadow-2xl"
|
||||
style={{ top: Math.min(y, window.innerHeight - 100), left: Math.min(x, window.innerWidth - 180) }}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
{items.map((item, i) => (
|
||||
<button
|
||||
key={i}
|
||||
onClick={() => {
|
||||
item.onClick()
|
||||
onClose()
|
||||
}}
|
||||
className={`block w-full px-3 py-1.5 text-left text-sm ${
|
||||
item.danger
|
||||
? 'text-pink hover:bg-ink-800'
|
||||
: 'text-muted hover:bg-ink-800 hover:text-text'
|
||||
}`}
|
||||
>
|
||||
{item.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
import { useRef, useState } from 'react'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { api, type Track } from '../api'
|
||||
import { formatBpm, formatDuration } from '../lib/format'
|
||||
|
||||
interface Props {
|
||||
track: Track
|
||||
onClose: () => void
|
||||
onApplied: (msg: string) => void
|
||||
onError: (msg: string) => void
|
||||
}
|
||||
|
||||
// Editable free-text fields (mirrors the backend's safe set). Rating is separate.
|
||||
const TEXT_FIELDS: { key: keyof Track; label: string; textarea?: boolean }[] = [
|
||||
{ key: 'title', label: 'Title' },
|
||||
{ key: 'artist', label: 'Artist' },
|
||||
{ key: 'album', label: 'Album' },
|
||||
{ key: 'genre', label: 'Genre' },
|
||||
{ key: 'label', label: 'Label' },
|
||||
{ key: 'remixer', label: 'Remixer' },
|
||||
{ key: 'producer', label: 'Producer' },
|
||||
{ key: 'mix', label: 'Mix' },
|
||||
{ key: 'release_date', label: 'Release date' },
|
||||
{ key: 'comment', label: 'Comment', textarea: true },
|
||||
]
|
||||
|
||||
export function EditTagsDialog({ track, onClose, onApplied, onError }: Props) {
|
||||
const qc = useQueryClient()
|
||||
const { data: facets } = useQuery({ queryKey: ['facets'], queryFn: api.facets })
|
||||
|
||||
const [form, setForm] = useState<Record<string, string>>(() => {
|
||||
const f: Record<string, string> = {}
|
||||
for (const { key } of TEXT_FIELDS) f[key] = (track[key] as string | null) ?? ''
|
||||
return f
|
||||
})
|
||||
const [rating, setRating] = useState<number>(track.rating)
|
||||
const [artFile, setArtFile] = useState<File | null>(null)
|
||||
const [artPreview, setArtPreview] = useState<string | null>(null)
|
||||
const [artMissing, setArtMissing] = useState(false)
|
||||
const fileInput = useRef<HTMLInputElement>(null)
|
||||
|
||||
const changedFields = () => {
|
||||
const changed: Record<string, string | number | null> = {}
|
||||
for (const { key } of TEXT_FIELDS) {
|
||||
const orig = (track[key] as string | null) ?? ''
|
||||
if (form[key] !== orig) changed[key] = form[key]
|
||||
}
|
||||
if (rating !== track.rating) changed.rating = rating
|
||||
return changed
|
||||
}
|
||||
|
||||
const apply = useMutation({
|
||||
mutationFn: async () => {
|
||||
if (artFile) await api.uploadArt(track.id, artFile)
|
||||
const changed = changedFields()
|
||||
if (Object.keys(changed).length > 0) await api.editTrack(track.id, changed)
|
||||
},
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ['tracks'] })
|
||||
qc.invalidateQueries({ queryKey: ['playlist'] })
|
||||
qc.invalidateQueries({ queryKey: ['state'] })
|
||||
qc.invalidateQueries({ queryKey: ['facets'] })
|
||||
qc.invalidateQueries({ queryKey: ['stats'] })
|
||||
onApplied(`Updated “${form.title || track.title || 'track'}” — Save to write to Traktor`)
|
||||
onClose()
|
||||
},
|
||||
onError: (e: Error) => onError(e.message),
|
||||
})
|
||||
|
||||
const pickArt = (f: File | null) => {
|
||||
if (!f) return
|
||||
setArtFile(f)
|
||||
setArtPreview(URL.createObjectURL(f))
|
||||
setArtMissing(false)
|
||||
}
|
||||
|
||||
const submit = () => {
|
||||
if (!artFile && Object.keys(changedFields()).length === 0) {
|
||||
onClose()
|
||||
return
|
||||
}
|
||||
apply.mutate()
|
||||
}
|
||||
|
||||
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-lg 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">Edit Tags</div>
|
||||
<div className="truncate text-xs text-muted">
|
||||
{track.artist} — {track.title}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="min-h-0 flex-1 space-y-3 overflow-y-auto px-5 py-4">
|
||||
{/* Album art */}
|
||||
<div className="flex items-center gap-4">
|
||||
<div className="flex h-24 w-24 shrink-0 items-center justify-center overflow-hidden rounded-md border border-line bg-ink-850">
|
||||
{artPreview ? (
|
||||
<img src={artPreview} alt="cover" className="h-full w-full object-cover" />
|
||||
) : artMissing ? (
|
||||
<span className="text-[10px] uppercase tracking-wider text-faint">No art</span>
|
||||
) : (
|
||||
<img
|
||||
src={api.artUrl(track.id)}
|
||||
alt="cover"
|
||||
className="h-full w-full object-cover"
|
||||
onError={() => setArtMissing(true)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
<div className="min-w-0">
|
||||
<span className="mb-1 block text-[11px] font-semibold uppercase tracking-wider text-faint">
|
||||
Album art
|
||||
</span>
|
||||
<input
|
||||
ref={fileInput}
|
||||
type="file"
|
||||
accept="image/jpeg,image/png"
|
||||
className="hidden"
|
||||
onChange={(e) => pickArt(e.target.files?.[0] ?? null)}
|
||||
/>
|
||||
<button
|
||||
onClick={() => fileInput.current?.click()}
|
||||
className="rounded-md border border-line bg-ink-850 px-3 py-1.5 text-sm text-text hover:border-ink-600"
|
||||
>
|
||||
{artFile ? 'Change image…' : 'Replace…'}
|
||||
</button>
|
||||
{artFile && (
|
||||
<div className="mt-1 truncate text-[11px] text-mint">{artFile.name} (applies on Save)</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{TEXT_FIELDS.map(({ key, label, textarea }) => (
|
||||
<label key={key} className="block">
|
||||
<span className="mb-1 block text-[11px] font-semibold uppercase tracking-wider text-faint">
|
||||
{label}
|
||||
</span>
|
||||
{textarea ? (
|
||||
<textarea
|
||||
value={form[key]}
|
||||
onChange={(e) => setForm({ ...form, [key]: e.target.value })}
|
||||
rows={2}
|
||||
className="w-full resize-y rounded-md border border-line bg-ink-850 px-2.5 py-1.5 text-sm text-text outline-none focus:border-accent"
|
||||
/>
|
||||
) : (
|
||||
<input
|
||||
value={form[key]}
|
||||
onChange={(e) => setForm({ ...form, [key]: e.target.value })}
|
||||
list={key === 'genre' ? 'genre-suggestions' : undefined}
|
||||
className="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"
|
||||
/>
|
||||
)}
|
||||
</label>
|
||||
))}
|
||||
<datalist id="genre-suggestions">
|
||||
{facets?.genres.map((g) => <option key={g.name} value={g.name} />)}
|
||||
</datalist>
|
||||
|
||||
{/* Rating */}
|
||||
<div>
|
||||
<span className="mb-1 block text-[11px] font-semibold uppercase tracking-wider text-faint">
|
||||
Rating
|
||||
</span>
|
||||
<div className="flex items-center gap-1 text-xl">
|
||||
{[1, 2, 3, 4, 5].map((i) => (
|
||||
<button
|
||||
key={i}
|
||||
onClick={() => setRating(i === rating ? 0 : i)}
|
||||
className={i <= rating ? 'text-gold' : 'text-ink-600 hover:text-faint'}
|
||||
>
|
||||
★
|
||||
</button>
|
||||
))}
|
||||
{rating > 0 && (
|
||||
<button
|
||||
onClick={() => setRating(0)}
|
||||
className="ml-2 text-xs text-faint hover:text-text"
|
||||
>
|
||||
clear
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Read-only technical fields */}
|
||||
<div className="mt-2 grid grid-cols-2 gap-x-4 gap-y-1 rounded-md bg-ink-850 px-3 py-2 text-xs text-muted">
|
||||
<div>BPM: <span className="tabular-nums text-text">{formatBpm(track.bpm)}</span></div>
|
||||
<div>Key: <span className="text-text">{track.key ?? '—'}</span></div>
|
||||
<div>Length: <span className="tabular-nums text-text">{formatDuration(track.length)}</span></div>
|
||||
<div>Bitrate: <span className="tabular-nums text-text">{track.bitrate ? Math.round(track.bitrate / 1000) + 'k' : '—'}</span></div>
|
||||
<div>Plays: <span className="tabular-nums text-text">{track.playcount ?? 0}</span></div>
|
||||
<div>Cues: <span className="tabular-nums text-text">{track.cue_count}</span></div>
|
||||
<div className="col-span-2 truncate" title={track.filepath ?? ''}>
|
||||
Path: <span className="text-text">{track.filepath ?? '—'}</span>
|
||||
</div>
|
||||
</div>
|
||||
<p className="text-[11px] leading-snug text-faint">
|
||||
BPM, key and path aren’t editable here. Changes apply in-app; click
|
||||
<span className="text-muted"> Save to Traktor</span> to write them to disk.
|
||||
New album art is written into the file; Traktor may need a manual
|
||||
“Import Cover Art” to refresh its own cached thumbnail.
|
||||
</p>
|
||||
</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={submit}
|
||||
disabled={apply.isPending}
|
||||
className="rounded-md bg-accent px-4 py-1.5 text-sm font-medium text-ink-950 hover:brightness-110 disabled:opacity-50"
|
||||
>
|
||||
{apply.isPending ? 'Applying…' : 'Apply'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -24,16 +24,19 @@ interface Props {
|
||||
uuid: string
|
||||
tracks: Track[]
|
||||
onError: (msg: string) => void
|
||||
onRowContextMenu?: (track: Track, x: number, y: number) => void
|
||||
}
|
||||
|
||||
function Row({
|
||||
track,
|
||||
index,
|
||||
onRemove,
|
||||
onContextMenu,
|
||||
}: {
|
||||
track: Track
|
||||
index: number
|
||||
onRemove: (id: string) => void
|
||||
onContextMenu?: (track: Track, x: number, y: number) => void
|
||||
}) {
|
||||
const { attributes, listeners, setNodeRef, transform, transition, isDragging } =
|
||||
useSortable({ id: track.id })
|
||||
@@ -45,6 +48,11 @@ function Row({
|
||||
className={`flex items-center border-b border-ink-850 text-sm ${
|
||||
isDragging ? 'z-10 bg-ink-800 shadow-lg' : 'hover:bg-ink-850'
|
||||
}`}
|
||||
onContextMenu={(e) => {
|
||||
if (!onContextMenu) return
|
||||
e.preventDefault()
|
||||
onContextMenu(track, e.clientX, e.clientY)
|
||||
}}
|
||||
>
|
||||
<button
|
||||
{...attributes}
|
||||
@@ -90,7 +98,7 @@ function Row({
|
||||
)
|
||||
}
|
||||
|
||||
export function PlaylistEditor({ uuid, tracks, onError }: Props) {
|
||||
export function PlaylistEditor({ uuid, tracks, onError, onRowContextMenu }: Props) {
|
||||
const qc = useQueryClient()
|
||||
const [items, setItems] = useState<Track[]>(tracks)
|
||||
|
||||
@@ -157,7 +165,13 @@ export function PlaylistEditor({ uuid, tracks, onError }: Props) {
|
||||
>
|
||||
<SortableContext items={items.map((t) => t.id)} strategy={verticalListSortingStrategy}>
|
||||
{items.map((t, i) => (
|
||||
<Row key={t.id} track={t} index={i} onRemove={remove} />
|
||||
<Row
|
||||
key={t.id}
|
||||
track={t}
|
||||
index={i}
|
||||
onRemove={remove}
|
||||
onContextMenu={onRowContextMenu}
|
||||
/>
|
||||
))}
|
||||
</SortableContext>
|
||||
</DndContext>
|
||||
|
||||
@@ -108,9 +108,10 @@ interface Props {
|
||||
sorting: SortingState
|
||||
onSortingChange: (s: SortingState) => void
|
||||
selection?: Selection
|
||||
onRowContextMenu?: (track: Track, x: number, y: number) => void
|
||||
}
|
||||
|
||||
export function TrackTable({ tracks, sorting, onSortingChange, selection }: Props) {
|
||||
export function TrackTable({ tracks, sorting, onSortingChange, selection, onRowContextMenu }: Props) {
|
||||
const table = useReactTable({
|
||||
data: tracks,
|
||||
columns,
|
||||
@@ -191,6 +192,11 @@ export function TrackTable({ tracks, sorting, onSortingChange, selection }: Prop
|
||||
height: vr.size,
|
||||
width: '100%',
|
||||
}}
|
||||
onContextMenu={(e) => {
|
||||
if (!onRowContextMenu) return
|
||||
e.preventDefault()
|
||||
onRowContextMenu(row.original, e.clientX, e.clientY)
|
||||
}}
|
||||
>
|
||||
{selection ? (
|
||||
<span className="flex w-10 shrink-0 items-center justify-center">
|
||||
|
||||
Reference in new issue
Block a user