import { useState } from "react"; export type Beeper = (callback?: () => void) => void; declare global { interface Window { webkitAudioContext: { new (): AudioContext }; } } export function createBeeper(duration = 200, type: OscillatorType = "sine"): Beeper { const ctxClass = window.AudioContext || window.webkitAudioContext; const ctx = new ctxClass(); return (finishedCallback?: () => void) => { duration = +duration; const osc = ctx.createOscillator(); osc.type = type; osc.connect(ctx.destination); osc.start(); setTimeout(() => { osc.stop(); finishedCallback?.(); }, duration); }; } export function useBeeper(duration = 200, type: OscillatorType = "sine"): Beeper { const [beeper] = useState(() => createBeeper(duration, type)); return beeper; }