react-blabla
Version:
A React text-to-speech component
27 lines • 1.31 kB
JavaScript
import { useState, useEffect } from 'react';
export function useGetVoices(params) {
var _a = useState([]), allVoices = _a[0], setAllVoices = _a[1];
var _b = useState([]), voicesByParam = _b[0], setVoicesByParam = _b[1];
window.speechSynthesis.onvoiceschanged = function () {
var all = window.speechSynthesis.getVoices();
setAllVoices(all);
};
useEffect(function () {
if (allVoices.length) {
if (params === null || params === void 0 ? void 0 : params.name) {
var voiceByName = allVoices.find(function (voice) { return voice.name === params.name; });
if (!voiceByName)
console.error('Incorrect name of voice!');
setVoicesByParam(voiceByName || allVoices[0]);
}
if (params === null || params === void 0 ? void 0 : params.language) {
var voicesByLanguage = allVoices.filter(function (voice) { return voice.lang === params.language; });
if (!voicesByLanguage.length)
console.error('Incorrect language code!');
setVoicesByParam(voicesByLanguage.length ? voicesByLanguage : allVoices[0]);
}
}
}, [allVoices]);
return voicesByParam;
}
//# sourceMappingURL=useGetVoices.js.map