mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented quick tag editing in library list
This commit is contained in:
1 parent
aad1ec5bc0
commit
c98b621f05
4 files changed
+190
-34
No files matched your search
@@ -115,6 +115,24 @@ export default function App() {
|
|||||||
}, [])
|
}, [])
|
||||||
const onError = useCallback((msg: string) => notify('error', msg), [notify])
|
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 collection = useQuery({ queryKey: ['collection'], queryFn: api.collection })
|
||||||
const loaded = collection.data?.loaded ?? false
|
const loaded = collection.data?.loaded ?? false
|
||||||
|
|
||||||
@@ -254,6 +272,7 @@ export default function App() {
|
|||||||
}}
|
}}
|
||||||
onRowContextMenu={(track, x, y) => setMenu({ track, x, y })}
|
onRowContextMenu={(track, x, y) => setMenu({ track, x, y })}
|
||||||
onPlay={playTrack}
|
onPlay={playTrack}
|
||||||
|
onEditField={editField}
|
||||||
activeTrackId={prepTrack?.id ?? null}
|
activeTrackId={prepTrack?.id ?? null}
|
||||||
columnVisibility={columnVisibility}
|
columnVisibility={columnVisibility}
|
||||||
columnOrder={columnOrder}
|
columnOrder={columnOrder}
|
||||||
|
|||||||
@@ -1,20 +1,49 @@
|
|||||||
|
import { useState } from 'react'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
value: number
|
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.
|
// Compact 5-star rating. Read-only by default (shows a dash when unrated). When
|
||||||
export function RatingStars({ value }: Props) {
|
// `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>
|
if (!value) return <span className="text-faint">—</span>
|
||||||
return (
|
return (
|
||||||
<span className="whitespace-nowrap tracking-tight" title={`${value} / 5`}>
|
<span className="whitespace-nowrap tracking-tight" title={`${value} / 5`}>
|
||||||
{[1, 2, 3, 4, 5].map((i) => (
|
{[1, 2, 3, 4, 5].map((i) => (
|
||||||
<span
|
<span key={i} className={i <= value ? 'text-gold' : 'text-ink-600'}>
|
||||||
key={i}
|
{i <= value ? '★' : '☆'}
|
||||||
className={i <= value ? 'text-gold' : 'text-ink-600'}
|
|
||||||
>
|
|
||||||
★
|
|
||||||
</span>
|
</span>
|
||||||
))}
|
))}
|
||||||
</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>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
@@ -48,6 +48,7 @@ interface Props {
|
|||||||
selection?: Selection
|
selection?: Selection
|
||||||
onRowContextMenu?: (track: Track, x: number, y: number) => void
|
onRowContextMenu?: (track: Track, x: number, y: number) => void
|
||||||
onPlay?: (track: Track) => void
|
onPlay?: (track: Track) => void
|
||||||
|
onEditField?: (track: Track, field: keyof Track, value: string | number) => void
|
||||||
activeTrackId?: string | null
|
activeTrackId?: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,12 +107,14 @@ export function TrackTable({
|
|||||||
selection,
|
selection,
|
||||||
onRowContextMenu,
|
onRowContextMenu,
|
||||||
onPlay,
|
onPlay,
|
||||||
|
onEditField,
|
||||||
activeTrackId,
|
activeTrackId,
|
||||||
}: Props) {
|
}: Props) {
|
||||||
const table = useReactTable({
|
const table = useReactTable({
|
||||||
data: tracks,
|
data: tracks,
|
||||||
columns: TRACK_COLUMNS,
|
columns: TRACK_COLUMNS,
|
||||||
state: { sorting, columnVisibility, columnOrder, columnSizing },
|
state: { sorting, columnVisibility, columnOrder, columnSizing },
|
||||||
|
meta: { onEditField },
|
||||||
onSortingChange: (updater) =>
|
onSortingChange: (updater) =>
|
||||||
onSortingChange(typeof updater === 'function' ? updater(sorting) : updater),
|
onSortingChange(typeof updater === 'function' ? updater(sorting) : updater),
|
||||||
onColumnSizingChange: (updater) =>
|
onColumnSizingChange: (updater) =>
|
||||||
|
|||||||
@@ -2,24 +2,98 @@
|
|||||||
// their default widths/visibility/order, and the (id,label) list the "Columns"
|
// 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
|
// 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.
|
// 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 type { Track } from '../api'
|
||||||
import { formatBpm, formatDuration, keyColor } from './format'
|
import { formatBpm, formatDuration, keyColor } from './format'
|
||||||
import { RatingStars } from '../components/RatingStars'
|
import { RatingStars } from '../components/RatingStars'
|
||||||
|
|
||||||
const col = createColumnHelper<Track>()
|
declare module '@tanstack/react-table' {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
function dash(v: string | null | undefined) {
|
interface TableMeta<TData extends RowData> {
|
||||||
const s = v?.toString().trim()
|
onEditField?: (track: Track, field: keyof Track, value: string | number) => void
|
||||||
return s ? <span className="truncate text-muted">{s}</span> : <span className="text-faint">—</span>
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Traktor dates arrive as "YYYY/M/D" strings; show them compactly (or raw).
|
const col = createColumnHelper<Track>()
|
||||||
function formatDate(v: string | null): string {
|
|
||||||
if (!v) return '—'
|
// Double-click-to-edit text cell. Shows `display` (or the raw value) until the
|
||||||
const m = v.match(/(\d{4})\/(\d{1,2})\/(\d{1,2})/)
|
// user double-clicks, then an input; commits on Enter/blur, cancels on Escape.
|
||||||
if (!m) return v
|
function InlineEdit({
|
||||||
return `${m[1]}-${m[2].padStart(2, '0')}-${m[3].padStart(2, '0')}`
|
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>[] = [
|
export const TRACK_COLUMNS: ColumnDef<Track, any>[] = [
|
||||||
@@ -27,22 +101,34 @@ export const TRACK_COLUMNS: ColumnDef<Track, any>[] = [
|
|||||||
id: 'title',
|
id: 'title',
|
||||||
header: 'Title',
|
header: 'Title',
|
||||||
size: 300,
|
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="min-w-0">
|
||||||
<div className="truncate font-medium text-text">
|
<InlineEdit
|
||||||
{c.getValue() || <span className="text-faint">Untitled</span>}
|
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>
|
||||||
<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('album', { id: 'album', header: 'Album', size: 180, cell: editable('album') }),
|
||||||
col.accessor('genre', { id: 'genre', header: 'Genre', size: 140, cell: (c) => dash(c.getValue()) }),
|
col.accessor('genre', { id: 'genre', header: 'Genre', size: 140, cell: editable('genre') }),
|
||||||
col.accessor('label', { id: 'label', header: 'Label', size: 140, cell: (c) => dash(c.getValue()) }),
|
col.accessor('label', { id: 'label', header: 'Label', size: 140, cell: editable('label') }),
|
||||||
col.accessor('remixer', { id: 'remixer', header: 'Remixer', size: 140, cell: (c) => dash(c.getValue()) }),
|
col.accessor('remixer', { id: 'remixer', header: 'Remixer', size: 140, cell: editable('remixer') }),
|
||||||
col.accessor('producer', { id: 'producer', header: 'Producer', size: 140, cell: (c) => dash(c.getValue()) }),
|
col.accessor('producer', { id: 'producer', header: 'Producer', size: 140, cell: editable('producer') }),
|
||||||
col.accessor('mix', { id: 'mix', header: 'Mix', size: 120, cell: (c) => dash(c.getValue()) }),
|
col.accessor('mix', { id: 'mix', header: 'Mix', size: 120, cell: editable('mix') }),
|
||||||
col.accessor('comment', { id: 'comment', header: 'Comment', size: 200, cell: (c) => dash(c.getValue()) }),
|
col.accessor('comment', { id: 'comment', header: 'Comment', size: 200, cell: editable('comment') }),
|
||||||
col.accessor('bpm', {
|
col.accessor('bpm', {
|
||||||
id: 'bpm',
|
id: 'bpm',
|
||||||
header: 'BPM',
|
header: 'BPM',
|
||||||
@@ -70,7 +156,12 @@ export const TRACK_COLUMNS: ColumnDef<Track, any>[] = [
|
|||||||
id: 'rating',
|
id: 'rating',
|
||||||
header: 'Rating',
|
header: 'Rating',
|
||||||
size: 92,
|
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', {
|
col.accessor('length', {
|
||||||
id: 'length',
|
id: 'length',
|
||||||
@@ -128,10 +219,24 @@ export const TRACK_COLUMNS: ColumnDef<Track, any>[] = [
|
|||||||
id: 'release_date',
|
id: 'release_date',
|
||||||
header: 'Released',
|
header: 'Released',
|
||||||
size: 104,
|
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. */
|
/** (id, label) pairs for the Columns menu, in the canonical definition order. */
|
||||||
export const COLUMN_MENU: { id: string; label: string }[] = TRACK_COLUMNS.map((c) => ({
|
export const COLUMN_MENU: { id: string; label: string }[] = TRACK_COLUMNS.map((c) => ({
|
||||||
id: c.id as string,
|
id: c.id as string,
|
||||||
|
|||||||
Reference in new issue
Block a user