File size: 1,459 Bytes
aa2e6af | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import { useEffect } from 'react';
import { useRecoilState } from 'recoil';
import { useToastContext } from '~/Providers';
import store from '~/store';
import SpeechRecognition, { useSpeechRecognition } from 'react-speech-recognition';
const useSpeechToTextBrowser = () => {
const { showToast } = useToastContext();
const [endpointSTT] = useRecoilState<string>(store.endpointSTT);
const { transcript, listening, resetTranscript, browserSupportsSpeechRecognition } =
useSpeechRecognition();
const toggleListening = () => {
if (browserSupportsSpeechRecognition) {
if (listening) {
SpeechRecognition.stopListening();
} else {
SpeechRecognition.startListening();
}
} else {
showToast({
message: 'Browser does not support SpeechRecognition',
status: 'error',
});
}
};
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.shiftKey && e.altKey && e.code === 'KeyL' && endpointSTT === 'browser') {
toggleListening();
}
};
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, []);
return {
isListening: listening,
isLoading: false,
text: transcript,
startRecording: toggleListening,
stopRecording: () => {
SpeechRecognition.stopListening();
resetTranscript();
},
};
};
export default useSpeechToTextBrowser;
|