chat-input.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import type { FC } from 'react'
  2. import {
  3. useRef,
  4. useState,
  5. } from 'react'
  6. import { useContext } from 'use-context-selector'
  7. import Recorder from 'js-audio-recorder'
  8. import { useTranslation } from 'react-i18next'
  9. import Textarea from 'rc-textarea'
  10. import type {
  11. EnableType,
  12. OnSend,
  13. VisionConfig,
  14. } from '../types'
  15. import { TransferMethod } from '../types'
  16. import TooltipPlus from '@/app/components/base/tooltip-plus'
  17. import { ToastContext } from '@/app/components/base/toast'
  18. import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
  19. import VoiceInput from '@/app/components/base/voice-input'
  20. import { Microphone01 } from '@/app/components/base/icons/src/vender/line/mediaAndDevices'
  21. import { Microphone01 as Microphone01Solid } from '@/app/components/base/icons/src/vender/solid/mediaAndDevices'
  22. import { XCircle } from '@/app/components/base/icons/src/vender/solid/general'
  23. import { Send03 } from '@/app/components/base/icons/src/vender/solid/communication'
  24. import ChatImageUploader from '@/app/components/base/image-uploader/chat-image-uploader'
  25. import ImageList from '@/app/components/base/image-uploader/image-list'
  26. import {
  27. useClipboardUploader,
  28. useDraggableUploader,
  29. useImageFiles,
  30. } from '@/app/components/base/image-uploader/hooks'
  31. type ChatInputProps = {
  32. visionConfig?: VisionConfig
  33. speechToTextConfig?: EnableType
  34. onSend?: OnSend
  35. }
  36. const ChatInput: FC<ChatInputProps> = ({
  37. visionConfig,
  38. speechToTextConfig,
  39. onSend,
  40. }) => {
  41. const { t } = useTranslation()
  42. const { notify } = useContext(ToastContext)
  43. const [voiceInputShow, setVoiceInputShow] = useState(false)
  44. const {
  45. files,
  46. onUpload,
  47. onRemove,
  48. onReUpload,
  49. onImageLinkLoadError,
  50. onImageLinkLoadSuccess,
  51. onClear,
  52. } = useImageFiles()
  53. const { onPaste } = useClipboardUploader({ onUpload, visionConfig, files })
  54. const { onDragEnter, onDragLeave, onDragOver, onDrop, isDragActive } = useDraggableUploader<HTMLTextAreaElement>({ onUpload, files, visionConfig })
  55. const isUseInputMethod = useRef(false)
  56. const [query, setQuery] = useState('')
  57. const handleContentChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
  58. const value = e.target.value
  59. setQuery(value)
  60. }
  61. const handleSend = () => {
  62. if (onSend) {
  63. onSend(query, files.filter(file => file.progress !== -1).map(fileItem => ({
  64. type: 'image',
  65. transfer_method: fileItem.type,
  66. url: fileItem.url,
  67. upload_file_id: fileItem.fileId,
  68. })))
  69. setQuery('')
  70. }
  71. if (!files.find(item => item.type === TransferMethod.local_file && !item.fileId)) {
  72. if (files.length)
  73. onClear()
  74. }
  75. }
  76. const handleKeyUp = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  77. if (e.code === 'Enter') {
  78. e.preventDefault()
  79. // prevent send message when using input method enter
  80. if (!e.shiftKey && !isUseInputMethod.current)
  81. handleSend()
  82. }
  83. }
  84. const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  85. isUseInputMethod.current = e.nativeEvent.isComposing
  86. if (e.code === 'Enter' && !e.shiftKey) {
  87. setQuery(query.replace(/\n$/, ''))
  88. e.preventDefault()
  89. }
  90. }
  91. const logError = (message: string) => {
  92. notify({ type: 'error', message, duration: 3000 })
  93. }
  94. const handleVoiceInputShow = () => {
  95. (Recorder as any).getPermission().then(() => {
  96. setVoiceInputShow(true)
  97. }, () => {
  98. logError(t('common.voiceInput.notAllow'))
  99. })
  100. }
  101. const media = useBreakpoints()
  102. const isMobile = media === MediaType.mobile
  103. const sendBtn = (
  104. <div
  105. className='group flex items-center justify-center w-8 h-8 rounded-lg hover:bg-[#EBF5FF] cursor-pointer'
  106. onClick={handleSend}
  107. >
  108. <Send03
  109. className={`
  110. w-5 h-5 text-gray-300 group-hover:text-primary-600
  111. ${!!query.trim() && 'text-primary-600'}
  112. `}
  113. />
  114. </div>
  115. )
  116. return (
  117. <div
  118. className={`
  119. relative p-[5.5px] max-h-[150px] bg-white border-[1.5px] border-gray-200 rounded-xl overflow-y-auto
  120. ${isDragActive && 'border-primary-600'}
  121. `}
  122. >
  123. {
  124. visionConfig?.enabled && (
  125. <>
  126. <div className='absolute bottom-2 left-2 flex items-center'>
  127. <ChatImageUploader
  128. settings={visionConfig}
  129. onUpload={onUpload}
  130. disabled={files.length >= visionConfig.number_limits}
  131. />
  132. <div className='mx-1 w-[1px] h-4 bg-black/5' />
  133. </div>
  134. <div className='pl-[52px]'>
  135. <ImageList
  136. list={files}
  137. onRemove={onRemove}
  138. onReUpload={onReUpload}
  139. onImageLinkLoadSuccess={onImageLinkLoadSuccess}
  140. onImageLinkLoadError={onImageLinkLoadError}
  141. />
  142. </div>
  143. </>
  144. )
  145. }
  146. <Textarea
  147. className={`
  148. block w-full px-2 pr-[118px] py-[7px] leading-5 max-h-none text-sm text-gray-700 outline-none appearance-none resize-none
  149. ${visionConfig?.enabled && 'pl-12'}
  150. `}
  151. value={query}
  152. onChange={handleContentChange}
  153. onKeyUp={handleKeyUp}
  154. onKeyDown={handleKeyDown}
  155. onPaste={onPaste}
  156. onDragEnter={onDragEnter}
  157. onDragLeave={onDragLeave}
  158. onDragOver={onDragOver}
  159. onDrop={onDrop}
  160. autoSize
  161. />
  162. <div className='absolute bottom-[7px] right-2 flex items-center h-8'>
  163. <div className='flex items-center px-1 h-5 rounded-md bg-gray-100 text-xs font-medium text-gray-500'>
  164. {query.trim().length}
  165. </div>
  166. {
  167. query
  168. ? (
  169. <div className='flex justify-center items-center ml-2 w-8 h-8 cursor-pointer hover:bg-gray-100 rounded-lg' onClick={() => setQuery('')}>
  170. <XCircle className='w-4 h-4 text-[#98A2B3]' />
  171. </div>
  172. )
  173. : speechToTextConfig?.enabled
  174. ? (
  175. <div
  176. className='group flex justify-center items-center ml-2 w-8 h-8 hover:bg-primary-50 rounded-lg cursor-pointer'
  177. onClick={handleVoiceInputShow}
  178. >
  179. <Microphone01 className='block w-4 h-4 text-gray-500 group-hover:hidden' />
  180. <Microphone01Solid className='hidden w-4 h-4 text-primary-600 group-hover:block' />
  181. </div>
  182. )
  183. : null
  184. }
  185. <div className='mx-2 w-[1px] h-4 bg-black opacity-5' />
  186. {isMobile
  187. ? sendBtn
  188. : (
  189. <TooltipPlus
  190. popupContent={
  191. <div>
  192. <div>{t('common.operation.send')} Enter</div>
  193. <div>{t('common.operation.lineBreak')} Shift Enter</div>
  194. </div>
  195. }
  196. >
  197. {sendBtn}
  198. </TooltipPlus>
  199. )}
  200. </div>
  201. {
  202. voiceInputShow && (
  203. <VoiceInput
  204. onCancel={() => setVoiceInputShow(false)}
  205. onConverted={text => setQuery(text)}
  206. />
  207. )
  208. }
  209. </div>
  210. )
  211. }
  212. export default ChatInput