chat-image-uploader.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import type { FC } from 'react'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import Uploader from './uploader'
  5. import ImageLinkInput from './image-link-input'
  6. import cn from '@/utils/classnames'
  7. import { ImagePlus } from '@/app/components/base/icons/src/vender/line/images'
  8. import { TransferMethod } from '@/types/app'
  9. import {
  10. PortalToFollowElem,
  11. PortalToFollowElemContent,
  12. PortalToFollowElemTrigger,
  13. } from '@/app/components/base/portal-to-follow-elem'
  14. import { Upload03 } from '@/app/components/base/icons/src/vender/line/general'
  15. import type { ImageFile, VisionSettings } from '@/types/app'
  16. type UploadOnlyFromLocalProps = {
  17. onUpload: (imageFile: ImageFile) => void
  18. disabled?: boolean
  19. limit?: number
  20. }
  21. const UploadOnlyFromLocal: FC<UploadOnlyFromLocalProps> = ({
  22. onUpload,
  23. disabled,
  24. limit,
  25. }) => {
  26. return (
  27. <Uploader onUpload={onUpload} disabled={disabled} limit={limit}>
  28. {hovering => (
  29. <div
  30. className={`
  31. relative flex items-center justify-center w-8 h-8 rounded-lg cursor-pointer
  32. ${hovering && 'bg-gray-100'}
  33. `}
  34. >
  35. <ImagePlus className="w-4 h-4 text-gray-500" />
  36. </div>
  37. )}
  38. </Uploader>
  39. )
  40. }
  41. type UploaderButtonProps = {
  42. methods: VisionSettings['transfer_methods']
  43. onUpload: (imageFile: ImageFile) => void
  44. disabled?: boolean
  45. limit?: number
  46. }
  47. const UploaderButton: FC<UploaderButtonProps> = ({
  48. methods,
  49. onUpload,
  50. disabled,
  51. limit,
  52. }) => {
  53. const { t } = useTranslation()
  54. const [open, setOpen] = useState(false)
  55. const hasUploadFromLocal = methods.find(
  56. method => method === TransferMethod.local_file,
  57. )
  58. const handleUpload = (imageFile: ImageFile) => {
  59. onUpload(imageFile)
  60. }
  61. const closePopover = () => setOpen(false)
  62. const handleToggle = () => {
  63. if (disabled)
  64. return
  65. setOpen(v => !v)
  66. }
  67. return (
  68. <PortalToFollowElem
  69. open={open}
  70. onOpenChange={setOpen}
  71. placement="top-start"
  72. >
  73. <PortalToFollowElemTrigger onClick={handleToggle}>
  74. <button
  75. type="button"
  76. disabled={disabled}
  77. className="relative flex items-center justify-center w-8 h-8 enabled:hover:bg-gray-100 rounded-lg disabled:cursor-not-allowed"
  78. >
  79. <ImagePlus className="w-4 h-4 text-gray-500" />
  80. </button>
  81. </PortalToFollowElemTrigger>
  82. <PortalToFollowElemContent className="z-50">
  83. <div className="p-2 w-[260px] bg-white rounded-lg border-[0.5px] border-gray-200 shadow-lg">
  84. <ImageLinkInput onUpload={handleUpload} disabled={disabled} />
  85. {hasUploadFromLocal && (
  86. <>
  87. <div className="flex items-center mt-2 px-2 text-xs font-medium text-gray-400">
  88. <div className="mr-3 w-[93px] h-[1px] bg-gradient-to-l from-[#F3F4F6]" />
  89. OR
  90. <div className="ml-3 w-[93px] h-[1px] bg-gradient-to-r from-[#F3F4F6]" />
  91. </div>
  92. <Uploader
  93. onUpload={handleUpload}
  94. limit={limit}
  95. closePopover={closePopover}
  96. >
  97. {hovering => (
  98. <div
  99. className={cn(
  100. 'flex items-center justify-center h-8 text-[13px] font-medium text-[#155EEF] rounded-lg cursor-pointer',
  101. hovering && 'bg-primary-50',
  102. )}
  103. >
  104. <Upload03 className="mr-1 w-4 h-4" />
  105. {t('common.imageUploader.uploadFromComputer')}
  106. </div>
  107. )}
  108. </Uploader>
  109. </>
  110. )}
  111. </div>
  112. </PortalToFollowElemContent>
  113. </PortalToFollowElem>
  114. )
  115. }
  116. type ChatImageUploaderProps = {
  117. settings: VisionSettings
  118. onUpload: (imageFile: ImageFile) => void
  119. disabled?: boolean
  120. }
  121. const ChatImageUploader: FC<ChatImageUploaderProps> = ({
  122. settings,
  123. onUpload,
  124. disabled,
  125. }) => {
  126. const onlyUploadLocal
  127. = settings.transfer_methods.length === 1
  128. && settings.transfer_methods[0] === TransferMethod.local_file
  129. if (onlyUploadLocal) {
  130. return (
  131. <UploadOnlyFromLocal
  132. onUpload={onUpload}
  133. disabled={disabled}
  134. limit={+settings.image_file_size_limit!}
  135. />
  136. )
  137. }
  138. return (
  139. <UploaderButton
  140. methods={settings.transfer_methods}
  141. onUpload={onUpload}
  142. disabled={disabled}
  143. limit={+settings.image_file_size_limit!}
  144. />
  145. )
  146. }
  147. export default ChatImageUploader