parameter-item.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import type { FC } from 'react'
  2. import { useEffect, useRef, useState } from 'react'
  3. import type { ModelParameterRule } from '../declarations'
  4. import { useLanguage } from '../hooks'
  5. import { isNullOrUndefined } from '../utils'
  6. import cn from '@/utils/classnames'
  7. import Switch from '@/app/components/base/switch'
  8. import Tooltip from '@/app/components/base/tooltip'
  9. import Slider from '@/app/components/base/slider'
  10. import Radio from '@/app/components/base/radio'
  11. import { SimpleSelect } from '@/app/components/base/select'
  12. import TagInput from '@/app/components/base/tag-input'
  13. export type ParameterValue = number | string | string[] | boolean | undefined
  14. type ParameterItemProps = {
  15. parameterRule: ModelParameterRule
  16. value?: ParameterValue
  17. onChange?: (value: ParameterValue) => void
  18. className?: string
  19. onSwitch?: (checked: boolean, assignValue: ParameterValue) => void
  20. isInWorkflow?: boolean
  21. }
  22. const ParameterItem: FC<ParameterItemProps> = ({
  23. parameterRule,
  24. value,
  25. onChange,
  26. className,
  27. onSwitch,
  28. isInWorkflow,
  29. }) => {
  30. const language = useLanguage()
  31. const [localValue, setLocalValue] = useState(value)
  32. const numberInputRef = useRef<HTMLInputElement>(null)
  33. const getDefaultValue = () => {
  34. let defaultValue: ParameterValue
  35. if (parameterRule.type === 'int' || parameterRule.type === 'float')
  36. defaultValue = isNullOrUndefined(parameterRule.default) ? (parameterRule.min || 0) : parameterRule.default
  37. else if (parameterRule.type === 'string' || parameterRule.type === 'text')
  38. defaultValue = parameterRule.options?.length ? (parameterRule.default || '') : (parameterRule.default || '')
  39. else if (parameterRule.type === 'boolean')
  40. defaultValue = !isNullOrUndefined(parameterRule.default) ? parameterRule.default : false
  41. else if (parameterRule.type === 'tag')
  42. defaultValue = !isNullOrUndefined(parameterRule.default) ? parameterRule.default : []
  43. return defaultValue
  44. }
  45. const renderValue = value ?? localValue ?? getDefaultValue()
  46. const handleInputChange = (newValue: ParameterValue) => {
  47. setLocalValue(newValue)
  48. if (onChange && (parameterRule.name === 'stop' || !isNullOrUndefined(value) || parameterRule.required))
  49. onChange(newValue)
  50. }
  51. const handleNumberInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  52. let num = +e.target.value
  53. if (!isNullOrUndefined(parameterRule.max) && num > parameterRule.max!) {
  54. num = parameterRule.max as number
  55. numberInputRef.current!.value = `${num}`
  56. }
  57. if (!isNullOrUndefined(parameterRule.min) && num < parameterRule.min!)
  58. num = parameterRule.min as number
  59. handleInputChange(num)
  60. }
  61. const handleNumberInputBlur = () => {
  62. if (numberInputRef.current)
  63. numberInputRef.current.value = renderValue as string
  64. }
  65. const handleSlideChange = (num: number) => {
  66. if (!isNullOrUndefined(parameterRule.max) && num > parameterRule.max!) {
  67. handleInputChange(parameterRule.max)
  68. numberInputRef.current!.value = `${parameterRule.max}`
  69. return
  70. }
  71. if (!isNullOrUndefined(parameterRule.min) && num < parameterRule.min!) {
  72. handleInputChange(parameterRule.min)
  73. numberInputRef.current!.value = `${parameterRule.min}`
  74. return
  75. }
  76. handleInputChange(num)
  77. numberInputRef.current!.value = `${num}`
  78. }
  79. const handleRadioChange = (v: number) => {
  80. handleInputChange(v === 1)
  81. }
  82. const handleStringInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
  83. handleInputChange(e.target.value)
  84. }
  85. const handleSelect = (option: { value: string | number; name: string }) => {
  86. handleInputChange(option.value)
  87. }
  88. const handleTagChange = (newSequences: string[]) => {
  89. handleInputChange(newSequences)
  90. }
  91. const handleSwitch = (checked: boolean) => {
  92. if (onSwitch) {
  93. const assignValue: ParameterValue = localValue || getDefaultValue()
  94. onSwitch(checked, assignValue)
  95. }
  96. }
  97. useEffect(() => {
  98. if ((parameterRule.type === 'int' || parameterRule.type === 'float') && numberInputRef.current)
  99. numberInputRef.current.value = `${renderValue}`
  100. }, [value])
  101. const renderInput = () => {
  102. const numberInputWithSlide = (parameterRule.type === 'int' || parameterRule.type === 'float')
  103. && !isNullOrUndefined(parameterRule.min)
  104. && !isNullOrUndefined(parameterRule.max)
  105. if (parameterRule.type === 'int') {
  106. let step = 100
  107. if (parameterRule.max) {
  108. if (parameterRule.max < 100)
  109. step = 1
  110. else if (parameterRule.max < 1000)
  111. step = 10
  112. else if (parameterRule.max < 10000)
  113. step = 100
  114. }
  115. return (
  116. <>
  117. {numberInputWithSlide && <Slider
  118. className='w-[120px]'
  119. value={renderValue as number}
  120. min={parameterRule.min}
  121. max={parameterRule.max}
  122. step={step}
  123. onChange={handleSlideChange}
  124. />}
  125. <input
  126. ref={numberInputRef}
  127. className='shrink-0 block ml-4 pl-3 w-16 h-8 appearance-none outline-none rounded-lg bg-gray-100 text-[13px] text-gra-900'
  128. type='number'
  129. max={parameterRule.max}
  130. min={parameterRule.min}
  131. step={numberInputWithSlide ? step : +`0.${parameterRule.precision || 0}`}
  132. onChange={handleNumberInputChange}
  133. onBlur={handleNumberInputBlur}
  134. />
  135. </>
  136. )
  137. }
  138. if (parameterRule.type === 'float') {
  139. return (
  140. <>
  141. {numberInputWithSlide && <Slider
  142. className='w-[120px]'
  143. value={renderValue as number}
  144. min={parameterRule.min}
  145. max={parameterRule.max}
  146. step={0.1}
  147. onChange={handleSlideChange}
  148. />}
  149. <input
  150. ref={numberInputRef}
  151. className='shrink-0 block ml-4 pl-3 w-16 h-8 appearance-none outline-none rounded-lg bg-gray-100 text-[13px] text-gra-900'
  152. type='number'
  153. max={parameterRule.max}
  154. min={parameterRule.min}
  155. step={numberInputWithSlide ? 0.1 : +`0.${parameterRule.precision || 0}`}
  156. onChange={handleNumberInputChange}
  157. onBlur={handleNumberInputBlur}
  158. />
  159. </>
  160. )
  161. }
  162. if (parameterRule.type === 'boolean') {
  163. return (
  164. <Radio.Group
  165. className='w-[200px] flex items-center'
  166. value={renderValue ? 1 : 0}
  167. onChange={handleRadioChange}
  168. >
  169. <Radio value={1} className='!mr-1 w-[94px]'>True</Radio>
  170. <Radio value={0} className='w-[94px]'>False</Radio>
  171. </Radio.Group>
  172. )
  173. }
  174. if (parameterRule.type === 'string' && !parameterRule.options?.length) {
  175. return (
  176. <input
  177. className={cn(isInWorkflow ? 'w-[200px]' : 'w-full', 'ml-4 flex items-center px-3 h-8 appearance-none outline-none rounded-lg bg-gray-100 text-[13px] text-gra-900')}
  178. value={renderValue as string}
  179. onChange={handleStringInputChange}
  180. />
  181. )
  182. }
  183. if (parameterRule.type === 'text') {
  184. return (
  185. <textarea
  186. className='w-full h-20 ml-4 px-1 rounded-lg bg-gray-100 outline-none text-[12px] text-gray-900'
  187. value={renderValue as string}
  188. onChange={handleStringInputChange}
  189. />
  190. )
  191. }
  192. if (parameterRule.type === 'string' && !!parameterRule?.options?.length) {
  193. return (
  194. <SimpleSelect
  195. className='!py-0'
  196. wrapperClassName={cn(isInWorkflow ? '!w-[200px]' : 'w-full', 'ml-4 !h-8')}
  197. defaultValue={renderValue as string}
  198. onSelect={handleSelect}
  199. items={parameterRule.options.map(option => ({ value: option, name: option }))}
  200. />
  201. )
  202. }
  203. if (parameterRule.type === 'tag') {
  204. return (
  205. <div className={cn(isInWorkflow ? 'w-[200px]' : 'w-full', 'ml-4')}>
  206. <TagInput
  207. items={renderValue as string[]}
  208. onChange={handleTagChange}
  209. customizedConfirmKey='Tab'
  210. isInWorkflow={isInWorkflow}
  211. />
  212. </div>
  213. )
  214. }
  215. return null
  216. }
  217. return (
  218. <div className={`flex items-center justify-between ${className}`}>
  219. <div>
  220. <div className={cn(isInWorkflow ? 'w-[140px]' : 'w-full', 'ml-4 shrink-0 flex items-center')}>
  221. <div
  222. className='mr-0.5 text-[13px] font-medium text-gray-700 truncate'
  223. title={parameterRule.label[language] || parameterRule.label.en_US}
  224. >
  225. {parameterRule.label[language] || parameterRule.label.en_US}
  226. </div>
  227. {
  228. parameterRule.help && (
  229. <Tooltip
  230. popupContent={(
  231. <div className='w-[200px] whitespace-pre-wrap'>{parameterRule.help[language] || parameterRule.help.en_US}</div>
  232. )}
  233. popupClassName='mr-1'
  234. triggerClassName='mr-1 w-4 h-4 shrink-0'
  235. />
  236. )
  237. }
  238. {
  239. !parameterRule.required && parameterRule.name !== 'stop' && (
  240. <Switch
  241. className='mr-1'
  242. defaultValue={!isNullOrUndefined(value)}
  243. onChange={handleSwitch}
  244. size='md'
  245. />
  246. )
  247. }
  248. </div>
  249. {
  250. parameterRule.type === 'tag' && (
  251. <div className={cn(!isInWorkflow && 'w-[200px]', 'text-gray-400 text-xs font-normal')}>
  252. {parameterRule?.tagPlaceholder?.[language]}
  253. </div>
  254. )
  255. }
  256. </div>
  257. {renderInput()}
  258. </div>
  259. )
  260. }
  261. export default ParameterItem