diff --git a/src/app/(admin)/ticket-toasts.tsx b/src/app/(admin)/ticket-toasts.tsx index d40fa76..55a113e 100644 --- a/src/app/(admin)/ticket-toasts.tsx +++ b/src/app/(admin)/ticket-toasts.tsx @@ -14,12 +14,37 @@ interface Toast { href: string; } +// A single, reused AudioContext — created fresh per chime, an AudioContext +// starts "suspended" unless made/resumed inside a real user gesture (click, +// keypress, ...), and a chime triggered by an incoming SSE message is a +// network callback, not a gesture. It fails *silently*: start()/stop() don't +// throw, nothing plays. Reusing one context that's unlocked by the page's +// first real click/keydown avoids that — once resumed, it stays running for +// the rest of the session. +let sharedAudioContext: AudioContext | null = null; + +function getAudioContext(): AudioContext | null { + if (typeof window === "undefined") return null; + const AudioCtor = window.AudioContext ?? (window as unknown as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext; + if (!AudioCtor) return null; + if (!sharedAudioContext) sharedAudioContext = new AudioCtor(); + return sharedAudioContext; +} + +if (typeof window !== "undefined") { + const unlockAudio = () => { + getAudioContext()?.resume(); + }; + window.addEventListener("pointerdown", unlockAudio, { once: true }); + window.addEventListener("keydown", unlockAudio, { once: true }); +} + /** A short two-tone chime via the Web Audio API — no audio asset needed. */ function playChime() { + const ctx = getAudioContext(); + if (!ctx) return; try { - const AudioCtor = window.AudioContext ?? (window as unknown as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext; - if (!AudioCtor) return; - const ctx = new AudioCtor(); + if (ctx.state === "suspended") ctx.resume(); const now = ctx.currentTime; for (const [freq, start] of [[880, 0], [1175, 0.12]] as const) { const osc = ctx.createOscillator(); @@ -34,7 +59,6 @@ function playChime() { osc.start(now + start); osc.stop(now + start + 0.25); } - setTimeout(() => ctx.close(), 500); } catch { // audio isn't critical to the notification — a silent toast still works }