You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

content.tsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import React, { memo, useCallback } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useEmbeddedChatbotContext } from '../context'
  4. import Input from '@/app/components/base/input'
  5. import Textarea from '@/app/components/base/textarea'
  6. import { PortalSelect } from '@/app/components/base/select'
  7. import { FileUploaderInAttachmentWrapper } from '@/app/components/base/file-uploader'
  8. import { InputVarType } from '@/app/components/workflow/types'
  9. type Props = {
  10. showTip?: boolean
  11. }
  12. const InputsFormContent = ({ showTip }: Props) => {
  13. const { t } = useTranslation()
  14. const {
  15. appParams,
  16. inputsForms,
  17. currentConversationId,
  18. currentConversationInputs,
  19. setCurrentConversationInputs,
  20. newConversationInputs,
  21. newConversationInputsRef,
  22. handleNewConversationInputsChange,
  23. } = useEmbeddedChatbotContext()
  24. const inputsFormValue = currentConversationId ? currentConversationInputs : newConversationInputs
  25. const handleFormChange = useCallback((variable: string, value: any) => {
  26. setCurrentConversationInputs({
  27. ...currentConversationInputs,
  28. [variable]: value,
  29. })
  30. handleNewConversationInputsChange({
  31. ...newConversationInputsRef.current,
  32. [variable]: value,
  33. })
  34. }, [newConversationInputsRef, handleNewConversationInputsChange, currentConversationInputs, setCurrentConversationInputs])
  35. const visibleInputsForms = inputsForms.filter(form => form.hide !== true)
  36. return (
  37. <div className='space-y-4'>
  38. {visibleInputsForms.map(form => (
  39. <div key={form.variable} className='space-y-1'>
  40. <div className='flex h-6 items-center gap-1'>
  41. <div className='system-md-semibold text-text-secondary'>{form.label}</div>
  42. {!form.required && (
  43. <div className='system-xs-regular text-text-tertiary'>{t('appDebug.variableTable.optional')}</div>
  44. )}
  45. </div>
  46. {form.type === InputVarType.textInput && (
  47. <Input
  48. value={inputsFormValue?.[form.variable] || ''}
  49. onChange={e => handleFormChange(form.variable, e.target.value)}
  50. placeholder={form.label}
  51. />
  52. )}
  53. {form.type === InputVarType.number && (
  54. <Input
  55. type='number'
  56. value={inputsFormValue?.[form.variable] || ''}
  57. onChange={e => handleFormChange(form.variable, e.target.value)}
  58. placeholder={form.label}
  59. />
  60. )}
  61. {form.type === InputVarType.paragraph && (
  62. <Textarea
  63. value={inputsFormValue?.[form.variable] || ''}
  64. onChange={e => handleFormChange(form.variable, e.target.value)}
  65. placeholder={form.label}
  66. />
  67. )}
  68. {form.type === InputVarType.select && (
  69. <PortalSelect
  70. popupClassName='w-[200px]'
  71. value={inputsFormValue?.[form.variable] ?? form.default ?? ''}
  72. items={form.options.map((option: string) => ({ value: option, name: option }))}
  73. onSelect={item => handleFormChange(form.variable, item.value as string)}
  74. placeholder={form.label}
  75. />
  76. )}
  77. {form.type === InputVarType.singleFile && (
  78. <FileUploaderInAttachmentWrapper
  79. value={inputsFormValue?.[form.variable] ? [inputsFormValue?.[form.variable]] : []}
  80. onChange={files => handleFormChange(form.variable, files[0])}
  81. fileConfig={{
  82. allowed_file_types: form.allowed_file_types,
  83. allowed_file_extensions: form.allowed_file_extensions,
  84. allowed_file_upload_methods: form.allowed_file_upload_methods,
  85. number_limits: 1,
  86. fileUploadConfig: (appParams as any).system_parameters,
  87. }}
  88. />
  89. )}
  90. {form.type === InputVarType.multiFiles && (
  91. <FileUploaderInAttachmentWrapper
  92. value={inputsFormValue?.[form.variable] || []}
  93. onChange={files => handleFormChange(form.variable, files)}
  94. fileConfig={{
  95. allowed_file_types: form.allowed_file_types,
  96. allowed_file_extensions: form.allowed_file_extensions,
  97. allowed_file_upload_methods: form.allowed_file_upload_methods,
  98. number_limits: form.max_length,
  99. fileUploadConfig: (appParams as any).system_parameters,
  100. }}
  101. />
  102. )}
  103. </div>
  104. ))}
  105. {showTip && (
  106. <div className='system-xs-regular text-text-tertiary'>{t('share.chat.chatFormTip')}</div>
  107. )}
  108. </div>
  109. )
  110. }
  111. export default memo(InputsFormContent)