form.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { useCallback } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useChatWithHistoryContext } from '../context'
  4. import Input from './form-input'
  5. import { PortalSelect } from '@/app/components/base/select'
  6. import { InputVarType } from '@/app/components/workflow/types'
  7. import { FileUploaderInAttachmentWrapper } from '@/app/components/base/file-uploader'
  8. const Form = () => {
  9. const { t } = useTranslation()
  10. const {
  11. inputsForms,
  12. newConversationInputs,
  13. newConversationInputsRef,
  14. handleNewConversationInputsChange,
  15. isMobile,
  16. } = useChatWithHistoryContext()
  17. const handleFormChange = useCallback((variable: string, value: any) => {
  18. handleNewConversationInputsChange({
  19. ...newConversationInputsRef.current,
  20. [variable]: value,
  21. })
  22. }, [newConversationInputsRef, handleNewConversationInputsChange])
  23. const renderField = (form: any) => {
  24. const {
  25. label,
  26. required,
  27. variable,
  28. options,
  29. } = form
  30. if (form.type === 'text-input' || form.type === 'paragraph') {
  31. return (
  32. <Input
  33. form={form}
  34. value={newConversationInputs[variable]}
  35. onChange={handleFormChange}
  36. />
  37. )
  38. }
  39. if (form.type === 'number') {
  40. return (
  41. <input
  42. className="grow h-9 rounded-lg bg-gray-100 px-2.5 outline-none appearance-none"
  43. type="number"
  44. value={newConversationInputs[variable] || ''}
  45. onChange={e => handleFormChange(variable, e.target.value)}
  46. placeholder={`${label}${!required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  47. />
  48. )
  49. }
  50. if (form.type === InputVarType.singleFile) {
  51. return (
  52. <FileUploaderInAttachmentWrapper
  53. value={newConversationInputs[variable] ? [newConversationInputs[variable]] : []}
  54. onChange={files => handleFormChange(variable, files[0])}
  55. fileConfig={{
  56. allowed_file_types: form.allowed_file_types,
  57. allowed_file_extensions: form.allowed_file_extensions,
  58. allowed_file_upload_methods: form.allowed_file_upload_methods,
  59. number_limits: 1,
  60. }}
  61. />
  62. )
  63. }
  64. if (form.type === InputVarType.multiFiles) {
  65. return (
  66. <FileUploaderInAttachmentWrapper
  67. value={newConversationInputs[variable]}
  68. onChange={files => handleFormChange(variable, files)}
  69. fileConfig={{
  70. allowed_file_types: form.allowed_file_types,
  71. allowed_file_extensions: form.allowed_file_extensions,
  72. allowed_file_upload_methods: form.allowed_file_upload_methods,
  73. number_limits: form.max_length,
  74. }}
  75. />
  76. )
  77. }
  78. return (
  79. <PortalSelect
  80. popupClassName='w-[200px]'
  81. value={newConversationInputs[variable]}
  82. items={options.map((option: string) => ({ value: option, name: option }))}
  83. onSelect={item => handleFormChange(variable, item.value as string)}
  84. placeholder={`${label}${!required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  85. />
  86. )
  87. }
  88. if (!inputsForms.length)
  89. return null
  90. return (
  91. <div className='mb-4 py-2'>
  92. {
  93. inputsForms.map(form => (
  94. <div
  95. key={form.variable}
  96. className={`flex mb-3 last-of-type:mb-0 text-sm text-gray-900 ${isMobile && '!flex-wrap'}`}
  97. >
  98. <div className={`shrink-0 mr-2 py-2 w-[128px] ${isMobile && '!w-full'}`}>{form.label}</div>
  99. {renderField(form)}
  100. </div>
  101. ))
  102. }
  103. </div>
  104. )
  105. }
  106. export default Form