From c98b621f05e9704dff9697f1cba50bbf59e80b9e Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 4 Jul 2026 23:37:27 +0200 Subject: [PATCH] Implemented quick tag editing in library list --- frontend/src/App.tsx | 19 +++ frontend/src/components/RatingStars.tsx | 43 +++++-- frontend/src/components/TrackTable.tsx | 3 + frontend/src/lib/trackColumns.tsx | 161 +++++++++++++++++++----- 4 files changed, 191 insertions(+), 35 deletions(-) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 67f6997..513d595 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -115,6 +115,24 @@ export default function App() { }, []) const onError = useCallback((msg: string) => notify('error', msg), [notify]) + // Inline single-field edit from a double-clicked table cell. + const editField = useCallback( + (track: Track, field: keyof Track, value: string | number) => { + api + .editTrack(track.id, { [field]: value }) + .then(() => { + qc.invalidateQueries({ queryKey: ['tracks'] }) + qc.invalidateQueries({ queryKey: ['playlist'] }) + qc.invalidateQueries({ queryKey: ['state'] }) + qc.invalidateQueries({ queryKey: ['facets'] }) + qc.invalidateQueries({ queryKey: ['stats'] }) + notify('success', `Updated ${String(field)} — Save to write to Traktor`) + }) + .catch((e) => onError((e as Error).message)) + }, + [qc, notify, onError], + ) + const collection = useQuery({ queryKey: ['collection'], queryFn: api.collection }) const loaded = collection.data?.loaded ?? false @@ -254,6 +272,7 @@ export default function App() { }} onRowContextMenu={(track, x, y) => setMenu({ track, x, y })} onPlay={playTrack} + onEditField={editField} activeTrackId={prepTrack?.id ?? null} columnVisibility={columnVisibility} columnOrder={columnOrder} diff --git a/frontend/src/components/RatingStars.tsx b/frontend/src/components/RatingStars.tsx index 1ccc406..4f17cfd 100644 --- a/frontend/src/components/RatingStars.tsx +++ b/frontend/src/components/RatingStars.tsx @@ -1,19 +1,48 @@ +import { useState } from 'react' + interface Props { value: number + /** When provided, the stars become clickable to set the rating (0–5). */ + onChange?: (value: number) => void } -// Compact 5-star rating. Filled stars use the gold accent; empties are faint. -export function RatingStars({ value }: Props) { - if (!value) return — +// Compact 5-star rating. Read-only by default (shows a dash when unrated). When +// `onChange` is given it's interactive: always shows five stars — hollow (☆) up +// to the rating, solid gold (★) below it — with hover preview. Clicking a star +// sets that rating; clicking the current top star again clears it to 0. +export function RatingStars({ value, onChange }: Props) { + const [hover, setHover] = useState(0) + + if (!onChange) { + if (!value) return — + return ( + + {[1, 2, 3, 4, 5].map((i) => ( + + {i <= value ? '★' : '☆'} + + ))} + + ) + } + + const shown = hover || value return ( {[1, 2, 3, 4, 5].map((i) => ( - { + e.stopPropagation() + onChange(i === value ? 0 : i) + }} + onMouseEnter={() => setHover(i)} + onMouseLeave={() => setHover(0)} + className={`transition-colors ${i <= shown ? 'text-gold' : 'text-ink-600 hover:text-gold/60'}`} + title={i === value ? 'Clear rating' : `Set ${i} star${i > 1 ? 's' : ''}`} > - ★ - + {i <= shown ? '★' : '☆'} + ))} ) diff --git a/frontend/src/components/TrackTable.tsx b/frontend/src/components/TrackTable.tsx index cddf8f5..9834ba3 100644 --- a/frontend/src/components/TrackTable.tsx +++ b/frontend/src/components/TrackTable.tsx @@ -48,6 +48,7 @@ interface Props { selection?: Selection onRowContextMenu?: (track: Track, x: number, y: number) => void onPlay?: (track: Track) => void + onEditField?: (track: Track, field: keyof Track, value: string | number) => void activeTrackId?: string | null } @@ -106,12 +107,14 @@ export function TrackTable({ selection, onRowContextMenu, onPlay, + onEditField, activeTrackId, }: Props) { const table = useReactTable({ data: tracks, columns: TRACK_COLUMNS, state: { sorting, columnVisibility, columnOrder, columnSizing }, + meta: { onEditField }, onSortingChange: (updater) => onSortingChange(typeof updater === 'function' ? updater(sorting) : updater), onColumnSizingChange: (updater) => diff --git a/frontend/src/lib/trackColumns.tsx b/frontend/src/lib/trackColumns.tsx index 1b2552d..c90e93f 100644 --- a/frontend/src/lib/trackColumns.tsx +++ b/frontend/src/lib/trackColumns.tsx @@ -2,24 +2,98 @@ // their default widths/visibility/order, and the (id,label) list the "Columns" // menu renders. Both TrackTable and the Toolbar menu read from here so they stay // in sync. Column layout (visibility, order, sizing) is persisted to userprefs. -import { createColumnHelper, type ColumnDef, type VisibilityState } from '@tanstack/react-table' +// +// Safe metadata fields are inline-editable: double-click a cell value to edit it +// (the right-click "Edit Tags" dialog remains for multi-field edits). Editing is +// wired through the table's `meta.onEditField` (see the module augmentation). +import { useState, type ReactNode } from 'react' +import { + createColumnHelper, + type CellContext, + type ColumnDef, + type RowData, + type VisibilityState, +} from '@tanstack/react-table' import type { Track } from '../api' import { formatBpm, formatDuration, keyColor } from './format' import { RatingStars } from '../components/RatingStars' -const col = createColumnHelper() - -function dash(v: string | null | undefined) { - const s = v?.toString().trim() - return s ? {s} : — +declare module '@tanstack/react-table' { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + interface TableMeta { + onEditField?: (track: Track, field: keyof Track, value: string | number) => void + } } -// Traktor dates arrive as "YYYY/M/D" strings; show them compactly (or raw). -function formatDate(v: string | null): string { - if (!v) return '—' - const m = v.match(/(\d{4})\/(\d{1,2})\/(\d{1,2})/) - if (!m) return v - return `${m[1]}-${m[2].padStart(2, '0')}-${m[3].padStart(2, '0')}` +const col = createColumnHelper() + +// Double-click-to-edit text cell. Shows `display` (or the raw value) until the +// user double-clicks, then an input; commits on Enter/blur, cancels on Escape. +function InlineEdit({ + value, + display, + onCommit, + className = 'w-full min-w-0 truncate text-muted', +}: { + value: string | null + display?: ReactNode + onCommit: (v: string) => void + className?: string +}) { + const [editing, setEditing] = useState(false) + const [val, setVal] = useState('') + + if (editing) { + const commit = () => { + setEditing(false) + if (val !== (value ?? '')) onCommit(val) + } + return ( + setVal(e.target.value)} + onBlur={commit} + onKeyDown={(e) => { + if (e.key === 'Enter') { + e.preventDefault() + commit() + } else if (e.key === 'Escape') { + e.preventDefault() + setEditing(false) + } + e.stopPropagation() + }} + onClick={(e) => e.stopPropagation()} + onDoubleClick={(e) => e.stopPropagation()} + className="w-full min-w-0 rounded bg-ink-800 px-1 py-0.5 text-sm text-text outline-none ring-1 ring-accent" + /> + ) + } + return ( +
{ + e.stopPropagation() + setVal(value ?? '') + setEditing(true) + }} + className={className} + title="Double-click to edit" + > + {display ?? (value?.trim() ? value : —)} +
+ ) +} + +// Cell renderer factory for a plain editable text field. +function editable(field: keyof Track, className?: string) { + return (c: CellContext) => ( + c.table.options.meta?.onEditField?.(c.row.original, field, v)} + /> + ) } export const TRACK_COLUMNS: ColumnDef[] = [ @@ -27,22 +101,34 @@ export const TRACK_COLUMNS: ColumnDef[] = [ id: 'title', header: 'Title', size: 300, - cell: (c) => ( -
-
- {c.getValue() || Untitled} + cell: (c) => { + const r = c.row.original + const edit = c.table.options.meta?.onEditField + const title = c.getValue() as string | null + return ( +
+ Untitled} + className="w-full min-w-0 truncate font-medium text-text" + onCommit={(v) => edit?.(r, 'title', v)} + /> + edit?.(r, 'artist', v)} + />
-
{c.row.original.artist}
-
- ), + ) + }, }), - col.accessor('album', { id: 'album', header: 'Album', size: 180, cell: (c) => dash(c.getValue()) }), - col.accessor('genre', { id: 'genre', header: 'Genre', size: 140, cell: (c) => dash(c.getValue()) }), - col.accessor('label', { id: 'label', header: 'Label', size: 140, cell: (c) => dash(c.getValue()) }), - col.accessor('remixer', { id: 'remixer', header: 'Remixer', size: 140, cell: (c) => dash(c.getValue()) }), - col.accessor('producer', { id: 'producer', header: 'Producer', size: 140, cell: (c) => dash(c.getValue()) }), - col.accessor('mix', { id: 'mix', header: 'Mix', size: 120, cell: (c) => dash(c.getValue()) }), - col.accessor('comment', { id: 'comment', header: 'Comment', size: 200, cell: (c) => dash(c.getValue()) }), + col.accessor('album', { id: 'album', header: 'Album', size: 180, cell: editable('album') }), + col.accessor('genre', { id: 'genre', header: 'Genre', size: 140, cell: editable('genre') }), + col.accessor('label', { id: 'label', header: 'Label', size: 140, cell: editable('label') }), + col.accessor('remixer', { id: 'remixer', header: 'Remixer', size: 140, cell: editable('remixer') }), + col.accessor('producer', { id: 'producer', header: 'Producer', size: 140, cell: editable('producer') }), + col.accessor('mix', { id: 'mix', header: 'Mix', size: 120, cell: editable('mix') }), + col.accessor('comment', { id: 'comment', header: 'Comment', size: 200, cell: editable('comment') }), col.accessor('bpm', { id: 'bpm', header: 'BPM', @@ -70,7 +156,12 @@ export const TRACK_COLUMNS: ColumnDef[] = [ id: 'rating', header: 'Rating', size: 92, - cell: (c) => , + cell: (c) => ( + c.table.options.meta?.onEditField?.(c.row.original, 'rating', v)} + /> + ), }), col.accessor('length', { id: 'length', @@ -128,10 +219,24 @@ export const TRACK_COLUMNS: ColumnDef[] = [ id: 'release_date', header: 'Released', size: 104, - cell: (c) => {formatDate(c.getValue())}, + cell: (c) => ( + {formatDate(c.getValue() as string | null)}} + onCommit={(v) => c.table.options.meta?.onEditField?.(c.row.original, 'release_date', v)} + /> + ), }), ] +// Traktor dates arrive as "YYYY/M/D" strings; show them compactly (or raw). +function formatDate(v: string | null): string { + if (!v) return '—' + const m = v.match(/(\d{4})\/(\d{1,2})\/(\d{1,2})/) + if (!m) return v + return `${m[1]}-${m[2].padStart(2, '0')}-${m[3].padStart(2, '0')}` +} + /** (id, label) pairs for the Columns menu, in the canonical definition order. */ export const COLUMN_MENU: { id: string; label: string }[] = TRACK_COLUMNS.map((c) => ({ id: c.id as string,