// Central definition of the library table's columns: the TanStack column defs, // 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. // // 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 { cueTypeColor } from './cues' import { RatingStars } from '../components/RatingStars' 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 /** Fields the loaded platform can actually persist * (`capabilities.tracks.editable_fields`). A field outside this set renders * read-only: the platform-level `onEditField` gate is all-or-nothing, and * platforms genuinely differ in WHICH fields they accept. Undefined means * "not gated" — every field with a handler stays editable. */ editableFields?: ReadonlySet /** Asked before an inline editor opens on a row; false keeps it closed. * A view mixing editable rows with others (a folder: collection tracks * beside loose files) answers per row — and says why itself. */ mayEdit?: (track: Track) => boolean } } 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', readOnly = false, onBegin, }: { value: string | null display?: ReactNode onCommit: (v: string) => void className?: string /** Render the value without offering an editor. */ readOnly?: boolean /** Asked on double-click; false keeps the editor closed. */ onBegin?: () => boolean }) { 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-md bg-well px-1 py-0.5 text-sm text-text outline-none ring-1 ring-accent" /> ) } if (readOnly) { return (
{display ?? (value?.trim() ? value : —)}
) } return (
{ e.stopPropagation() if (onBegin && !onBegin()) return setVal(value ?? '') setEditing(true) }} className={className} title="Double-click to edit" > {display ?? (value?.trim() ? value : —)}
) } /** Whether this row's editor may open (`TableMeta.mayEdit`). */ function mayBegin(c: CellContext): boolean { return c.table.options.meta?.mayEdit?.(c.row.original) ?? true } /** Whether this table may edit `field`, per the platform's capabilities. */ function canEdit(c: CellContext, field: string): boolean { const meta = c.table.options.meta if (!meta?.onEditField) return false return meta.editableFields ? meta.editableFields.has(field) : true } // Cell renderer factory for a plain editable text field. function editable(field: keyof Track, className?: string) { return (c: CellContext) => ( mayBegin(c)} onCommit={(v) => c.table.options.meta?.onEditField?.(c.row.original, field, v)} /> ) } export const TRACK_COLUMNS: ColumnDef[] = [ col.accessor('title', { id: 'title', header: 'Title', size: 300, 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 group-data-[active]:text-accent" readOnly={!canEdit(c, 'title')} onBegin={() => mayBegin(c)} onCommit={(v) => edit?.(r, 'title', v)} /> mayBegin(c)} onCommit={(v) => edit?.(r, 'artist', v)} />
) }, }), col.accessor('media_kind', { id: 'type', header: 'Type', size: 64, cell: (c) => c.getValue() === 'stem' ? ( // Stem file → stacked lines ("burger"), the usual stem glyph. ) : ( // Normal audio track → music note. ), }), 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('comment2', { id: 'comment2', header: 'Comment 2', size: 200, cell: editable('comment2') }), col.accessor('bpm', { id: 'bpm', header: 'BPM', size: 74, cell: (c) => {formatBpm(c.getValue())}, }), col.accessor('key', { id: 'key', header: 'Key', size: 64, cell: (c) => { const k = c.getValue() if (!k) return — return ( {k} ) }, }), col.accessor('rating', { id: 'rating', header: 'Rating', size: 92, cell: (c) => ( c.table.options.meta?.onEditField?.(c.row.original, 'rating', v) : undefined } /> ), }), col.accessor('length', { id: 'length', header: 'Time', size: 64, cell: (c) => {formatDuration(c.getValue())}, }), col.accessor('bitrate', { id: 'bitrate', header: 'Bitrate', size: 84, cell: (c) => { const b = c.getValue() return b ? ( {Math.round(b / 1000)} ) : ( — ) }, }), col.accessor('cue_count', { id: 'cue_count', header: 'Cues', size: 104, cell: (c) => { const r = c.row.original const n = c.getValue() as number const markers = r.grid_marker_count // One lit dot per occupied hotcue slot, in the cue's colour, so a row // previews the track's prep without loading it. Cues that exist but are // not in the bank (memory cues), or not yet read (a lazily-parsed // device), fall back to the count so a prepped track never looks bare. return ( 1 ? ` · flexible beatgrid (${markers} markers)` : markers ? ' · beatgrid analyzed' : '' }`} > {r.hotcues.map((h) => { const color = h.color ?? cueTypeColor(h.type) return ( ) })} {r.hotcues.length === 0 && ( 0 ? 'text-text' : 'text-faint'}`}>{n || '—'} )} {markers > 1 && ( // Gold marks a flexible (multi-tempo) grid — rare, and worth seeing. ⊞ )} ) }, }), col.accessor('playcount', { id: 'playcount', header: 'Plays', size: 68, cell: (c) => {c.getValue() || 0}, }), col.accessor('import_date', { id: 'import_date', header: 'Imported', size: 104, cell: (c) => {formatDate(c.getValue())}, }), col.accessor('last_played', { id: 'last_played', header: 'Last played', size: 108, cell: (c) => {formatDate(c.getValue())}, }), col.accessor('release_date', { id: 'release_date', header: 'Released', size: 104, cell: (c) => ( {formatDate(c.getValue() as string | null)}} readOnly={!canEdit(c, 'release_date')} onBegin={() => mayBegin(c)} onCommit={(v) => c.table.options.meta?.onEditField?.(c.row.original, 'release_date', v)} /> ), }), col.accessor('filepath', { id: 'path', header: 'Path', size: 320, // Read-only: the path is the track's identity. Truncated from the LEFT // (rtl box, ltr text) so the filename — the part you are looking for — // stays visible; the full path is the tooltip. cell: (c) => { const v = c.getValue() return v ? (
{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 header's column chooser, in definition order. */ export const COLUMN_MENU: { id: string; label: string }[] = TRACK_COLUMNS.map((c) => ({ id: c.id as string, label: typeof c.header === 'string' ? c.header : (c.id as string), })) export const DEFAULT_COLUMN_ORDER: string[] = TRACK_COLUMNS.map((c) => c.id as string) // Columns shown out of the box; the rest are opt-in via the menu. const DEFAULT_VISIBLE = new Set([ 'title', 'type', 'album', 'genre', 'bpm', 'key', 'rating', 'length', 'cue_count', 'playcount', ]) export const DEFAULT_COLUMN_VISIBILITY: VisibilityState = Object.fromEntries( DEFAULT_COLUMN_ORDER.map((id) => [id, DEFAULT_VISIBLE.has(id)]), )