parameter-item.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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' || parameterRule.type === 'float') {
  106. let step = 100
  107. if (parameterRule.max) {
  108. if (parameterRule.max < 10)
  109. step = 0.1
  110. else if (parameterRule.max < 100)
  111. step = 1
  112. else if (parameterRule.max < 1000)
  113. step = 10
  114. else if (parameterRule.max < 10000)
  115. step = 100
  116. }
  117. return (
  118. <>
  119. {numberInputWithSlide && <Slider
  120. className='w-[120px]'
  121. value={renderValue as number}
  122. min={parameterRule.min}
  123. max={parameterRule.max}
  124. step={step}
  125. onChange={handleSlideChange}
  126. />}
  127. <input
  128. ref={numberInputRef}
  129. 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'
  130. type='number'
  131. max={parameterRule.max}
  132. min={parameterRule.min}
  133. step={numberInputWithSlide ? step : +`0.${parameterRule.precision || 0}`}
  134. onChange={handleNumberInputChange}
  135. onBlur={handleNumberInputBlur}
  136. />
  137. </>
  138. )
  139. }
  140. if (parameterRule.type === 'boolean') {
  141. return (
  142. <Radio.Group
  143. className='w-[200px] flex items-center'
  144. value={renderValue ? 1 : 0}
  145. onChange={handleRadioChange}
  146. >
  147. <Radio value={1} className='!mr-1 w-[94px]'>True</Radio>
  148. <Radio value={0} className='w-[94px]'>False</Radio>
  149. </Radio.Group>
  150. )
  151. }
  152. if (parameterRule.type === 'string' && !parameterRule.options?.length) {
  153. return (
  154. <input
  155. 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')}
  156. value={renderValue as string}
  157. onChange={handleStringInputChange}
  158. />
  159. )
  160. }
  161. if (parameterRule.type === 'text') {
  162. return (
  163. <textarea
  164. className='w-full h-20 ml-4 px-1 rounded-lg bg-gray-100 outline-none text-[12px] text-gray-900'
  165. value={renderValue as string}
  166. onChange={handleStringInputChange}
  167. />
  168. )
  169. }
  170. if (parameterRule.type === 'string' && !!parameterRule?.options?.length) {
  171. return (
  172. <SimpleSelect
  173. className='!py-0'
  174. wrapperClassName={cn(isInWorkflow ? '!w-[200px]' : 'w-full', 'ml-4 !h-8')}
  175. defaultValue={renderValue as string}
  176. onSelect={handleSelect}
  177. items={parameterRule.options.map(option => ({ value: option, name: option }))}
  178. />
  179. )
  180. }
  181. if (parameterRule.type === 'tag') {
  182. return (
  183. <div className={cn(isInWorkflow ? 'w-[200px]' : 'w-full', 'ml-4')}>
  184. <TagInput
  185. items={renderValue as string[]}
  186. onChange={handleTagChange}
  187. customizedConfirmKey='Tab'
  188. isInWorkflow={isInWorkflow}
  189. />
  190. </div>
  191. )
  192. }
  193. return null
  194. }
  195. return (
  196. <div className={`flex items-center justify-between ${className}`}>
  197. <div>
  198. <div className={cn(isInWorkflow ? 'w-[140px]' : 'w-full', 'ml-4 shrink-0 flex items-center')}>
  199. <div
  200. className='mr-0.5 text-[13px] font-medium text-gray-700 truncate'
  201. title={parameterRule.label[language] || parameterRule.label.en_US}
  202. >
  203. {parameterRule.label[language] || parameterRule.label.en_US}
  204. </div>
  205. {
  206. parameterRule.help && (
  207. <Tooltip
  208. popupContent={(
  209. <div className='w-[200px] whitespace-pre-wrap'>{parameterRule.help[language] || parameterRule.help.en_US}</div>
  210. )}
  211. popupClassName='mr-1'
  212. triggerClassName='mr-1 w-4 h-4 shrink-0'
  213. />
  214. )
  215. }
  216. {
  217. !parameterRule.required && parameterRule.name !== 'stop' && (
  218. <Switch
  219. className='mr-1'
  220. defaultValue={!isNullOrUndefined(value)}
  221. onChange={handleSwitch}
  222. size='md'
  223. />
  224. )
  225. }
  226. </div>
  227. {
  228. parameterRule.type === 'tag' && (
  229. <div className={cn(!isInWorkflow && 'w-[200px]', 'text-gray-400 text-xs font-normal')}>
  230. {parameterRule?.tagPlaceholder?.[language]}
  231. </div>
  232. )
  233. }
  234. </div>
  235. {renderInput()}
  236. </div>
  237. )
  238. }
  239. export default ParameterItem