param-config-content.tsx 8.8 KB

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