index.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React, { useCallback, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import produce from 'immer'
  4. import { RiEqualizer2Line } from '@remixicon/react'
  5. import { TextToAudio } from '@/app/components/base/icons/src/vender/features'
  6. import FeatureCard from '@/app/components/base/features/new-feature-panel/feature-card'
  7. import Button from '@/app/components/base/button'
  8. import VoiceSettings from '@/app/components/base/features/new-feature-panel/text-to-speech/voice-settings'
  9. import { useFeatures, useFeaturesStore } from '@/app/components/base/features/hooks'
  10. import type { OnFeaturesChange } from '@/app/components/base/features/types'
  11. import { FeatureEnum } from '@/app/components/base/features/types'
  12. import { languages } from '@/i18n/language'
  13. import { TtsAutoPlay } from '@/types/app'
  14. type Props = {
  15. disabled: boolean
  16. onChange?: OnFeaturesChange
  17. }
  18. const TextToSpeech = ({
  19. disabled,
  20. onChange,
  21. }: Props) => {
  22. const { t } = useTranslation()
  23. const textToSpeech = useFeatures(s => s.features.text2speech) // .language .voice .autoPlay
  24. const languageInfo = languages.find(i => i.value === textToSpeech?.language)
  25. const [modalOpen, setModalOpen] = useState(false)
  26. const [isHovering, setIsHovering] = useState(false)
  27. const features = useFeatures(s => s.features)
  28. const featuresStore = useFeaturesStore()
  29. const handleChange = useCallback((type: FeatureEnum, enabled: boolean) => {
  30. const {
  31. features,
  32. setFeatures,
  33. } = featuresStore!.getState()
  34. const newFeatures = produce(features, (draft) => {
  35. draft[type] = {
  36. ...draft[type],
  37. enabled,
  38. }
  39. })
  40. setFeatures(newFeatures)
  41. if (onChange)
  42. onChange()
  43. }, [featuresStore, onChange])
  44. return (
  45. <FeatureCard
  46. icon={
  47. <div className='shrink-0 p-1 rounded-lg border-[0.5px] border-divider-subtle shadow-xs bg-util-colors-violet-violet-600'>
  48. <TextToAudio className='w-4 h-4 text-text-primary-on-surface' />
  49. </div>
  50. }
  51. title={t('appDebug.feature.textToSpeech.title')}
  52. value={!!features.text2speech?.enabled}
  53. onChange={state => handleChange(FeatureEnum.text2speech, state)}
  54. onMouseEnter={() => setIsHovering(true)}
  55. onMouseLeave={() => setIsHovering(false)}
  56. disabled={disabled}
  57. >
  58. <>
  59. {!features.text2speech?.enabled && (
  60. <div className='min-h-8 text-text-tertiary system-xs-regular line-clamp-2'>{t('appDebug.feature.textToSpeech.description')}</div>
  61. )}
  62. {!!features.text2speech?.enabled && (
  63. <>
  64. {!isHovering && !modalOpen && (
  65. <div className='pt-0.5 flex items-center gap-4'>
  66. <div className=''>
  67. <div className='mb-0.5 text-text-tertiary system-2xs-medium-uppercase'>{t('appDebug.voice.voiceSettings.language')}</div>
  68. <div className='text-text-secondary system-xs-regular'>{languageInfo?.name || '-'}</div>
  69. </div>
  70. <div className='w-px h-[27px] bg-divider-subtle rotate-12'></div>
  71. <div className=''>
  72. <div className='mb-0.5 text-text-tertiary system-2xs-medium-uppercase'>{t('appDebug.voice.voiceSettings.voice')}</div>
  73. <div className='text-text-secondary system-xs-regular'>{features.text2speech?.voice || t('appDebug.voice.defaultDisplay')}</div>
  74. </div>
  75. <div className='w-px h-[27px] bg-divider-subtle rotate-12'></div>
  76. <div className=''>
  77. <div className='mb-0.5 text-text-tertiary system-2xs-medium-uppercase'>{t('appDebug.voice.voiceSettings.autoPlay')}</div>
  78. <div className='text-text-secondary system-xs-regular'>{features.text2speech?.autoPlay === TtsAutoPlay.enabled ? t('appDebug.voice.voiceSettings.autoPlayEnabled') : t('appDebug.voice.voiceSettings.autoPlayDisabled')}</div>
  79. </div>
  80. </div>
  81. )}
  82. {(isHovering || modalOpen) && (
  83. <VoiceSettings open={modalOpen && !disabled} onOpen={setModalOpen} onChange={onChange}>
  84. <Button className='w-full' disabled={disabled}>
  85. <RiEqualizer2Line className='mr-1 w-4 h-4' />
  86. {t('appDebug.voice.voiceSettings.title')}
  87. </Button>
  88. </VoiceSettings>
  89. )}
  90. </>
  91. )}
  92. </>
  93. </FeatureCard>
  94. )
  95. }
  96. export default TextToSpeech