Implemented quick tag editing in library list

This commit is contained in:
Ben committed 2026-07-04 23:37:27 +02:00
1 parent aad1ec5bc0
commit c98b621f05
4 files changed
+190 -34

No files matched your search

+19
View File
@@ -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}
+36 -7
View File
@@ -1,20 +1,49 @@
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) {
// 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 <span className="text-faint">—</span>
return (
<span className="whitespace-nowrap tracking-tight" title={`${value} / 5`}>
{[1, 2, 3, 4, 5].map((i) => (
<span
key={i}
className={i <= value ? 'text-gold' : 'text-ink-600'}
>
★
<span key={i} className={i <= value ? 'text-gold' : 'text-ink-600'}>
{i <= value ? '★' : '☆'}
</span>
))}
</span>
)
}
const shown = hover || value
return (
<span className="whitespace-nowrap tracking-tight" title={`${value} / 5`}>
{[1, 2, 3, 4, 5].map((i) => (
<button
key={i}
onClick={(e) => {
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 ? '★' : '☆'}
</button>
))}
</span>
)
}
+3
View File
@@ -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) =>
+132 -27
View File
@@ -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<Track>()
function dash(v: string | null | undefined) {
const s = v?.toString().trim()
return s ? <span className="truncate text-muted">{s}</span> : <span className="text-faint">—</span>
declare module '@tanstack/react-table' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface TableMeta<TData extends RowData> {
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<Track>()
// 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 (
<input
autoFocus
value={val}
onChange={(e) => 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 (
<div
onDoubleClick={(e) => {
e.stopPropagation()
setVal(value ?? '')
setEditing(true)
}}
className={className}
title="Double-click to edit"
>
{display ?? (value?.trim() ? value : <span className="text-faint">—</span>)}
</div>
)
}
// Cell renderer factory for a plain editable text field.
function editable(field: keyof Track, className?: string) {
return (c: CellContext<Track, unknown>) => (
<InlineEdit
value={c.getValue() as string | null}
className={className}
onCommit={(v) => c.table.options.meta?.onEditField?.(c.row.original, field, v)}
/>
)
}
export const TRACK_COLUMNS: ColumnDef<Track, any>[] = [
@@ -27,22 +101,34 @@ export const TRACK_COLUMNS: ColumnDef<Track, any>[] = [
id: 'title',
header: 'Title',
size: 300,
cell: (c) => (
cell: (c) => {
const r = c.row.original
const edit = c.table.options.meta?.onEditField
const title = c.getValue() as string | null
return (
<div className="min-w-0">
<div className="truncate font-medium text-text">
{c.getValue() || <span className="text-faint">Untitled</span>}
<InlineEdit
value={title}
display={title || <span className="text-faint">Untitled</span>}
className="w-full min-w-0 truncate font-medium text-text"
onCommit={(v) => edit?.(r, 'title', v)}
/>
<InlineEdit
value={r.artist}
className="w-full min-w-0 truncate text-xs text-muted"
onCommit={(v) => edit?.(r, 'artist', v)}
/>
</div>
<div className="truncate text-xs text-muted">{c.row.original.artist}</div>
</div>
),
)
},
}),
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<Track, any>[] = [
id: 'rating',
header: 'Rating',
size: 92,
cell: (c) => <RatingStars value={c.getValue()} />,
cell: (c) => (
<RatingStars
value={c.getValue() as number}
onChange={(v) => c.table.options.meta?.onEditField?.(c.row.original, 'rating', v)}
/>
),
}),
col.accessor('length', {
id: 'length',
@@ -128,10 +219,24 @@ export const TRACK_COLUMNS: ColumnDef<Track, any>[] = [
id: 'release_date',
header: 'Released',
size: 104,
cell: (c) => <span className="tabular-nums text-muted">{formatDate(c.getValue())}</span>,
cell: (c) => (
<InlineEdit
value={c.getValue() as string | null}
display={<span className="tabular-nums text-muted">{formatDate(c.getValue() as string | null)}</span>}
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,