param-config-content.tsx 10.0 KB

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