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 } 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 (
!busy && onClose()} >
e.stopPropagation()} >
{title}
{body}
) }