mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
455 lines
16 KiB
TypeScript
455 lines
16 KiB
TypeScript
// Central definition of the library table's columns: the TanStack column defs,
|
|
// their default widths/visibility/order, and the (id,label) list the header's
|
|
// right-click column chooser renders. TrackTable and that menu both 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, formatLoudness, 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<TData extends RowData> {
|
|
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<string>
|
|
/** 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<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',
|
|
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 (
|
|
<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-md bg-well px-1 py-0.5 text-sm text-text outline-none ring-1 ring-accent"
|
|
/>
|
|
)
|
|
}
|
|
if (readOnly) {
|
|
return (
|
|
<div className={className}>
|
|
{display ?? (value?.trim() ? value : <span className="text-faint">—</span>)}
|
|
</div>
|
|
)
|
|
}
|
|
return (
|
|
<div
|
|
onDoubleClick={(e) => {
|
|
e.stopPropagation()
|
|
if (onBegin && !onBegin()) return
|
|
setVal(value ?? '')
|
|
setEditing(true)
|
|
}}
|
|
className={className}
|
|
title="Double-click to edit"
|
|
>
|
|
{display ?? (value?.trim() ? value : <span className="text-faint">—</span>)}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/** Whether this row's editor may open (`TableMeta.mayEdit`). */
|
|
function mayBegin(c: CellContext<Track, unknown>): 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<Track, unknown>, 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<Track, unknown>) => (
|
|
<InlineEdit
|
|
value={c.getValue() as string | null}
|
|
className={className}
|
|
readOnly={!canEdit(c, field)}
|
|
onBegin={() => mayBegin(c)}
|
|
onCommit={(v) => c.table.options.meta?.onEditField?.(c.row.original, field, v)}
|
|
/>
|
|
)
|
|
}
|
|
|
|
export const TRACK_COLUMNS: ColumnDef<Track, any>[] = [
|
|
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 (
|
|
<div className="min-w-0">
|
|
<InlineEdit
|
|
value={title}
|
|
display={title || <span className="text-faint">Untitled</span>}
|
|
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)}
|
|
/>
|
|
<InlineEdit
|
|
value={r.artist}
|
|
className="w-full min-w-0 truncate text-xs text-muted"
|
|
readOnly={!canEdit(c, 'artist')}
|
|
onBegin={() => mayBegin(c)}
|
|
onCommit={(v) => edit?.(r, 'artist', v)}
|
|
/>
|
|
</div>
|
|
)
|
|
},
|
|
}),
|
|
col.accessor('media_kind', {
|
|
id: 'type',
|
|
header: 'Type',
|
|
size: 64,
|
|
// The icon takes the track's colour tag, when it has one; otherwise both
|
|
// glyphs are neutral, so colour in this column only ever means the tag.
|
|
cell: (c) => {
|
|
const color = c.row.original.color_hex
|
|
const tint = color ? undefined : 'text-faint'
|
|
return c.getValue() === 'stem' ? (
|
|
// Stem file → stacked lines ("burger"), the usual stem glyph.
|
|
<span title="Stem file" className={tint} style={color ? { color } : undefined}>
|
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-label="Stem">
|
|
<rect x="3" y="6" width="18" height="2.4" rx="1.2" />
|
|
<rect x="3" y="10.8" width="18" height="2.4" rx="1.2" />
|
|
<rect x="3" y="15.6" width="18" height="2.4" rx="1.2" />
|
|
</svg>
|
|
</span>
|
|
) : (
|
|
// Normal audio track → music note.
|
|
<span title="Audio track" className={tint} style={color ? { color } : undefined}>
|
|
<svg width="15" height="15" viewBox="0 0 24 24" fill="currentColor" aria-label="Audio">
|
|
<path d="M12 3v10.55A4 4 0 1 0 14 17V7h4V3h-6z" />
|
|
</svg>
|
|
</span>
|
|
)
|
|
},
|
|
}),
|
|
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) => <span className="font-mono text-xs text-text">{formatBpm(c.getValue())}</span>,
|
|
}),
|
|
col.accessor('key', {
|
|
id: 'key',
|
|
header: 'Key',
|
|
size: 64,
|
|
cell: (c) => {
|
|
const k = c.getValue()
|
|
if (!k) return <span className="text-faint">—</span>
|
|
return (
|
|
<span
|
|
className="inline-block min-w-[2.1rem] rounded-md px-1.5 py-0.5 text-center font-mono text-[11px] font-semibold"
|
|
style={{
|
|
color: keyColor(c.row.original.key_wheel),
|
|
background: 'color-mix(in srgb, currentColor 16%, transparent)',
|
|
boxShadow: 'inset 0 0 0 1px color-mix(in srgb, currentColor 32%, transparent)',
|
|
}}
|
|
>
|
|
{k}
|
|
</span>
|
|
)
|
|
},
|
|
}),
|
|
col.accessor('rating', {
|
|
id: 'rating',
|
|
header: 'Rating',
|
|
size: 92,
|
|
cell: (c) => (
|
|
<RatingStars
|
|
value={c.getValue() as number}
|
|
// RatingStars is read-only without an onChange, which is exactly the
|
|
// behaviour wanted when the platform cannot persist a rating.
|
|
onChange={
|
|
canEdit(c, 'rating')
|
|
? (v) => c.table.options.meta?.onEditField?.(c.row.original, 'rating', v)
|
|
: undefined
|
|
}
|
|
/>
|
|
),
|
|
}),
|
|
col.accessor('length', {
|
|
id: 'length',
|
|
header: 'Time',
|
|
size: 64,
|
|
cell: (c) => <span className="font-mono text-xs text-muted">{formatDuration(c.getValue())}</span>,
|
|
}),
|
|
col.accessor('bitrate', {
|
|
id: 'bitrate',
|
|
header: 'Bitrate',
|
|
size: 84,
|
|
cell: (c) => {
|
|
const b = c.getValue()
|
|
return b ? (
|
|
<span className="tabular-nums text-muted">{Math.round(b / 1000)}</span>
|
|
) : (
|
|
<span className="text-faint">—</span>
|
|
)
|
|
},
|
|
}),
|
|
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 (
|
|
<span
|
|
className="flex items-center gap-1"
|
|
title={`${n} cue${n === 1 ? '' : 's'}${
|
|
markers > 1 ? ` · flexible beatgrid (${markers} markers)` : markers ? ' · beatgrid analyzed' : ''
|
|
}`}
|
|
>
|
|
{r.hotcues.map((h) => {
|
|
const color = h.color ?? cueTypeColor(h.type)
|
|
return (
|
|
<span
|
|
key={h.slot}
|
|
className="h-2 w-2 shrink-0 rounded-full"
|
|
style={{ background: color, boxShadow: `0 0 6px ${color}` }}
|
|
/>
|
|
)
|
|
})}
|
|
{r.hotcues.length === 0 && (
|
|
<span className={`font-mono text-xs ${n > 0 ? 'text-text' : 'text-faint'}`}>{n || '—'}</span>
|
|
)}
|
|
{markers > 1 && (
|
|
// Gold marks a flexible (multi-tempo) grid — rare, and worth seeing.
|
|
<span className="ml-0.5 text-[10px] text-gold">⊞</span>
|
|
)}
|
|
</span>
|
|
)
|
|
},
|
|
}),
|
|
col.accessor('playcount', {
|
|
id: 'playcount',
|
|
header: 'Plays',
|
|
size: 68,
|
|
cell: (c) => <span className="font-mono text-xs text-muted">{c.getValue() || 0}</span>,
|
|
}),
|
|
col.accessor('import_date', {
|
|
id: 'import_date',
|
|
header: 'Imported',
|
|
size: 104,
|
|
cell: (c) => <span className="tabular-nums text-muted">{formatDate(c.getValue())}</span>,
|
|
}),
|
|
col.accessor('last_played', {
|
|
id: 'last_played',
|
|
header: 'Last played',
|
|
size: 108,
|
|
cell: (c) => <span className="tabular-nums text-muted">{formatDate(c.getValue())}</span>,
|
|
}),
|
|
col.accessor('release_date', {
|
|
id: 'release_date',
|
|
header: 'Released',
|
|
size: 104,
|
|
cell: (c) => (
|
|
<InlineEdit
|
|
value={c.getValue() as string | null}
|
|
display={<span className="tabular-nums text-muted">{formatDate(c.getValue() as string | null)}</span>}
|
|
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 ? (
|
|
<div
|
|
className="w-full truncate text-xs text-muted"
|
|
style={{ direction: 'rtl', textAlign: 'left' }}
|
|
title={v}
|
|
>
|
|
<bdi>{v}</bdi>
|
|
</div>
|
|
) : (
|
|
<span className="text-faint">—</span>
|
|
)
|
|
},
|
|
}),
|
|
// The Mastering columns (`core/loudness.py`), grouped in the column chooser.
|
|
// Unmeasured is `undefined`, never null: TanStack sorts null as 0, which
|
|
// would rank every unmeasured track as the loudest; `sortUndefined: 'last'`
|
|
// keeps them at the bottom whichever way the column is sorted.
|
|
loudnessColumn('lufs', 'LUFS', 'LUFS'),
|
|
loudnessColumn('rms', 'RMS', 'dB'),
|
|
loudnessColumn('true_peak', 'True Peak', 'dBTP', true),
|
|
loudnessColumn('lra', 'LRA', 'LU'),
|
|
loudnessColumn('plr', 'PLR', 'dB'),
|
|
// The DJ app's own auto gain (Traktor's deck gain, rekordbox's Auto Gain),
|
|
// from the backend's Track — not Konduktor's loudness store — so showing it
|
|
// fetches nothing. None sorts last, like an unmeasured loudness.
|
|
col.accessor((t) => t.auto_gain_db ?? undefined, {
|
|
id: 'auto_gain',
|
|
header: 'Auto Gain',
|
|
size: 104,
|
|
sortUndefined: 'last',
|
|
cell: (c) => {
|
|
const v = c.getValue() as number | undefined
|
|
if (v == null) return <span className="text-faint" title="No auto gain: Mastering ▸ Auto gain… sets one">—</span>
|
|
return (
|
|
<span className="whitespace-nowrap font-mono text-xs tabular-nums text-muted">
|
|
{formatLoudness(v, 'dB', true)}
|
|
</span>
|
|
)
|
|
},
|
|
}),
|
|
]
|
|
|
|
function loudnessColumn(field: 'lufs' | 'rms' | 'true_peak' | 'lra' | 'plr', header: string, unit: string,
|
|
signed = false): ColumnDef<Track, any> {
|
|
return col.accessor((t) => t.loudness?.[field], {
|
|
id: field,
|
|
header,
|
|
size: field === 'lufs' || field === 'true_peak' ? 112 : 96,
|
|
sortUndefined: 'last',
|
|
cell: (c) => {
|
|
const v = c.getValue() as number | undefined
|
|
if (v == null) return <span className="text-faint">—</span>
|
|
const stale = c.row.original.loudness?.stale
|
|
return (
|
|
<span
|
|
className={`whitespace-nowrap font-mono text-xs tabular-nums ${stale ? 'text-faint' : 'text-muted'}`}
|
|
title={stale ? 'File changed since analysed — Analyze again to refresh' : undefined}
|
|
>
|
|
{formatLoudness(v, unit, signed)}
|
|
</span>
|
|
)
|
|
},
|
|
})
|
|
}
|
|
|
|
/** The loudness columns: shown, they fetch `/api/loudness` (see App). */
|
|
export const MASTERING_COLUMNS: readonly string[] = ['lufs', 'rms', 'true_peak', 'lra', 'plr']
|
|
/** The column chooser's "Mastering ▸", in order: loudness, then auto gain. */
|
|
const MASTERING_MENU: readonly string[] = [...MASTERING_COLUMNS, 'auto_gain']
|
|
const MASTERING_LABELS: Record<string, string> = {
|
|
lufs: 'LUFS (integrated loudness)',
|
|
rms: 'RMS (loudest sections)',
|
|
true_peak: 'True Peak',
|
|
lra: 'LRA (loudness range)',
|
|
plr: 'PLR (peak to loudness)',
|
|
auto_gain: "Auto Gain (the DJ app's)",
|
|
}
|
|
|
|
// 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.
|
|
* `group` puts a column in a submenu (the Mastering columns). */
|
|
export const COLUMN_MENU: { id: string; label: string; group?: 'mastering' }[] = TRACK_COLUMNS.map((c) => {
|
|
const id = c.id as string
|
|
const mastering = MASTERING_MENU.includes(id)
|
|
return {
|
|
id,
|
|
label: MASTERING_LABELS[id] ?? (typeof c.header === 'string' ? c.header : id),
|
|
...(mastering ? { group: 'mastering' as const } : {}),
|
|
}
|
|
})
|
|
|
|
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)]),
|
|
)
|