import { useEffect, useState } from 'react' import { useQuery, useQueryClient } from '@tanstack/react-query' import { api, type AutoCueEvent, type AutoHotcuesResult, type CuePoint, type Track, } from '../api' import { formatDuration } from '../lib/format' /** * Auto Hotcues: bind each hotcue slot to a structural event plus an offset in * beats, then analyse the track and place them all in one go. * * The template (event + offset per slot) is remembered in userprefs — a DJ cues * every track the same way — but the per-slot "replace" ticks are NOT: whether * to overwrite a hand-placed cue is a decision about THIS track, and a * remembered tick would silently clobber the next track's cues. */ export const AUTO_CUE_EVENTS: { id: AutoCueEvent; label: string }[] = [ { id: 'first_beat', label: 'First beat' }, { id: 'intro_end', label: 'Intro end' }, { id: 'build_1', label: 'Build 1' }, { id: 'drop_1', label: 'Drop 1' }, { id: 'breakdown_1', label: 'Breakdown 1' }, { id: 'build_2', label: 'Build 2' }, { id: 'drop_2', label: 'Drop 2' }, { id: 'breakdown_2', label: 'Breakdown 2' }, { id: 'build_3', label: 'Build 3' }, { id: 'drop_3', label: 'Drop 3' }, { id: 'breakdown_3', label: 'Breakdown 3' }, { id: 'outro', label: 'Outro start' }, { id: 'last_beat', label: 'Last beat' }, ] const EVENT_IDS = new Set(AUTO_CUE_EVENTS.map((e) => e.id)) export const eventLabel = (id: AutoCueEvent) => AUTO_CUE_EVENTS.find((e) => e.id === id)?.label ?? id interface Row { event: AutoCueEvent | null offset: number // beats } const DEFAULT_TEMPLATE: AutoCueEvent[] = [ 'first_beat', 'build_1', 'drop_1', 'breakdown_1', 'build_2', 'drop_2', 'outro', 'last_beat', ] const PREF_KEY = 'autoCueTemplate' const MAX_OFFSET = 256 /** The remembered template, padded/truncated to this library's bank size. * Anything malformed in prefs falls back to the default for that slot. */ function loadTemplate(prefs: Record | undefined, slots: number): Row[] { const saved = Array.isArray(prefs?.[PREF_KEY]) ? (prefs![PREF_KEY] as unknown[]) : null return Array.from({ length: slots }, (_, i) => { const r = saved?.[i] as { event?: unknown; offset_beats?: unknown } | undefined if (saved && r && typeof r === 'object') { const event = r.event === null ? null : EVENT_IDS.has(String(r.event)) ? (r.event as AutoCueEvent) : null const off = typeof r.offset_beats === 'number' && isFinite(r.offset_beats) ? Math.round(r.offset_beats) : 0 return { event, offset: Math.max(-MAX_OFFSET, Math.min(MAX_OFFSET, off)) } } return { event: DEFAULT_TEMPLATE[i] ?? null, offset: 0 } }) } interface Props { track: Track slotCount: number slotLabel: (slot: number) => string /** The track's current hotcues by slot, to mark occupied rows. */ existing: Map onClose: () => void onDone: (result: AutoHotcuesResult) => void onError: (msg: string) => void } export function AutoCueDialog({ track, slotCount, slotLabel, existing, onClose, onDone, onError }: Props) { const qc = useQueryClient() const prefs = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs }) const [rows, setRows] = useState(null) const [replace, setReplace] = useState>(new Set()) const [busy, setBusy] = useState(false) // Hydrate once from prefs (the query is normally already cached by PrepStrip). useEffect(() => { if (rows === null && (prefs.data || prefs.isError)) setRows(loadTemplate(prefs.data, slotCount)) }, [prefs.data, prefs.isError, rows, slotCount]) useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape' && !busy) onClose() } window.addEventListener('keydown', onKey) return () => window.removeEventListener('keydown', onKey) }, [busy, onClose]) const setRow = (i: number, patch: Partial) => setRows((rs) => rs && rs.map((r, j) => (j === i ? { ...r, ...patch } : r))) const nudge = (i: number, by: number) => setRows((rs) => rs && rs.map((r, j) => (j === i ? { ...r, offset: Math.max(-MAX_OFFSET, Math.min(MAX_OFFSET, r.offset + by)) } : r)), ) const active = (rows ?? []).filter((r) => r.event !== null).length const confirm = async () => { if (!rows || busy || active === 0) return setBusy(true) const template = rows.map((r) => ({ event: r.event, offset_beats: r.offset })) // Remember the template whatever the analysis finds: it describes how the // user cues, not this track. api .patchPrefs({ [PREF_KEY]: template }) .then((p) => qc.setQueryData(['prefs'], p)) .catch(() => {}) try { const result = await api.autoCues( track.id, rows.flatMap((r, slot) => r.event ? [{ slot, event: r.event, offset_beats: r.offset, overwrite: replace.has(slot) }] : [], ), ) onDone(result) onClose() } catch (e) { onError((e as Error).message) setBusy(false) } } return (
!busy && onClose()}>
e.stopPropagation()} >
Auto Hotcues
{track.artist} — {track.title}
Slot Event Offset (beats) Existing cue
{rows === null ? (
Loading…
) : ( rows.map((r, slot) => { const cue = existing.get(slot) return (
{slotLabel(slot)}
{ const v = Math.round(Number(e.target.value)) if (isFinite(v)) setRow(slot, { offset: Math.max(-MAX_OFFSET, Math.min(MAX_OFFSET, v)) }) }} className="h-7 w-12 rounded-md border border-line bg-ink-850 px-1 text-center text-sm tabular-nums text-text" />
{cue ? ( cue.editable ? ( ) : ( Grid cue (kept) ) ) : ( Empty )}
) }) )}

Events are found on the beatgrid, counting bar 1 from its first marker — if drops land a bar or two off, move the grid marker onto the first downbeat. Occupied slots are kept unless you tick Replace. If two slots land on the same beat, only the lower one gets the cue. Your slot setup is remembered for the next track.

) }