Added batch auto hot cue and reworked remove context menu

This commit is contained in:
patben8 committed 2026-09-25 11:54:09 +02:00
1 parent 76bf142fb4
commit d18db9566c
21 files changed
+1044 -150

No files matched your search

+76 -37
View File
@@ -3,6 +3,7 @@ import { useQuery, useQueryClient } from '@tanstack/react-query'
import {
api,
type AutoCueEvent,
type AutoCueSlot,
type AutoHotcuesResult,
type CuePoint,
type Track,
@@ -17,6 +18,11 @@ import { formatDuration } from '../lib/format'
* 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.
*
* Batch mode (`batch` instead of `track`) edits the same remembered template but
* does not run it: it hands the slots to `onRun`, which starts a job. There is
* no single track to show existing cues for, so each slot's tick reads "Replace
* existing" and applies to every track in the batch — still never remembered.
*/
export const AUTO_CUE_EVENTS: { id: AutoCueEvent; label: string }[] = [
@@ -64,18 +70,33 @@ function loadTemplate(prefs: Record<string, unknown> | undefined, slots: number)
})
}
interface Props {
track: Track
type Props = {
slotCount: number
slotLabel: (slot: number) => string
/** The track's current hotcues by slot, to mark occupied rows. */
existing: Map<number, CuePoint>
onClose: () => void
onDone: (result: AutoHotcuesResult) => void
onError: (msg: string) => void
}
} & (
| {
track: Track
/** The track's current hotcues by slot, to mark occupied rows. */
existing: Map<number, CuePoint>
onDone: (result: AutoHotcuesResult) => void
batch?: undefined
onRun?: undefined
}
| {
track?: undefined
existing?: undefined
onDone?: undefined
/** How many tracks the template will be applied to. */
batch: { count: number; withoutGrid: number }
/** Start the batch; resolve once it has started. */
onRun: (slots: AutoCueSlot[]) => Promise<void>
}
)
export function AutoCueDialog({ track, slotCount, slotLabel, existing, onClose, onDone, onError }: Props) {
export function AutoCueDialog(props: Props) {
const { slotCount, slotLabel, onClose, onError } = props
const qc = useQueryClient()
const prefs = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs })
const [rows, setRows] = useState<Row[] | null>(null)
@@ -114,14 +135,15 @@ export function AutoCueDialog({ track, slotCount, slotLabel, existing, onClose,
.patchPrefs({ [PREF_KEY]: template })
.then((p) => qc.setQueryData(['prefs'], p))
.catch(() => {})
const slots: AutoCueSlot[] = rows.flatMap((r, slot) =>
r.event ? [{ slot, event: r.event, offset_beats: r.offset, overwrite: replace.has(slot) }] : [],
)
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)
if (props.batch) {
await props.onRun(slots)
} else {
props.onDone(await api.autoCues(props.track.id, slots))
}
onClose()
} catch (e) {
onError((e as Error).message)
@@ -138,7 +160,9 @@ export function AutoCueDialog({ track, slotCount, slotLabel, existing, onClose,
<div className="border-b border-line px-5 py-4">
<div className="text-[15px] font-semibold tracking-tight">Auto Hotcues</div>
<div className="truncate text-xs text-muted">
{track.artist} — {track.title}
{props.batch
? `${props.batch.count} tracks — the same slots on every one`
: `${props.track.artist} — ${props.track.title}`}
</div>
</div>
@@ -147,13 +171,31 @@ export function AutoCueDialog({ track, slotCount, slotLabel, existing, onClose,
<span>Slot</span>
<span>Event</span>
<span className="text-center">Offset (beats)</span>
<span>Existing cue</span>
<span>{props.batch ? 'Occupied slot' : 'Existing cue'}</span>
</div>
{rows === null ? (
<div className="py-6 text-center text-sm text-muted">Loading…</div>
) : (
rows.map((r, slot) => {
const cue = existing.get(slot)
const cue = props.existing?.get(slot)
const tick = (label: string, title?: string) => (
<label className="flex min-w-0 items-center gap-1.5 text-xs text-muted" title={title}>
<input
type="checkbox"
checked={replace.has(slot)}
disabled={!r.event}
onChange={(e) =>
setReplace((s) => {
const n = new Set(s)
if (e.target.checked) n.add(slot)
else n.delete(slot)
return n
})
}
/>
<span className="truncate">{label}</span>
</label>
)
return (
<div key={slot} className="grid grid-cols-[2rem_1fr_9rem_7.5rem] items-center gap-2 py-1">
<span className="flex h-7 w-7 items-center justify-center rounded bg-ink-800 text-xs font-bold text-text">
@@ -197,26 +239,11 @@ export function AutoCueDialog({ track, slotCount, slotLabel, existing, onClose,
+
</button>
</div>
{cue ? (
{props.batch ? (
tick('Replace', 'Replace the cue in this slot on every track that has one')
) : cue ? (
cue.editable ? (
<label className="flex min-w-0 items-center gap-1.5 text-xs text-muted" title={cue.name ?? ''}>
<input
type="checkbox"
checked={replace.has(slot)}
disabled={!r.event}
onChange={(e) =>
setReplace((s) => {
const n = new Set(s)
if (e.target.checked) n.add(slot)
else n.delete(slot)
return n
})
}
/>
<span className="truncate">
Replace {cue.name || formatDuration(cue.start)}
</span>
</label>
tick(`Replace ${cue.name || formatDuration(cue.start)}`, cue.name ?? '')
) : (
<span className="truncate text-xs text-faint" title="This cue belongs to the beatgrid and is never replaced">
Grid cue (kept)
@@ -229,6 +256,12 @@ export function AutoCueDialog({ track, slotCount, slotLabel, existing, onClose,
)
})
)}
{props.batch && props.batch.withoutGrid > 0 && (
<p className="mt-3 text-[11px] leading-relaxed text-gold">
{props.batch.withoutGrid} of these {props.batch.withoutGrid === 1 ? 'has' : 'have'} no
beatgrid — {props.batch.withoutGrid === 1 ? 'it' : 'they'}’ll get one analyzed first.
</p>
)}
<p className="mt-3 text-[11px] leading-relaxed text-faint">
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
@@ -250,7 +283,13 @@ export function AutoCueDialog({ track, slotCount, slotLabel, existing, onClose,
disabled={busy || rows === null || active === 0}
className="rounded-md bg-accent px-4 py-1.5 text-sm font-semibold text-ink-950 hover:brightness-110 disabled:opacity-40"
>
{busy ? 'Analyzing…' : `Analyze & place ${active} cue${active === 1 ? '' : 's'}`}
{busy
? props.batch
? 'Starting…'
: 'Analyzing…'
: props.batch
? `Place ${active} cue${active === 1 ? '' : 's'} on ${props.batch.count} tracks`
: `Analyze & place ${active} cue${active === 1 ? '' : 's'}`}
</button>
</div>
</div>
+79
View File
@@ -0,0 +1,79 @@
import { useEffect, useState, type ReactNode } from 'react'
export interface ConfirmRequest {
title: string
/** What will happen, in a sentence or two. */
body: ReactNode
/** The confirm button's label, e.g. "Remove 3 tracks". */
confirmLabel: string
onConfirm: () => Promise<void> | void
}
interface Props extends ConfirmRequest {
onClose: () => void
}
/** A yes/no confirmation for destructive actions. Enter confirms and Esc
* cancels, so a confirmed Delete key stays a two-keystroke action. */
export function ConfirmDialog({ title, body, confirmLabel, onConfirm, onClose }: Props) {
const [busy, setBusy] = useState(false)
const confirm = async () => {
if (busy) return
setBusy(true)
try {
await onConfirm()
onClose()
} catch {
setBusy(false) // the caller has already reported it; allow a retry
}
}
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (e.key === 'Escape' && !busy) onClose()
// A focused button handles its own Enter — otherwise tabbing to Cancel
// and pressing Enter would run the destructive action.
if (e.key === 'Enter' && !(e.target instanceof HTMLButtonElement)) {
e.preventDefault()
void confirm()
}
}
window.addEventListener('keydown', onKey)
return () => window.removeEventListener('keydown', onKey)
})
return (
<div
aria-modal="true"
className="fixed inset-0 z-40 flex items-center justify-center bg-black/60 p-6"
onClick={() => !busy && onClose()}
>
<div
className="w-full max-w-sm overflow-hidden rounded-xl border border-line bg-ink-900 shadow-2xl"
onClick={(e) => e.stopPropagation()}
>
<div className="border-b border-line px-5 py-4 text-[15px] font-semibold tracking-tight">
{title}
</div>
<div className="space-y-2 px-5 py-4 text-sm text-muted">{body}</div>
<div className="flex items-center justify-end gap-2 border-t border-line px-5 py-3">
<button
onClick={onClose}
disabled={busy}
className="rounded-md px-3 py-1.5 text-sm text-muted hover:bg-ink-800 hover:text-text disabled:opacity-40"
>
Cancel
</button>
<button
onClick={() => void confirm()}
disabled={busy}
className="rounded-md bg-pink px-4 py-1.5 text-sm font-medium text-ink-950 hover:brightness-110 disabled:opacity-50"
>
{confirmLabel}
</button>
</div>
</div>
</div>
)
}