UNPKG

laif-ds

Version:

Design System di Laif con componenti React basati su principi di Atomic Design

56 lines (55 loc) 1.44 kB
"use client"; import { useState as c, useRef as R, useEffect as m } from "react"; import { recordAudio as d } from "../lib/audio-utils.js"; function w({ transcribeAudio: s, onTranscriptionComplete: a }) { const [g, i] = c(!1), [h, p] = c(!!s), [S, o] = c(!1), [v, f] = c(!1), [t, n] = c(null), u = R(null); m(() => { (async () => { const r = !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia); p(r && !!s); })(); }, [s]); const l = async () => { o(!1), f(!0); try { d.stop(); const e = await u.current; if (s) { const r = await s(e); a == null || a(r); } } catch (e) { console.error("Error transcribing audio:", e); } finally { f(!1), i(!1), t && (t.getTracks().forEach((e) => e.stop()), n(null)), u.current = null; } }; return { isListening: g, isSpeechSupported: h, isRecording: S, isTranscribing: v, audioStream: t, toggleListening: async () => { if (g) await l(); else try { i(!0), o(!0); const e = await navigator.mediaDevices.getUserMedia({ audio: !0 }); n(e), u.current = d(e); } catch (e) { console.error("Error recording audio:", e), i(!1), o(!1), t && (t.getTracks().forEach((r) => r.stop()), n(null)); } }, stopRecording: l }; } export { w as useAudioRecording };