Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

content.tsx 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React, { 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. return (
  36. <div className='space-y-4'>
  37. {inputsForms.map(form => (
  38. <div key={form.variable} className='space-y-1'>
  39. <div className='flex h-6 items-center gap-1'>
  40. <div className='system-md-semibold text-text-secondary'>{form.label}</div>
  41. {!form.required && (
  42. <div className='system-xs-regular text-text-tertiary'>{t('appDebug.variableTable.optional')}</div>
  43. )}
  44. </div>
  45. {form.type === InputVarType.textInput && (
  46. <Input
  47. value={inputsFormValue?.[form.variable] || ''}
  48. onChange={e => handleFormChange(form.variable, e.target.value)}
  49. placeholder={form.label}
  50. />
  51. )}
  52. {form.type === InputVarType.number && (
  53. <Input
  54. type='number'
  55. value={inputsFormValue?.[form.variable] || ''}
  56. onChange={e => handleFormChange(form.variable, e.target.value)}
  57. placeholder={form.label}
  58. />
  59. )}
  60. {form.type === InputVarType.paragraph && (
  61. <Textarea
  62. value={inputsFormValue?.[form.variable] || ''}
  63. onChange={e => handleFormChange(form.variable, e.target.value)}
  64. placeholder={form.label}
  65. />
  66. )}
  67. {form.type === InputVarType.select && (
  68. <PortalSelect
  69. popupClassName='w-[200px]'
  70. value={inputsFormValue?.[form.variable]}
  71. items={form.options.map((option: string) => ({ value: option, name: option }))}
  72. onSelect={item => handleFormChange(form.variable, item.value as string)}
  73. placeholder={form.label}
  74. />
  75. )}
  76. {form.type === InputVarType.singleFile && (
  77. <FileUploaderInAttachmentWrapper
  78. value={inputsFormValue?.[form.variable] ? [inputsFormValue?.[form.variable]] : []}
  79. onChange={files => handleFormChange(form.variable, files[0])}
  80. fileConfig={{
  81. allowed_file_types: form.allowed_file_types,
  82. allowed_file_extensions: form.allowed_file_extensions,
  83. allowed_file_upload_methods: form.allowed_file_upload_methods,
  84. number_limits: 1,
  85. fileUploadConfig: (appParams as any).system_parameters,
  86. }}
  87. />
  88. )}
  89. {form.type === InputVarType.multiFiles && (
  90. <FileUploaderInAttachmentWrapper
  91. value={inputsFormValue?.[form.variable] || []}
  92. onChange={files => handleFormChange(form.variable, files)}
  93. fileConfig={{
  94. allowed_file_types: form.allowed_file_types,
  95. allowed_file_extensions: form.allowed_file_extensions,
  96. allowed_file_upload_methods: form.allowed_file_upload_methods,
  97. number_limits: form.max_length,
  98. fileUploadConfig: (appParams as any).system_parameters,
  99. }}
  100. />
  101. )}
  102. </div>
  103. ))}
  104. {showTip && (
  105. <div className='system-xs-regular text-text-tertiary'>{t('share.chat.chatFormTip')}</div>
  106. )}
  107. </div>
  108. )
  109. }
  110. export default InputsFormContent