mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Added reorderable library columns
This commit is contained in:
1 parent
d848efaf50
commit
db426e351a
8 files changed
+536
-212
No files matched your search
@@ -0,0 +1,67 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import type { VisibilityState } from '@tanstack/react-table'
|
||||
import { COLUMN_MENU } from '../lib/trackColumns'
|
||||
|
||||
interface Props {
|
||||
visibility: VisibilityState
|
||||
onChange: (v: VisibilityState) => void
|
||||
onReset: () => void
|
||||
}
|
||||
|
||||
/** Dropdown of checkboxes to show/hide library columns, plus a layout reset. */
|
||||
export function ColumnsMenu({ visibility, onChange, onReset }: Props) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
const onDoc = (e: MouseEvent) => {
|
||||
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false)
|
||||
}
|
||||
document.addEventListener('mousedown', onDoc)
|
||||
return () => document.removeEventListener('mousedown', onDoc)
|
||||
}, [open])
|
||||
|
||||
const isVisible = (id: string) => visibility[id] !== false // default-visible unless explicitly false
|
||||
|
||||
return (
|
||||
<div ref={ref} className="relative">
|
||||
<button
|
||||
onClick={() => setOpen((o) => !o)}
|
||||
className="flex items-center gap-1.5 rounded-md border border-line bg-ink-850 px-2.5 py-1.5 text-sm text-text transition-colors hover:border-ink-600"
|
||||
title="Choose columns"
|
||||
>
|
||||
<span className="text-faint">▦</span> Columns
|
||||
</button>
|
||||
{open && (
|
||||
<div className="absolute right-0 z-30 mt-1 w-52 rounded-md border border-line bg-ink-850 p-1 shadow-2xl">
|
||||
<div className="max-h-80 overflow-y-auto p-1">
|
||||
{COLUMN_MENU.map((c) => (
|
||||
<label
|
||||
key={c.id}
|
||||
className="flex cursor-pointer items-center gap-2 rounded px-2 py-1.5 text-sm text-text hover:bg-ink-800"
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={isVisible(c.id)}
|
||||
onChange={(e) => onChange({ ...visibility, [c.id]: e.target.checked })}
|
||||
className="accent-accent"
|
||||
/>
|
||||
{c.label}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
<button
|
||||
onClick={() => {
|
||||
onReset()
|
||||
setOpen(false)
|
||||
}}
|
||||
className="mt-1 w-full rounded px-2 py-1.5 text-left text-xs text-muted hover:bg-ink-800 hover:text-text"
|
||||
>
|
||||
Reset to defaults
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { VisibilityState } from '@tanstack/react-table'
|
||||
import { api } from '../api'
|
||||
import { ColumnsMenu } from './ColumnsMenu'
|
||||
|
||||
export interface Filters {
|
||||
search: string
|
||||
@@ -24,12 +26,21 @@ export const emptyFilters: Filters = {
|
||||
interface Props {
|
||||
filters: Filters
|
||||
onChange: (f: Filters) => void
|
||||
columnVisibility: VisibilityState
|
||||
onColumnVisibilityChange: (v: VisibilityState) => void
|
||||
onResetColumns: () => void
|
||||
}
|
||||
|
||||
const selectCls =
|
||||
'rounded-md border border-line bg-ink-850 px-2.5 py-1.5 text-sm text-text outline-none focus:border-accent hover:border-ink-600 transition-colors'
|
||||
|
||||
export function Toolbar({ filters, onChange }: Props) {
|
||||
export function Toolbar({
|
||||
filters,
|
||||
onChange,
|
||||
columnVisibility,
|
||||
onColumnVisibilityChange,
|
||||
onResetColumns,
|
||||
}: Props) {
|
||||
const { data: facets } = useQuery({ queryKey: ['facets'], queryFn: api.facets })
|
||||
const set = (patch: Partial<Filters>) => onChange({ ...filters, ...patch })
|
||||
const active =
|
||||
@@ -120,14 +131,21 @@ export function Toolbar({ filters, onChange }: Props) {
|
||||
<option value="no">No cues</option>
|
||||
</select>
|
||||
|
||||
{active && (
|
||||
<button
|
||||
onClick={() => onChange(emptyFilters)}
|
||||
className="ml-auto rounded-md px-2.5 py-1.5 text-sm text-muted hover:bg-ink-800 hover:text-text"
|
||||
>
|
||||
Clear filters
|
||||
</button>
|
||||
)}
|
||||
<div className="ml-auto flex items-center gap-2">
|
||||
{active && (
|
||||
<button
|
||||
onClick={() => onChange(emptyFilters)}
|
||||
className="rounded-md px-2.5 py-1.5 text-sm text-muted hover:bg-ink-800 hover:text-text"
|
||||
>
|
||||
Clear filters
|
||||
</button>
|
||||
)}
|
||||
<ColumnsMenu
|
||||
visibility={columnVisibility}
|
||||
onChange={onColumnVisibilityChange}
|
||||
onReset={onResetColumns}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,100 +1,33 @@
|
||||
import { useMemo, useRef } from 'react'
|
||||
import { useRef } from 'react'
|
||||
import {
|
||||
createColumnHelper,
|
||||
flexRender,
|
||||
getCoreRowModel,
|
||||
getSortedRowModel,
|
||||
useReactTable,
|
||||
type ColumnSizingState,
|
||||
type Header,
|
||||
type SortingState,
|
||||
type VisibilityState,
|
||||
} from '@tanstack/react-table'
|
||||
import { useVirtualizer } from '@tanstack/react-virtual'
|
||||
import {
|
||||
DndContext,
|
||||
PointerSensor,
|
||||
closestCenter,
|
||||
useSensor,
|
||||
useSensors,
|
||||
type DragEndEvent,
|
||||
} from '@dnd-kit/core'
|
||||
import { restrictToHorizontalAxis } from '@dnd-kit/modifiers'
|
||||
import {
|
||||
SortableContext,
|
||||
arrayMove,
|
||||
horizontalListSortingStrategy,
|
||||
useSortable,
|
||||
} from '@dnd-kit/sortable'
|
||||
import { CSS } from '@dnd-kit/utilities'
|
||||
import type { Track } from '../api'
|
||||
import { formatBpm, formatDuration, keyColor } from '../lib/format'
|
||||
import { RatingStars } from './RatingStars'
|
||||
|
||||
const col = createColumnHelper<Track>()
|
||||
|
||||
const columns = [
|
||||
col.accessor('title', {
|
||||
header: 'Title',
|
||||
size: 320,
|
||||
cell: (c) => (
|
||||
<div className="min-w-0">
|
||||
<div className="truncate font-medium text-text">
|
||||
{c.getValue() || <span className="text-faint">Untitled</span>}
|
||||
</div>
|
||||
<div className="truncate text-xs text-muted">{c.row.original.artist}</div>
|
||||
</div>
|
||||
),
|
||||
}),
|
||||
col.accessor('album', {
|
||||
header: 'Album',
|
||||
size: 200,
|
||||
cell: (c) => <span className="truncate text-muted">{c.getValue() || '—'}</span>,
|
||||
}),
|
||||
col.accessor('genre', {
|
||||
header: 'Genre',
|
||||
size: 150,
|
||||
cell: (c) => {
|
||||
const g = c.getValue()?.trim()
|
||||
return g ? (
|
||||
<span className="truncate text-muted">{g}</span>
|
||||
) : (
|
||||
<span className="text-faint">—</span>
|
||||
)
|
||||
},
|
||||
}),
|
||||
col.accessor('bpm', {
|
||||
header: 'BPM',
|
||||
size: 74,
|
||||
cell: (c) => <span className="tabular-nums text-text">{formatBpm(c.getValue())}</span>,
|
||||
}),
|
||||
col.accessor('key', {
|
||||
header: 'Key',
|
||||
size: 64,
|
||||
cell: (c) => {
|
||||
const k = c.getValue()
|
||||
if (!k) return <span className="text-faint">—</span>
|
||||
return (
|
||||
<span
|
||||
className="rounded px-1.5 py-0.5 text-xs font-semibold"
|
||||
style={{ color: keyColor(k), background: 'color-mix(in srgb, currentColor 14%, transparent)' }}
|
||||
>
|
||||
{k}
|
||||
</span>
|
||||
)
|
||||
},
|
||||
}),
|
||||
col.accessor('rating', {
|
||||
header: 'Rating',
|
||||
size: 92,
|
||||
cell: (c) => <RatingStars value={c.getValue()} />,
|
||||
}),
|
||||
col.accessor('length', {
|
||||
header: 'Time',
|
||||
size: 64,
|
||||
cell: (c) => <span className="tabular-nums text-muted">{formatDuration(c.getValue())}</span>,
|
||||
}),
|
||||
col.accessor('cue_count', {
|
||||
header: 'Cues',
|
||||
size: 72,
|
||||
cell: (c) => {
|
||||
const n = c.getValue()
|
||||
const grid = c.row.original.has_grid
|
||||
return (
|
||||
<span className="flex items-center gap-1 tabular-nums">
|
||||
<span className={n > 0 ? 'text-text' : 'text-faint'}>{n}</span>
|
||||
{grid && <span className="text-[10px] text-mint" title="Beatgrid analyzed">⊞</span>}
|
||||
</span>
|
||||
)
|
||||
},
|
||||
}),
|
||||
col.accessor('playcount', {
|
||||
header: 'Plays',
|
||||
size: 68,
|
||||
cell: (c) => <span className="tabular-nums text-muted">{c.getValue() || 0}</span>,
|
||||
}),
|
||||
]
|
||||
import { TRACK_COLUMNS } from '../lib/trackColumns'
|
||||
|
||||
interface Selection {
|
||||
selected: Set<string>
|
||||
@@ -107,16 +40,69 @@ interface Props {
|
||||
tracks: Track[]
|
||||
sorting: SortingState
|
||||
onSortingChange: (s: SortingState) => void
|
||||
columnVisibility: VisibilityState
|
||||
columnOrder: string[]
|
||||
columnSizing: ColumnSizingState
|
||||
onColumnOrderChange: (o: string[]) => void
|
||||
onColumnSizingChange: (s: ColumnSizingState) => void
|
||||
selection?: Selection
|
||||
onRowContextMenu?: (track: Track, x: number, y: number) => void
|
||||
onPlay?: (track: Track) => void
|
||||
activeTrackId?: string | null
|
||||
}
|
||||
|
||||
/** One draggable + resizable header cell. */
|
||||
function SortableHeader({ header }: { header: Header<Track, unknown> }) {
|
||||
const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
|
||||
id: header.column.id,
|
||||
})
|
||||
const sorted = header.column.getIsSorted()
|
||||
return (
|
||||
<div
|
||||
ref={setNodeRef}
|
||||
style={{
|
||||
width: header.getSize(),
|
||||
transform: CSS.Translate.toString(transform),
|
||||
transition,
|
||||
opacity: isDragging ? 0.6 : 1,
|
||||
zIndex: isDragging ? 20 : undefined,
|
||||
}}
|
||||
className="relative flex shrink-0 items-center"
|
||||
>
|
||||
<button
|
||||
{...attributes}
|
||||
{...listeners}
|
||||
onClick={header.column.getToggleSortingHandler()}
|
||||
className="flex flex-1 cursor-grab items-center gap-1 px-3 py-2.5 text-left text-[11px] font-semibold uppercase tracking-wider text-muted hover:text-text active:cursor-grabbing"
|
||||
>
|
||||
{flexRender(header.column.columnDef.header, header.getContext())}
|
||||
<span className="text-accent">
|
||||
{sorted === 'asc' ? '↑' : sorted === 'desc' ? '↓' : ''}
|
||||
</span>
|
||||
</button>
|
||||
{header.column.getCanResize() && (
|
||||
<div
|
||||
onMouseDown={header.getResizeHandler()}
|
||||
onTouchStart={header.getResizeHandler()}
|
||||
onPointerDown={(e) => e.stopPropagation()}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
className="absolute right-0 top-0 h-full w-1.5 cursor-col-resize touch-none select-none hover:bg-accent/40"
|
||||
title="Drag to resize"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function TrackTable({
|
||||
tracks,
|
||||
sorting,
|
||||
onSortingChange,
|
||||
columnVisibility,
|
||||
columnOrder,
|
||||
columnSizing,
|
||||
onColumnOrderChange,
|
||||
onColumnSizingChange,
|
||||
selection,
|
||||
onRowContextMenu,
|
||||
onPlay,
|
||||
@@ -124,12 +110,16 @@ export function TrackTable({
|
||||
}: Props) {
|
||||
const table = useReactTable({
|
||||
data: tracks,
|
||||
columns,
|
||||
state: { sorting },
|
||||
columns: TRACK_COLUMNS,
|
||||
state: { sorting, columnVisibility, columnOrder, columnSizing },
|
||||
onSortingChange: (updater) =>
|
||||
onSortingChange(typeof updater === 'function' ? updater(sorting) : updater),
|
||||
onColumnSizingChange: (updater) =>
|
||||
onColumnSizingChange(typeof updater === 'function' ? updater(columnSizing) : updater),
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
getSortedRowModel: getSortedRowModel(),
|
||||
enableColumnResizing: true,
|
||||
columnResizeMode: 'onChange',
|
||||
})
|
||||
|
||||
const rows = table.getRowModel().rows
|
||||
@@ -141,123 +131,130 @@ export function TrackTable({
|
||||
estimateSize: () => 44,
|
||||
overscan: 12,
|
||||
})
|
||||
|
||||
const totalWidth = useMemo(
|
||||
() => table.getTotalSize(),
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[],
|
||||
)
|
||||
const virtualRows = virtualizer.getVirtualItems()
|
||||
|
||||
const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 5 } }))
|
||||
const headers = table.getHeaderGroups()[0].headers
|
||||
const leadWidth = 40 + (onPlay ? 36 : 0)
|
||||
const totalWidth = table.getTotalSize() + leadWidth
|
||||
|
||||
const onDragEnd = (e: DragEndEvent) => {
|
||||
const { active, over } = e
|
||||
if (!over || active.id === over.id) return
|
||||
const from = columnOrder.indexOf(active.id as string)
|
||||
const to = columnOrder.indexOf(over.id as string)
|
||||
if (from < 0 || to < 0) return
|
||||
onColumnOrderChange(arrayMove(columnOrder, from, to))
|
||||
}
|
||||
|
||||
return (
|
||||
<div ref={parentRef} className="h-full overflow-auto">
|
||||
<div style={{ width: totalWidth, minWidth: '100%' }}>
|
||||
{/* Header */}
|
||||
<div className="sticky top-0 z-10 flex border-b border-line bg-ink-850">
|
||||
{selection ? (
|
||||
<span className="flex w-10 shrink-0 items-center justify-center">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selection.allSelected}
|
||||
onChange={selection.onToggleAll}
|
||||
className="accent-accent"
|
||||
title="Select all"
|
||||
/>
|
||||
</span>
|
||||
) : (
|
||||
<span className="w-10 shrink-0" />
|
||||
)}
|
||||
{onPlay && <span className="w-9 shrink-0" />}
|
||||
{table.getHeaderGroups()[0].headers.map((header) => {
|
||||
const sorted = header.column.getIsSorted()
|
||||
return (
|
||||
<button
|
||||
key={header.id}
|
||||
onClick={header.column.getToggleSortingHandler()}
|
||||
style={{ width: header.getSize() }}
|
||||
className="flex items-center gap-1 px-3 py-2.5 text-left text-[11px] font-semibold uppercase tracking-wider text-muted hover:text-text"
|
||||
>
|
||||
{flexRender(header.column.columnDef.header, header.getContext())}
|
||||
<span className="text-accent">
|
||||
{sorted === 'asc' ? '↑' : sorted === 'desc' ? '↓' : ''}
|
||||
</span>
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
<DndContext
|
||||
sensors={sensors}
|
||||
collisionDetection={closestCenter}
|
||||
modifiers={[restrictToHorizontalAxis]}
|
||||
onDragEnd={onDragEnd}
|
||||
>
|
||||
<div style={{ width: totalWidth, minWidth: '100%' }}>
|
||||
{/* Header */}
|
||||
<div className="sticky top-0 z-10 flex border-b border-line bg-ink-850">
|
||||
{selection ? (
|
||||
<span className="flex w-10 shrink-0 items-center justify-center">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selection.allSelected}
|
||||
onChange={selection.onToggleAll}
|
||||
className="accent-accent"
|
||||
title="Select all"
|
||||
/>
|
||||
</span>
|
||||
) : (
|
||||
<span className="w-10 shrink-0" />
|
||||
)}
|
||||
{onPlay && <span className="w-9 shrink-0" />}
|
||||
<SortableContext
|
||||
items={headers.map((h) => h.column.id)}
|
||||
strategy={horizontalListSortingStrategy}
|
||||
>
|
||||
{headers.map((header) => (
|
||||
<SortableHeader key={header.id} header={header} />
|
||||
))}
|
||||
</SortableContext>
|
||||
</div>
|
||||
|
||||
{/* Virtualized rows */}
|
||||
<div style={{ height: virtualizer.getTotalSize(), position: 'relative' }}>
|
||||
{virtualRows.map((vr) => {
|
||||
const row = rows[vr.index]
|
||||
const isSelected = selection?.selected.has(row.original.id) ?? false
|
||||
const isActive = activeTrackId === row.original.id
|
||||
return (
|
||||
<div
|
||||
key={row.id}
|
||||
className={`group absolute left-0 flex items-center border-b border-ink-850 text-sm ${
|
||||
isSelected ? 'bg-accent-soft/50' : 'hover:bg-ink-850'
|
||||
}`}
|
||||
style={{
|
||||
top: 0,
|
||||
transform: `translateY(${vr.start}px)`,
|
||||
height: vr.size,
|
||||
width: '100%',
|
||||
}}
|
||||
onContextMenu={(e) => {
|
||||
if (!onRowContextMenu) return
|
||||
e.preventDefault()
|
||||
onRowContextMenu(row.original, e.clientX, e.clientY)
|
||||
}}
|
||||
>
|
||||
{selection ? (
|
||||
<span className="flex w-10 shrink-0 items-center justify-center">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={isSelected}
|
||||
onChange={() => selection.onToggle(row.original.id)}
|
||||
className="accent-accent"
|
||||
/>
|
||||
</span>
|
||||
) : (
|
||||
<span className="w-10 shrink-0 pr-2 text-right text-xs tabular-nums text-faint">
|
||||
{vr.index + 1}
|
||||
</span>
|
||||
)}
|
||||
{onPlay && (
|
||||
<span className="flex w-9 shrink-0 items-center justify-center">
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onPlay(row.original)
|
||||
}}
|
||||
className={`flex h-6 w-6 items-center justify-center rounded-full transition-colors hover:bg-accent hover:text-ink-950 ${
|
||||
isActive
|
||||
? 'text-accent opacity-100'
|
||||
: 'text-faint opacity-0 group-hover:opacity-100'
|
||||
}`}
|
||||
title="Play in deck"
|
||||
{/* Virtualized rows */}
|
||||
<div style={{ height: virtualizer.getTotalSize(), position: 'relative' }}>
|
||||
{virtualRows.map((vr) => {
|
||||
const row = rows[vr.index]
|
||||
const isSelected = selection?.selected.has(row.original.id) ?? false
|
||||
const isActive = activeTrackId === row.original.id
|
||||
return (
|
||||
<div
|
||||
key={row.id}
|
||||
className={`group absolute left-0 flex items-center border-b border-ink-850 text-sm ${
|
||||
isSelected ? 'bg-accent-soft/50' : 'hover:bg-ink-850'
|
||||
}`}
|
||||
style={{
|
||||
top: 0,
|
||||
transform: `translateY(${vr.start}px)`,
|
||||
height: vr.size,
|
||||
width: '100%',
|
||||
}}
|
||||
onContextMenu={(e) => {
|
||||
if (!onRowContextMenu) return
|
||||
e.preventDefault()
|
||||
onRowContextMenu(row.original, e.clientX, e.clientY)
|
||||
}}
|
||||
>
|
||||
{selection ? (
|
||||
<span className="flex w-10 shrink-0 items-center justify-center">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={isSelected}
|
||||
onChange={() => selection.onToggle(row.original.id)}
|
||||
className="accent-accent"
|
||||
/>
|
||||
</span>
|
||||
) : (
|
||||
<span className="w-10 shrink-0 pr-2 text-right text-xs tabular-nums text-faint">
|
||||
{vr.index + 1}
|
||||
</span>
|
||||
)}
|
||||
{onPlay && (
|
||||
<span className="flex w-9 shrink-0 items-center justify-center">
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onPlay(row.original)
|
||||
}}
|
||||
className={`flex h-6 w-6 items-center justify-center rounded-full transition-colors hover:bg-accent hover:text-ink-950 ${
|
||||
isActive
|
||||
? 'text-accent opacity-100'
|
||||
: 'text-faint opacity-0 group-hover:opacity-100'
|
||||
}`}
|
||||
title="Play in deck"
|
||||
>
|
||||
<svg width="11" height="11" viewBox="0 0 24 24" fill="currentColor" aria-hidden>
|
||||
<path d="M8 5.14v13.72a1 1 0 0 0 1.54.84l10.29-6.86a1 1 0 0 0 0-1.68L9.54 4.3A1 1 0 0 0 8 5.14z" />
|
||||
</svg>
|
||||
</button>
|
||||
</span>
|
||||
)}
|
||||
{row.getVisibleCells().map((cell) => (
|
||||
<div
|
||||
key={cell.id}
|
||||
style={{ width: cell.column.getSize() }}
|
||||
className="flex shrink-0 items-center overflow-hidden px-3"
|
||||
>
|
||||
<svg width="11" height="11" viewBox="0 0 24 24" fill="currentColor" aria-hidden>
|
||||
<path d="M8 5.14v13.72a1 1 0 0 0 1.54.84l10.29-6.86a1 1 0 0 0 0-1.68L9.54 4.3A1 1 0 0 0 8 5.14z" />
|
||||
</svg>
|
||||
</button>
|
||||
</span>
|
||||
)}
|
||||
{row.getVisibleCells().map((cell) => (
|
||||
<div
|
||||
key={cell.id}
|
||||
style={{ width: cell.column.getSize() }}
|
||||
className="flex items-center overflow-hidden px-3"
|
||||
>
|
||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</DndContext>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user