index.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use client'
  2. import useSWR from 'swr'
  3. import React from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { usePathname } from 'next/navigation'
  6. import { useFeatures } from '../../hooks'
  7. import type { OnFeaturesChange } from '../../types'
  8. import ParamsConfig from './params-config'
  9. import { Speaker } from '@/app/components/base/icons/src/vender/solid/mediaAndDevices'
  10. import { languages } from '@/i18n/language'
  11. import { fetchAppVoices } from '@/service/apps'
  12. import AudioBtn from '@/app/components/base/audio-btn'
  13. type TextToSpeechProps = {
  14. onChange?: OnFeaturesChange
  15. disabled?: boolean
  16. }
  17. const TextToSpeech = ({
  18. onChange,
  19. disabled,
  20. }: TextToSpeechProps) => {
  21. const { t } = useTranslation()
  22. const textToSpeech = useFeatures(s => s.features.text2speech)
  23. const pathname = usePathname()
  24. const matched = pathname.match(/\/app\/([^/]+)/)
  25. const appId = (matched?.length && matched[1]) ? matched[1] : ''
  26. const language = textToSpeech?.language
  27. const languageInfo = languages.find(i => i.value === textToSpeech?.language)
  28. const voiceItems = useSWR({ appId, language }, fetchAppVoices).data
  29. const voiceItem = voiceItems?.find(item => item.value === textToSpeech?.voice)
  30. return (
  31. <div className='flex items-center px-3 h-12 bg-gray-50 rounded-xl overflow-hidden'>
  32. <div className='shrink-0 flex items-center justify-center mr-1 w-6 h-6'>
  33. <Speaker className='w-4 h-4 text-[#7839EE]' />
  34. </div>
  35. <div className='shrink-0 mr-2 whitespace-nowrap text-sm text-gray-800 font-semibold'>
  36. {t('appDebug.feature.textToSpeech.title')}
  37. </div>
  38. <div
  39. className='grow '>
  40. </div>
  41. <div className='shrink-0 text-xs text-gray-500 inline-flex items-center gap-2'>
  42. {languageInfo && (`${languageInfo?.name} - `)}{voiceItem?.name ?? t('appDebug.voice.defaultDisplay')}
  43. { languageInfo?.example && (
  44. <AudioBtn
  45. value={languageInfo?.example}
  46. voice={voiceItem?.value}
  47. noCache={false}
  48. isAudition={true}
  49. />
  50. )}
  51. </div>
  52. <div className='shrink-0 flex items-center'>
  53. <ParamsConfig onChange={onChange} disabled={disabled} />
  54. </div>
  55. </div>
  56. )
  57. }
  58. export default React.memo(TextToSpeech)