param-config-content.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. 'use client'
  2. import useSWR from 'swr'
  3. import produce from 'immer'
  4. import React, { Fragment } from 'react'
  5. import { usePathname } from 'next/navigation'
  6. import { useTranslation } from 'react-i18next'
  7. import { RiCloseLine } from '@remixicon/react'
  8. import { Listbox, Transition } from '@headlessui/react'
  9. import { CheckIcon, ChevronDownIcon } from '@heroicons/react/20/solid'
  10. import { useFeatures, useFeaturesStore } from '@/app/components/base/features/hooks'
  11. import type { Item } from '@/app/components/base/select'
  12. import { fetchAppVoices } from '@/service/apps'
  13. import Tooltip from '@/app/components/base/tooltip'
  14. import Switch from '@/app/components/base/switch'
  15. import AudioBtn from '@/app/components/base/audio-btn'
  16. import { languages } from '@/i18n/language'
  17. import { TtsAutoPlay } from '@/types/app'
  18. import type { OnFeaturesChange } from '@/app/components/base/features/types'
  19. import classNames from '@/utils/classnames'
  20. type VoiceParamConfigProps = {
  21. onClose: () => void
  22. onChange?: OnFeaturesChange
  23. }
  24. const VoiceParamConfig = ({
  25. onClose,
  26. onChange,
  27. }: VoiceParamConfigProps) => {
  28. const { t } = useTranslation()
  29. const pathname = usePathname()
  30. const matched = pathname.match(/\/app\/([^/]+)/)
  31. const appId = (matched?.length && matched[1]) ? matched[1] : ''
  32. const text2speech = useFeatures(state => state.features.text2speech)
  33. const featuresStore = useFeaturesStore()
  34. let languageItem = languages.find(item => item.value === text2speech?.language)
  35. if (languages && !languageItem)
  36. languageItem = languages[0]
  37. const localLanguagePlaceholder = languageItem?.name || t('common.placeholder.select')
  38. const language = languageItem?.value
  39. const voiceItems = useSWR({ appId, language }, fetchAppVoices).data
  40. let voiceItem = voiceItems?.find(item => item.value === text2speech?.voice)
  41. if (voiceItems && !voiceItem)
  42. voiceItem = voiceItems[0]
  43. const localVoicePlaceholder = voiceItem?.name || t('common.placeholder.select')
  44. const handleChange = (value: Record<string, string>) => {
  45. const {
  46. features,
  47. setFeatures,
  48. } = featuresStore!.getState()
  49. const newFeatures = produce(features, (draft) => {
  50. draft.text2speech = {
  51. ...draft.text2speech,
  52. ...value,
  53. }
  54. })
  55. setFeatures(newFeatures)
  56. if (onChange)
  57. onChange()
  58. }
  59. return (
  60. <>
  61. <div className='mb-4 flex items-center justify-between'>
  62. <div className='text-text-primary system-xl-semibold'>{t('appDebug.voice.voiceSettings.title')}</div>
  63. <div className='p-1 cursor-pointer' onClick={onClose}><RiCloseLine className='w-4 h-4 text-text-tertiary'/></div>
  64. </div>
  65. <div className='mb-3'>
  66. <div className='mb-1 py-1 flex items-center text-text-secondary system-sm-semibold'>
  67. {t('appDebug.voice.voiceSettings.language')}
  68. <Tooltip
  69. popupContent={
  70. <div className='w-[180px]'>
  71. {t('appDebug.voice.voiceSettings.resolutionTooltip').split('\n').map(item => (
  72. <div key={item}>{item}
  73. </div>
  74. ))}
  75. </div>
  76. }
  77. />
  78. </div>
  79. <Listbox
  80. value={languageItem}
  81. onChange={(value: Item) => {
  82. handleChange({
  83. language: String(value.value),
  84. })
  85. }}
  86. >
  87. <div className='relative h-8'>
  88. <Listbox.Button
  89. className={'w-full h-full rounded-lg border-0 bg-gray-100 py-1.5 pl-3 pr-10 sm:text-sm sm:leading-6 focus-visible:outline-none focus-visible:bg-gray-200 group-hover:bg-gray-200 cursor-pointer'}>
  90. <span className={classNames('block truncate text-left', !languageItem?.name && 'text-gray-400')}>
  91. {languageItem?.name ? t(`common.voice.language.${languageItem?.value.replace('-', '')}`) : localLanguagePlaceholder}
  92. </span>
  93. <span className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2">
  94. <ChevronDownIcon
  95. className="h-5 w-5 text-gray-400"
  96. aria-hidden="true"
  97. />
  98. </span>
  99. </Listbox.Button>
  100. <Transition
  101. as={Fragment}
  102. leave="transition ease-in duration-100"
  103. leaveFrom="opacity-100"
  104. leaveTo="opacity-0"
  105. >
  106. <Listbox.Options
  107. className="absolute z-10 mt-1 px-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg border-gray-200 border-[0.5px] focus:outline-none sm:text-sm">
  108. {languages.map((item: Item) => (
  109. <Listbox.Option
  110. key={item.value}
  111. className={({ active }) =>
  112. `relative cursor-pointer select-none py-2 pl-3 pr-9 rounded-lg hover:bg-gray-100 text-gray-700 ${active ? 'bg-gray-100' : ''
  113. }`
  114. }
  115. value={item}
  116. disabled={false}
  117. >
  118. {({ /* active, */ selected }) => (
  119. <>
  120. <span
  121. className={classNames('block', selected && 'font-normal')}>{t(`common.voice.language.${(item.value).toString().replace('-', '')}`)}</span>
  122. {(selected || item.value === text2speech?.language) && (
  123. <span
  124. className={classNames(
  125. 'absolute inset-y-0 right-0 flex items-center pr-4 text-gray-700',
  126. )}
  127. >
  128. <CheckIcon className="h-5 w-5" aria-hidden="true"/>
  129. </span>
  130. )}
  131. </>
  132. )}
  133. </Listbox.Option>
  134. ))}
  135. </Listbox.Options>
  136. </Transition>
  137. </div>
  138. </Listbox>
  139. </div>
  140. <div className='mb-3'>
  141. <div className='mb-1 py-1 text-text-secondary system-sm-semibold'>
  142. {t('appDebug.voice.voiceSettings.voice')}
  143. </div>
  144. <div className='flex items-center gap-1'>
  145. <Listbox
  146. value={voiceItem ?? {}}
  147. disabled={!languageItem}
  148. onChange={(value: Item) => {
  149. handleChange({
  150. voice: String(value.value),
  151. })
  152. }}
  153. >
  154. <div className={'grow relative h-8'}>
  155. <Listbox.Button
  156. className={'w-full h-full rounded-lg border-0 bg-gray-100 py-1.5 pl-3 pr-10 sm:text-sm sm:leading-6 focus-visible:outline-none focus-visible:bg-gray-200 group-hover:bg-gray-200 cursor-pointer'}>
  157. <span
  158. className={classNames('block truncate text-left', !voiceItem?.name && 'text-gray-400')}>{voiceItem?.name ?? localVoicePlaceholder}</span>
  159. <span className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2">
  160. <ChevronDownIcon
  161. className="h-5 w-5 text-gray-400"
  162. aria-hidden="true"
  163. />
  164. </span>
  165. </Listbox.Button>
  166. <Transition
  167. as={Fragment}
  168. leave="transition ease-in duration-100"
  169. leaveFrom="opacity-100"
  170. leaveTo="opacity-0"
  171. >
  172. <Listbox.Options
  173. className="absolute z-10 mt-1 px-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg border-gray-200 border-[0.5px] focus:outline-none sm:text-sm">
  174. {voiceItems?.map((item: Item) => (
  175. <Listbox.Option
  176. key={item.value}
  177. className={({ active }) =>
  178. `relative cursor-pointer select-none py-2 pl-3 pr-9 rounded-lg hover:bg-gray-100 text-gray-700 ${active ? 'bg-gray-100' : ''
  179. }`
  180. }
  181. value={item}
  182. disabled={false}
  183. >
  184. {({ /* active, */ selected }) => (
  185. <>
  186. <span className={classNames('block', selected && 'font-normal')}>{item.name}</span>
  187. {(selected || item.value === text2speech?.voice) && (
  188. <span
  189. className={classNames(
  190. 'absolute inset-y-0 right-0 flex items-center pr-4 text-gray-700',
  191. )}
  192. >
  193. <CheckIcon className="h-5 w-5" aria-hidden="true"/>
  194. </span>
  195. )}
  196. </>
  197. )}
  198. </Listbox.Option>
  199. ))}
  200. </Listbox.Options>
  201. </Transition>
  202. </div>
  203. </Listbox>
  204. {languageItem?.example && (
  205. <div className='shrink-0 h-8 p-1 rounded-lg bg-components-button-tertiary-bg'>
  206. <AudioBtn
  207. value={languageItem?.example}
  208. isAudition
  209. voice={text2speech?.voice}
  210. noCache
  211. />
  212. </div>
  213. )}
  214. </div>
  215. </div>
  216. <div>
  217. <div className='mb-1 py-1 text-text-secondary system-sm-semibold'>
  218. {t('appDebug.voice.voiceSettings.autoPlay')}
  219. </div>
  220. <Switch className='shrink-0'
  221. defaultValue={text2speech?.autoPlay === TtsAutoPlay.enabled}
  222. onChange={(value: boolean) => {
  223. handleChange({
  224. autoPlay: value ? TtsAutoPlay.enabled : TtsAutoPlay.disabled,
  225. })
  226. }}
  227. />
  228. </div>
  229. </>
  230. )
  231. }
  232. export default React.memo(VoiceParamConfig)