Created
December 27, 2022 19:46
-
-
Save AliBahaari/c85685099158f480b80345a56f843bf9 to your computer and use it in GitHub Desktop.
A hook for speech-to-text.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { isServer } from '@hasty-bazar-commerce/utils' | |
| import { useEffect, useState } from 'react' | |
| export const useSpeechRecognition = ( | |
| lang?: string, | |
| continues?: boolean, | |
| interimResults?: boolean, | |
| ) => { | |
| const [speechRecognitionState, setSpeechRecognitionState] = useState<boolean>() | |
| useEffect(() => { | |
| const checkSpeechRecognition = | |
| global?.window && | |
| !isServer() && | |
| (window?.speechRecognition || window?.webkitSpeechRecognition) | |
| if (checkSpeechRecognition) { | |
| setSpeechRecognitionState(true) | |
| } else { | |
| setSpeechRecognitionState(false) | |
| } | |
| }, []) | |
| let speechRecognitionConstructor: any = {} | |
| if (speechRecognitionState) { | |
| let SpeechRecognition = window.speechRecognition || window.webkitSpeechRecognition | |
| speechRecognitionConstructor = new SpeechRecognition() | |
| speechRecognitionConstructor.lang = lang ?? 'fa-IR' | |
| speechRecognitionConstructor.continuous = continues ?? false | |
| speechRecognitionConstructor.interimResults = interimResults ?? false | |
| } | |
| return [speechRecognitionConstructor, speechRecognitionState] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment