Dj deck is now a reusable component

This commit is contained in:
patben8 committed 2026-11-02 22:48:04 +01:00
1 parent 2fcf6e7070
commit 063463efdd
9 files changed
+2310 -1784

No files matched your search

File diff suppressed because it is too large. Load diff
@@ -0,0 +1,232 @@
import { Icon } from '../../lib/icons'
import { GridEditStrip, TempoControls, TempoFold } from '../GridControls'
import { HotcueBar } from '../HotcueBar'
import { LoopControls } from '../LoopControls'
import { MemoryCueStrip } from '../MemoryCueStrip'
import { ModeSwitch } from '../ModeSwitch'
import type { Deck } from './useDeck'
const DIVIDER = <span aria-hidden className="h-7 w-px shrink-0 bg-line" />
/**
* The deck's controls row: transport · loop/jump · pads or grid · tempo.
*
* A container, so the row gives way in stages as it narrows instead of the
* pads sliding under the tempo group: the pads drop their names first (each pad
* queries its own width, see HotcueBar), then below 77rem the tempo group folds
* into one BPM button. That is the row's measured width just before the pad
* bank would hit its minimum (with a lockable grid's extra Lock button, and the
* Hot / Mem toggle's ~3rem with its gap), so move it with the controls.
*/
export function DeckControls({ deck }: { deck: Deck }) {
const {
caps,
track,
toggle,
ready,
previewing,
playing,
onCuePress,
onCueRelease,
cueHeld,
loopMode,
setLoopMode,
grid,
beatSize,
stepSize,
loopActive,
activeBeats,
toggleSizeLoop,
loopRegion,
loopInPoint,
loopIn,
loopOut,
toggleLoop,
moveOrJump,
gridMode,
setGridMode,
bankMode,
setBankMode,
setPadMenu,
setMemoryMenu,
setRenamingSlot,
setRenamingMemory,
activeMarkerIndex,
markerCount,
activeMarker,
atMarker,
beforeFirstMarker,
originalGridRef,
prevMarker,
nextMarker,
nudgeGrid,
nudgeGridBeats,
addMarkerHere,
deleteMarkerHere,
deleteGrid,
resetGrid,
showMemory,
cueData,
cuePoint,
callMemoryCue,
addMemoryCue,
memoryAddRefusal,
renamingMemory,
renameMemoryCue,
slotCount,
slotLabel,
selectedSlot,
setSelectedSlot,
onSlotPress,
onSlotRelease,
hotcueAt,
renamingSlot,
renameSlot,
tempoProps,
} = deck
return (
<div className="@container flex h-11 shrink-0 items-center gap-3">
<button
onClick={toggle}
disabled={!ready}
className={
'flex h-10 w-10 shrink-0 items-center justify-center rounded-full transition-all active:scale-95 disabled:cursor-not-allowed disabled:opacity-40 ' +
(previewing
? 'bg-gradient-to-b from-white to-gold text-ink-950 shadow-[0_0_0_4px_rgb(255_200_97/0.22),0_8px_24px_-4px_var(--color-gold)]'
: playing
? 'btn-primary shadow-[0_0_0_4px_color-mix(in_oklab,var(--color-accent)_22%,transparent),0_8px_24px_-4px_var(--color-accent)]'
: 'btn-glass text-text')
}
title={playing ? 'Pause (Space)' : 'Play (Space)'}
>
<Icon name={playing ? 'pause' : 'play'} size={16} />
</button>
<button
onPointerDown={(e) => {
e.preventDefault()
e.currentTarget.setPointerCapture(e.pointerId)
onCuePress()
}}
onPointerUp={onCueRelease}
onPointerCancel={onCueRelease}
disabled={!ready}
aria-pressed={cueHeld}
className={`flex h-10 w-14 shrink-0 items-center justify-center rounded-xl font-mono text-[13px] font-semibold tracking-wider transition-[background-color,box-shadow,transform,color] duration-75 disabled:cursor-not-allowed disabled:opacity-40 ${
cueHeld
? // Held: lit like a pressed controller button — brighter fill,
// solid gold rim, a glow, and a slight press-in.
'scale-[0.96] bg-gold/35 text-[#fff1cc] shadow-[inset_0_1px_0_rgb(255_255_255/0.3),inset_0_0_0_1px_rgb(255_200_97/0.9),0_0_20px_-2px_rgb(255_200_97/0.75)]'
: 'bg-gold/10 text-gold shadow-[inset_0_1px_0_rgb(255_255_255/0.12),inset_0_0_0_1px_rgb(255_200_97/0.4)] hover:bg-gold/15'
}`}
title="Cue — set the cue point (paused), hold to preview from it, or jump back to it (playing) (C)"
>
CUE
</button>
{DIVIDER}
<LoopControls
mode={loopMode}
onMode={setLoopMode}
hasGrid={grid != null}
ready={ready}
size={beatSize}
onSmaller={() => stepSize(-1)}
onBigger={() => stepSize(1)}
loopActive={loopActive}
sizeLit={loopActive && activeBeats === beatSize}
onToggleSize={toggleSizeLoop}
canToggle={loopRegion != null}
inArmed={loopInPoint != null}
manualSet={loopRegion != null && activeBeats == null}
onLoopIn={loopIn}
onLoopOut={loopOut}
onToggleLoop={toggleLoop}
onMove={moveOrJump}
/>
{DIVIDER}
{caps.cues.memory_cues && !gridMode && (
<ModeSwitch
label="Cue bank"
value={bankMode}
options={[
{ value: 'hot', text: 'HOT', title: 'Hot cues: the pads' },
{ value: 'memory', text: 'MEM', title: 'Memory cues: call one to make it the CUE point' },
]}
onChange={(m) => {
setBankMode(m)
setPadMenu(null)
setMemoryMenu(null)
setRenamingSlot(null)
setRenamingMemory(null)
}}
/>
)}
{gridMode ? (
<GridEditStrip
markerIndex={activeMarkerIndex}
markerCount={markerCount}
markerStart={activeMarker?.start ?? null}
atMarker={atMarker}
beforeFirst={beforeFirstMarker}
canReset={originalGridRef.current != null}
onPrevMarker={prevMarker}
onNextMarker={nextMarker}
onNudgeMarker={nudgeGrid}
onNudgeMarkerBeats={nudgeGridBeats}
onAddMarker={addMarkerHere}
onDeleteMarker={deleteMarkerHere}
onDeleteGrid={deleteGrid}
onReset={resetGrid}
/>
) : showMemory ? (
<MemoryCueStrip
cues={cueData?.cues ?? []}
cuePoint={cuePoint}
onCall={callMemoryCue}
onMenu={(cue, x, y) => setMemoryMenu({ cue, x, y })}
onAdd={() => void addMemoryCue()}
addRefusal={memoryAddRefusal}
renamingId={renamingMemory}
onRenameCommit={renameMemoryCue}
onRenameCancel={() => setRenamingMemory(null)}
/>
) : (
<HotcueBar
cues={cueData?.cues ?? []}
slotCount={slotCount}
slotLabel={slotLabel}
selectedSlot={selectedSlot}
onSlotPress={onSlotPress}
onSlotRelease={onSlotRelease}
onSlotMenu={(slot, x, y) => {
setSelectedSlot(slot)
if (hotcueAt(slot)) setPadMenu({ slot, x, y })
}}
renamingSlot={renamingSlot}
onRenameCommit={renameSlot}
onRenameCancel={() => setRenamingSlot(null)}
/>
)}
{DIVIDER}
<ModeSwitch
label="Deck mode"
value={gridMode ? 'grid' : 'cues'}
options={[
{ value: 'cues', text: 'CUES', title: 'Cues: the hotcue pads or memory cues' },
{ value: 'grid', text: 'GRID', title: 'Edit the beatgrid: markers, phase, reset' },
]}
disabled={!track}
onChange={(m) => {
setGridMode(m === 'grid')
setPadMenu(null)
setRenamingSlot(null)
setMemoryMenu(null)
setRenamingMemory(null)
}}
/>
<TempoControls {...tempoProps} className="@max-[77rem]:hidden" />
<TempoFold locked={tempoProps.locked} className="hidden @max-[77rem]:flex">
<TempoControls {...tempoProps} />
</TempoFold>
</div>
)
}
+182
View File
@@ -0,0 +1,182 @@
import { useState, type ReactNode } from 'react'
import { api } from '../../api'
import { keyColor } from '../../lib/format'
import { Icon } from '../../lib/icons'
import { readOnlyNotice, readOnlyShort } from '../../lib/platformCopy'
import { useAppSetting } from '../AppSettings'
import { BpmReadout } from '../GridControls'
import type { Deck } from './useDeck'
/** mm:ss.f — tenths by default, as a CDJ does; `places` is the
* `timeReadoutDecimals` setting (0 drops the fraction). Truncates, never
* rounds, so the readout never shows a second before it has been reached. */
function fmtTime(secs: number, places: number): string {
if (!isFinite(secs) || secs < 0) secs = 0
const scale = 10 ** places
// The epsilon keeps e.g. 0.3 s from reading 0.299 at 3 places.
const units = Math.floor(secs * scale + 1e-6)
const whole = Math.floor(units / scale)
const m = Math.floor(whole / 60)
const s = whole % 60
const frac = places > 0 ? `.${(units % scale).toString().padStart(places, '0')}` : ''
return `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}${frac}`
}
/** The deck's header: the track, its readouts, the analysis actions — and
* `actions`, anything the layout adds after them. */
export function DeckHeader({ deck, actions }: { deck: Deck; actions?: ReactNode }) {
const {
caps,
track,
origin,
subtitle,
setInfoOpen,
current,
duration,
activeMarker,
canEditGrid,
commitBpm,
gridAnchorRef,
playheadNow,
bpmEntryRequest,
snap,
setSnap,
runAnalyzeGrid,
gridBusy,
openAutoHotcues,
canAutoCue,
} = deck
const timeDecimals = useAppSetting('timeReadoutDecimals')
return (
<div className="flex h-12 shrink-0 items-center gap-3">
<CoverThumb
trackId={track && origin === 'collection' ? track.id : null}
onOpen={track ? () => setInfoOpen(true) : undefined}
/>
<div className="min-w-0 flex-1">
{track ? (
<>
<div className="flex min-w-0 items-center gap-2">
<span className="truncate text-[17px] font-semibold tracking-tight text-text" title={track.title ?? ''}>
{track.title ?? 'Untitled'}
</span>
{/* The deck stays usable for listening on a read-only library,
so say which it is rather than leaving the edit controls
looking live. The controls report the reason when pressed. */}
{readOnlyShort(caps) && (
<span
className="shrink-0 rounded-md bg-ink-800 px-1.5 py-0.5 text-[10px] uppercase tracking-wider text-muted"
title={readOnlyNotice(caps) ?? ''}
>
{readOnlyShort(caps)}
</span>
)}
</div>
<div className="truncate text-[13px] text-muted" title={subtitle}>
{subtitle}
</div>
</>
) : (
<div className="text-xs uppercase tracking-wider text-faint">No track loaded</div>
)}
</div>
<div className="well flex h-11 shrink-0 items-stretch rounded-xl">
<div className="flex flex-col justify-center gap-px px-4">
<span className="text-[10px] tracking-[0.08em] text-faint">ELAPSED</span>
<span className="font-mono text-[17px] font-medium">{fmtTime(current, timeDecimals)}</span>
</div>
<span aria-hidden className="my-2 w-px bg-line" />
<div className="flex flex-col justify-center gap-px px-4">
<span className="text-[10px] tracking-[0.08em] text-faint">REMAIN</span>
<span className="font-mono text-[17px] font-medium">−{fmtTime(Math.max(0, duration - current), timeDecimals)}</span>
</div>
<span aria-hidden className="my-2 w-px bg-line" />
<div className="flex flex-col justify-center gap-px px-4">
<span className="text-[10px] tracking-[0.08em] text-faint">KEY</span>
<span
className="font-mono text-[17px] font-semibold"
style={{ color: keyColor(track?.key_wheel ?? null) }}
>
{track?.key || '—'}
</span>
</div>
<span aria-hidden className="my-2 w-px bg-line" />
<BpmReadout
bpm={activeMarker?.bpm ?? null}
editable={canEditGrid}
onSetBpm={(bpm) => commitBpm(bpm, gridAnchorRef.current ?? playheadNow())}
onClose={() => (gridAnchorRef.current = null)}
openRequest={bpmEntryRequest}
/>
</div>
<button
onClick={() => setSnap((v) => !v)}
aria-pressed={snap}
title="Snap cues and loops to the nearest beat"
className={`h-9 shrink-0 rounded-full px-3.5 text-xs font-semibold tracking-[0.08em] ${
snap ? 'is-selected text-text' : 'btn-glass text-muted'
}`}
>
SNAP
</button>
<button
onClick={runAnalyzeGrid}
disabled={!track || gridBusy}
title={canEditGrid
? 'Detect BPM and first beat, then set the grid + hotcue 1 (fix the octave with ÷2 / ×2); measure loudness'
: 'Measure loudness (this track’s grid cannot be edited here)'}
className="btn-glass flex h-9 shrink-0 items-center gap-2 rounded-full px-3.5 text-[13px] font-medium disabled:opacity-40"
>
<Icon name="wave" size={16} />
{gridBusy ? 'Analyzing…' : 'Analyze'}
</button>
<button
onClick={openAutoHotcues}
disabled={!track || !canAutoCue}
title={
canAutoCue
? "Place hotcues on the track's drops, breakdowns and other sections"
: 'Set a beatgrid first'
}
className="btn-glass flex h-9 shrink-0 items-center gap-2 rounded-full px-3.5 text-[13px] font-medium disabled:opacity-40"
>
<Icon name="sparkle" size={16} />
Auto hotcues
</button>
{actions}
</div>
)
}
/**
* The loaded track's sleeve. Until it loads — and for a track with no art — a
* swatch of the ambient colours stands in, so the header never has a hole in it.
* With a track loaded it is a button that opens the track-info popup.
*/
function CoverThumb({ trackId, onOpen }: { trackId: string | null; onOpen?: () => void }) {
const [failed, setFailed] = useState<string | null>(null)
const showArt = trackId != null && failed !== trackId
return (
<button
type="button"
onClick={onOpen}
disabled={!onOpen}
title={onOpen ? 'Track info' : undefined}
aria-label="Track info"
className="relative h-11 w-11 shrink-0 overflow-hidden rounded-xl shadow-[inset_0_1px_0_rgb(255_255_255/0.35),0_8px_24px_-6px_var(--amb-2)] transition-transform enabled:cursor-zoom-in enabled:hover:scale-105 enabled:active:scale-95 disabled:cursor-default"
style={{ background: 'linear-gradient(135deg, var(--amb-1), var(--amb-2) 55%, var(--amb-3))' }}
>
{showArt && (
<img
key={trackId}
src={api.artUrl(trackId)}
alt=""
className="absolute inset-0 h-full w-full object-cover"
onError={() => setFailed(trackId)}
/>
)}
</button>
)
}
@@ -0,0 +1,81 @@
import { api } from '../../api'
import { CapabilitiesContext } from '../../lib/capabilities'
import { AutoCueDialog } from '../AutoCueDialog'
import { ContextMenu } from '../ContextMenu'
import { TrackInfoDialog } from '../TrackInfoDialog'
import type { Deck } from './useDeck'
/** The deck's menus and dialogs (all portalled, so where this renders does not
* matter). Under the deck's OWN capabilities: the track-info dialog reads
* them from context. */
export function DeckOverlays({ deck }: { deck: Deck }) {
const {
caps,
track,
origin,
onError,
onEditTags,
memoryMenu,
setMemoryMenu,
memoryMenuItems,
padMenu,
setPadMenu,
padMenuItems,
infoOpen,
setInfoOpen,
autoOpen,
setAutoOpen,
slotCount,
slotLabel,
existingHotcues,
autoHotcuesDone,
} = deck
return (
<CapabilitiesContext.Provider value={caps}>
{memoryMenu && (
<ContextMenu
key={`m:${memoryMenu.cue.id}:${memoryMenu.x}:${memoryMenu.y}`}
x={memoryMenu.x}
y={memoryMenu.y}
items={memoryMenuItems(memoryMenu.cue)}
onClose={() => setMemoryMenu(null)}
/>
)}
{padMenu && (
<ContextMenu
key={`${padMenu.slot}:${padMenu.x}:${padMenu.y}`}
x={padMenu.x}
y={padMenu.y}
items={padMenuItems(padMenu.slot)}
onClose={() => setPadMenu(null)}
/>
)}
{infoOpen && track && (
<TrackInfoDialog
track={track}
artUrl={origin === 'collection' ? api.artUrl(track.id) : null}
onClose={() => setInfoOpen(false)}
onEditTags={
onEditTags
? () => {
setInfoOpen(false)
onEditTags(track)
}
: undefined
}
/>
)}
{autoOpen && track && (
<AutoCueDialog
track={track}
slotCount={slotCount}
slotLabel={slotLabel}
existing={existingHotcues}
onClose={() => setAutoOpen(false)}
onDone={autoHotcuesDone}
onError={(msg) => onError?.(msg)}
/>
)}
</CapabilitiesContext.Provider>
)
}
+112
View File
@@ -0,0 +1,112 @@
import { MainWaveform } from '../MainWaveform'
import { OverviewWaveform } from '../OverviewWaveform'
import type { Deck } from './useDeck'
/** The main view's zoom and raw/bars toggle — shared by every deck's lane
* (so beats line up between them), owned by the deck area. */
export interface DeckView {
secPerView: number
setSecPerView: (sec: number) => void
rawWave: boolean
setRawWave: (raw: boolean) => void
}
/** The scrolling main waveform, in its well — or what stands in for it: no
* track, a load error, the analysis running. */
export function DeckMainWave({ deck, view, empty = 'Load a track to prep it' }: { deck: Deck; view: DeckView; empty?: string }) {
const {
track,
waveStatus,
showWaves,
cols,
raw,
current,
duration,
cueData,
cuePoint,
grid,
activeMarkerIndex,
gridMode,
activeLoop,
loopActive,
loopRegion,
loopInPoint,
seekManual,
onScratchStart,
onScratchMove,
onScratchEnd,
lanes,
toggleStem,
} = deck
if (!track) {
return (
<div className="well flex min-h-0 flex-1 items-center justify-center rounded-2xl text-xs text-faint">
{empty}
</div>
)
}
if (waveStatus === 'error') {
return (
<div className="well flex min-h-0 flex-1 items-center justify-center rounded-2xl text-xs text-pink">
Could not load audio — file may be missing or an unsupported format.
</div>
)
}
return (
<div className="well relative min-h-0 flex-1 overflow-hidden rounded-2xl">
{showWaves && cols ? (
<MainWaveform
cols={cols}
raw={raw}
rawMode={view.rawWave}
onRawModeChange={view.setRawWave}
currentTime={current}
duration={duration}
cues={cueData?.cues ?? []}
cuePoint={cuePoint}
grid={grid}
activeMarker={activeMarkerIndex}
emphasizeGrid={gridMode}
loop={activeLoop}
idleLoop={!loopActive ? loopRegion : null}
loopIn={loopInPoint}
secPerView={view.secPerView}
onZoomChange={view.setSecPerView}
onSeek={seekManual}
onScratchStart={onScratchStart}
onScratchMove={onScratchMove}
onScratchEnd={onScratchEnd}
lanes={lanes}
onStemToggle={toggleStem}
/>
) : (
<div className="flex h-full items-center justify-center text-xs text-faint">
Analysing waveform…
</div>
)}
</div>
)
}
/** The whole-track overview, framing the main view's range. Nothing without a
* playable track (the main well says why). */
export function DeckOverview({ deck, view }: { deck: Deck; view: DeckView }) {
const { track, waveStatus, showWaves, cols, current, duration, cueData, cuePoint, activeLoop, seekManual } = deck
if (!track || waveStatus === 'error') return null
return (
<div className="well h-7 shrink-0 overflow-hidden rounded-[9px]">
{showWaves && cols && (
<OverviewWaveform
cols={cols}
currentTime={current}
duration={duration}
cues={cueData?.cues ?? []}
cuePoint={cuePoint}
loop={activeLoop}
secPerView={view.secPerView}
onSeek={seekManual}
/>
)}
</div>
)
}
File diff suppressed because it is too large. Load diff