diff --git a/src/components/TTSControls.tsx b/src/components/TTSControls.tsx new file mode 100644 index 00000000..99c677b2 --- /dev/null +++ b/src/components/TTSControls.tsx @@ -0,0 +1,77 @@ +import React from 'react' +import { useTextToSpeech } from '../hooks/useTextToSpeech' +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' +import { faPlay, faPause, faStop } from '@fortawesome/free-solid-svg-icons' + +interface Props { + text: string + defaultLang?: string + className?: string +} + +const TTSControls: React.FC = ({ text, defaultLang, className }) => { + const { + supported, speaking, paused, + speak, pause, resume, stop, + rate, setRate + } = useTextToSpeech({ defaultLang }) + + const canPlay = supported && text?.trim().length > 0 + + const handlePlayPause = () => { + if (!canPlay) return + if (!speaking) { + speak(text, defaultLang) + } else if (paused) { + resume() + } else { + pause() + } + } + + const playLabel = !speaking ? 'Listen' : (paused ? 'Resume' : 'Pause') + + if (!supported) return null + + return ( +
+ + + +
+ ) +} + +export default TTSControls +