From b75d8ca6211ac76caed7a0139ff32163eb8f4526 Mon Sep 17 00:00:00 2001 From: Rozstone <42225395+wststone@users.noreply.github.com> Date: Sun, 10 Mar 2024 13:11:41 +0800 Subject: [PATCH] fix: auto closing when close local image uploading (#2767) --- .../configuration/base/warning-mask/index.tsx | 2 +- .../image-uploader/chat-image-uploader.tsx | 97 +++++----- .../base/image-uploader/image-link-input.tsx | 1 + .../base/image-uploader/image-list.tsx | 172 ++++++++++-------- .../base/image-uploader/uploader.tsx | 16 +- 5 files changed, 158 insertions(+), 130 deletions(-) diff --git a/web/app/components/app/configuration/base/warning-mask/index.tsx b/web/app/components/app/configuration/base/warning-mask/index.tsx index 550c3f73a89445..03df4f16dfbf0e 100644 --- a/web/app/components/app/configuration/base/warning-mask/index.tsx +++ b/web/app/components/app/configuration/base/warning-mask/index.tsx @@ -24,7 +24,7 @@ const WarningMask: FC = ({ return (
-
+
{warningIcon}
{title} diff --git a/web/app/components/base/image-uploader/chat-image-uploader.tsx b/web/app/components/base/image-uploader/chat-image-uploader.tsx index 97f85a31372d0e..4d34e9c15aa8e4 100644 --- a/web/app/components/base/image-uploader/chat-image-uploader.tsx +++ b/web/app/components/base/image-uploader/chat-image-uploader.tsx @@ -1,6 +1,7 @@ import type { FC } from 'react' import { useState } from 'react' import { useTranslation } from 'react-i18next' +import cn from 'classnames' import Uploader from './uploader' import ImageLinkInput from './image-link-input' import { ImagePlus } from '@/app/components/base/icons/src/vender/line/images' @@ -25,16 +26,16 @@ const UploadOnlyFromLocal: FC = ({ }) => { return ( - { - hovering => ( -
( +
- -
- ) - } + `} + > + +
+ )}
) } @@ -54,13 +55,16 @@ const UploaderButton: FC = ({ const { t } = useTranslation() const [open, setOpen] = useState(false) - const hasUploadFromLocal = methods.find(method => method === TransferMethod.local_file) + const hasUploadFromLocal = methods.find( + method => method === TransferMethod.local_file, + ) const handleUpload = (imageFile: ImageFile) => { - setOpen(false) onUpload(imageFile) } + const closePopover = () => setOpen(false) + const handleToggle = () => { if (disabled) return @@ -72,43 +76,46 @@ const UploaderButton: FC = ({ -
- -
+
- -
+ +
- { - hasUploadFromLocal && ( - <> -
-
- OR -
-
- - { - hovering => ( -
- - {t('common.imageUploader.uploadFromComputer')} -
- ) - } -
- - ) - } + {hasUploadFromLocal && ( + <> +
+
+ OR +
+
+ + {hovering => ( +
+ + {t('common.imageUploader.uploadFromComputer')} +
+ )} +
+ + )}
@@ -125,7 +132,9 @@ const ChatImageUploader: FC = ({ onUpload, disabled, }) => { - const onlyUploadLocal = settings.transfer_methods.length === 1 && settings.transfer_methods[0] === TransferMethod.local_file + const onlyUploadLocal + = settings.transfer_methods.length === 1 + && settings.transfer_methods[0] === TransferMethod.local_file if (onlyUploadLocal) { return ( diff --git a/web/app/components/base/image-uploader/image-link-input.tsx b/web/app/components/base/image-uploader/image-link-input.tsx index 6c1435db30887b..d9ca50ac3e6433 100644 --- a/web/app/components/base/image-uploader/image-link-input.tsx +++ b/web/app/components/base/image-uploader/image-link-input.tsx @@ -30,6 +30,7 @@ const ImageLinkInput: FC = ({ return (
setImageLink(e.target.value)} diff --git a/web/app/components/base/image-uploader/image-list.tsx b/web/app/components/base/image-uploader/image-list.tsx index b359d2edeb7dbd..6573815950037a 100644 --- a/web/app/components/base/image-uploader/image-list.tsx +++ b/web/app/components/base/image-uploader/image-list.tsx @@ -1,7 +1,11 @@ import type { FC } from 'react' import { useState } from 'react' import { useTranslation } from 'react-i18next' -import { Loading02, XClose } from '@/app/components/base/icons/src/vender/line/general' +import cn from 'classnames' +import { + Loading02, + XClose, +} from '@/app/components/base/icons/src/vender/line/general' import { RefreshCcw01 } from '@/app/components/base/icons/src/vender/line/arrows' import { AlertTriangle } from '@/app/components/base/icons/src/vender/solid/alertsAndFeedback' import TooltipPlus from '@/app/components/base/tooltip-plus' @@ -30,7 +34,11 @@ const ImageList: FC = ({ const [imagePreviewUrl, setImagePreviewUrl] = useState('') const handleImageLinkLoadSuccess = (item: ImageFile) => { - if (item.type === TransferMethod.remote_url && onImageLinkLoadSuccess && item.progress !== -1) + if ( + item.type === TransferMethod.remote_url + && onImageLinkLoadSuccess + && item.progress !== -1 + ) onImageLinkLoadSuccess(item._id) } const handleImageLinkLoadError = (item: ImageFile) => { @@ -39,89 +47,95 @@ const ImageList: FC = ({ } return ( -
- { - list.map(item => ( -
- { - item.type === TransferMethod.local_file && item.progress !== 100 && ( - <> -
-1 ? `${item.progress}%` : 0 }} - > - { - item.progress === -1 && ( - onReUpload && onReUpload(item._id)} /> - ) - } -
- { - item.progress > -1 && ( - {item.progress}% - ) - } - - ) - } - { - item.type === TransferMethod.remote_url && item.progress !== 100 && ( -
+ {list.map(item => ( +
+ {item.type === TransferMethod.local_file && item.progress !== 100 && ( + <> +
-1 ? `${item.progress}%` : 0 }} + > + {item.progress === -1 && ( + onReUpload && onReUpload(item._id)} + /> + )} +
+ {item.progress > -1 && ( + + {item.progress}% + + )} + + )} + {item.type === TransferMethod.remote_url && item.progress !== 100 && ( +
- { - item.progress > -1 && ( - - ) - } - { - item.progress === -1 && ( - - - - ) - } -
- ) + ${ + item.progress === -1 + ? 'bg-[#FEF0C7] border-[#DC6803]' + : 'bg-black/[0.16] border-transparent' } - handleImageLinkLoadSuccess(item)} - onError={() => handleImageLinkLoadError(item)} - src={item.type === TransferMethod.remote_url ? item.url : item.base64Url} - onClick={() => item.progress === 100 && setImagePreviewUrl((item.type === TransferMethod.remote_url ? item.url : item.base64Url) as string)} - /> - { - !readonly && ( -
onRemove && onRemove(item._id)} + `} + > + {item.progress > -1 && ( + + )} + {item.progress === -1 && ( + - -
+ + + )} +
+ )} + {item.file?.name} handleImageLinkLoadSuccess(item)} + onError={() => handleImageLinkLoadError(item)} + src={ + item.type === TransferMethod.remote_url + ? item.url + : item.base64Url + } + onClick={() => + item.progress === 100 + && setImagePreviewUrl( + (item.type === TransferMethod.remote_url + ? item.url + : item.base64Url) as string, ) } -
- )) - } - { - imagePreviewUrl && ( - setImagePreviewUrl('')} /> - ) - } + {!readonly && ( + + )} +
+ ))} + {imagePreviewUrl && ( + setImagePreviewUrl('')} + /> + )}
) } diff --git a/web/app/components/base/image-uploader/uploader.tsx b/web/app/components/base/image-uploader/uploader.tsx index f43c24c3f6e130..c6f5e707ebe45a 100644 --- a/web/app/components/base/image-uploader/uploader.tsx +++ b/web/app/components/base/image-uploader/uploader.tsx @@ -7,6 +7,7 @@ import { ALLOW_FILE_EXTENSIONS } from '@/types/app' type UploaderProps = { children: (hovering: boolean) => JSX.Element onUpload: (imageFile: ImageFile) => void + closePopover?: () => void limit?: number disabled?: boolean } @@ -14,11 +15,16 @@ type UploaderProps = { const Uploader: FC = ({ children, onUpload, + closePopover, limit, disabled, }) => { const [hovering, setHovering] = useState(false) - const { handleLocalFileUpload } = useLocalFileUploader({ limit, onUpload, disabled }) + const { handleLocalFileUpload } = useLocalFileUploader({ + limit, + onUpload, + disabled, + }) const handleChange = (e: ChangeEvent) => { const file = e.target.files?.[0] @@ -27,6 +33,7 @@ const Uploader: FC = ({ return handleLocalFileUpload(file) + closePopover?.() } return ( @@ -37,11 +44,8 @@ const Uploader: FC = ({ > {children(hovering)} (e.target as HTMLInputElement).value = ''} + className='absolute block inset-0 opacity-0 text-[0] w-full disabled:cursor-not-allowed cursor-pointer' + onClick={e => ((e.target as HTMLInputElement).value = '')} type='file' accept={ALLOW_FILE_EXTENSIONS.map(ext => `.${ext}`).join(',')} onChange={handleChange}