import React, { useCallback, useState } from 'react' import { useTranslation } from 'react-i18next' import produce from 'immer' import { RiEqualizer2Line } from '@remixicon/react' import { TextToAudio } from '@/app/components/base/icons/src/vender/features' import FeatureCard from '@/app/components/base/features/new-feature-panel/feature-card' import Button from '@/app/components/base/button' import VoiceSettings from '@/app/components/base/features/new-feature-panel/text-to-speech/voice-settings' import { useFeatures, useFeaturesStore } from '@/app/components/base/features/hooks' import type { OnFeaturesChange } from '@/app/components/base/features/types' import { FeatureEnum } from '@/app/components/base/features/types' import { languages } from '@/i18n/language' import { TtsAutoPlay } from '@/types/app' type Props = { disabled: boolean onChange?: OnFeaturesChange } const TextToSpeech = ({ disabled, onChange, }: Props) => { const { t } = useTranslation() const textToSpeech = useFeatures(s => s.features.text2speech) // .language .voice .autoPlay const languageInfo = languages.find(i => i.value === textToSpeech?.language) const [modalOpen, setModalOpen] = useState(false) const [isHovering, setIsHovering] = useState(false) const features = useFeatures(s => s.features) const featuresStore = useFeaturesStore() const handleChange = useCallback((type: FeatureEnum, enabled: boolean) => { const { features, setFeatures, } = featuresStore!.getState() const newFeatures = produce(features, (draft) => { draft[type] = { ...draft[type], enabled, } }) setFeatures(newFeatures) if (onChange) onChange() }, [featuresStore, onChange]) return ( } title={t('appDebug.feature.textToSpeech.title')} value={!!features.text2speech?.enabled} onChange={state => handleChange(FeatureEnum.text2speech, state)} onMouseEnter={() => setIsHovering(true)} onMouseLeave={() => setIsHovering(false)} disabled={disabled} > <> {!features.text2speech?.enabled && (
{t('appDebug.feature.textToSpeech.description')}
)} {!!features.text2speech?.enabled && ( <> {!isHovering && !modalOpen && (
{t('appDebug.voice.voiceSettings.language')}
{languageInfo?.name || '-'}
{t('appDebug.voice.voiceSettings.voice')}
{features.text2speech?.voice || t('appDebug.voice.defaultDisplay')}
{t('appDebug.voice.voiceSettings.autoPlay')}
{features.text2speech?.autoPlay === TtsAutoPlay.enabled ? t('appDebug.voice.voiceSettings.autoPlayEnabled') : t('appDebug.voice.voiceSettings.autoPlayDisabled')}
)} {(isHovering || modalOpen) && ( )} )}
) } export default TextToSpeech