/* eslint-disable jsx-a11y/media-has-caption */
import { useEffect } from 'react';
import { useRecoilValue } from 'recoil';
import type { TMessageAudio } from '~/common';
import { VolumeIcon, VolumeMuteIcon, Spinner } from '@librechat/client';
import { useLocalize, useTTSBrowser, useTTSExternal } from '~/hooks';
import { logger } from '~/utils';
import store from '~/store';
export function BrowserTTS({
isLast,
index,
messageId,
content,
className,
renderButton,
}: TMessageAudio) {
const localize = useLocalize();
const playbackRate = useRecoilValue(store.playbackRate);
const { toggleSpeech, isSpeaking, isLoading, audioRef } = useTTSBrowser({
isLast,
index,
messageId,
content,
});
const renderIcon = () => {
if (isLoading === true) {
return ;
}
if (isSpeaking === true) {
return ;
}
return ;
};
useEffect(() => {
const messageAudio = document.getElementById(`audio-${messageId}`) as HTMLAudioElement | null;
if (!messageAudio) {
return;
}
if (playbackRate != null && playbackRate > 0 && messageAudio.playbackRate !== playbackRate) {
messageAudio.playbackRate = playbackRate;
}
}, [audioRef, isSpeaking, playbackRate, messageId]);
logger.log(
'MessageAudio: audioRef.current?.src, audioRef.current',
audioRef.current?.src,
audioRef.current,
);
const handleClick = () => {
if (audioRef.current) {
audioRef.current.muted = false;
}
toggleSpeech();
};
const title = isSpeaking === true ? localize('com_ui_stop') : localize('com_ui_read_aloud');
return (
<>
{renderButton ? (
renderButton({
onClick: handleClick,
title: title,
icon: renderIcon(),
isActive: isSpeaking,
className,
})
) : (
)}