| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   memo, | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |   useState, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | } from 'react' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   RiDeleteBinLine, | 
					
						
							|  |  |  |   RiDownloadLine, | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |   RiEyeLine, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | } from '@remixicon/react' | 
					
						
							|  |  |  | import FileTypeIcon from '../file-type-icon' | 
					
						
							|  |  |  | import { | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |   downloadFile, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   fileIsUploaded, | 
					
						
							|  |  |  |   getFileAppearanceType, | 
					
						
							|  |  |  |   getFileExtension, | 
					
						
							|  |  |  | } from '../utils' | 
					
						
							|  |  |  | import FileImageRender from '../file-image-render' | 
					
						
							|  |  |  | import type { FileEntity } from '../types' | 
					
						
							|  |  |  | import ActionButton from '@/app/components/base/action-button' | 
					
						
							|  |  |  | import ProgressCircle from '@/app/components/base/progress-bar/progress-circle' | 
					
						
							|  |  |  | import { formatFileSize } from '@/utils/format' | 
					
						
							|  |  |  | import cn from '@/utils/classnames' | 
					
						
							|  |  |  | import { ReplayLine } from '@/app/components/base/icons/src/vender/other' | 
					
						
							|  |  |  | import { SupportUploadFileTypes } from '@/app/components/workflow/types' | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  | import ImagePreview from '@/app/components/base/image-uploader/image-preview' | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type FileInAttachmentItemProps = { | 
					
						
							|  |  |  |   file: FileEntity | 
					
						
							|  |  |  |   showDeleteAction?: boolean | 
					
						
							|  |  |  |   showDownloadAction?: boolean | 
					
						
							|  |  |  |   onRemove?: (fileId: string) => void | 
					
						
							|  |  |  |   onReUpload?: (fileId: string) => void | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |   canPreview?: boolean | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | const FileInAttachmentItem = ({ | 
					
						
							|  |  |  |   file, | 
					
						
							|  |  |  |   showDeleteAction, | 
					
						
							|  |  |  |   showDownloadAction = true, | 
					
						
							|  |  |  |   onRemove, | 
					
						
							|  |  |  |   onReUpload, | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |   canPreview, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | }: FileInAttachmentItemProps) => { | 
					
						
							| 
									
										
										
										
											2024-11-01 15:45:27 +08:00
										 |  |  |   const { id, name, type, progress, supportFileType, base64Url, url, isRemote } = file | 
					
						
							|  |  |  |   const ext = getFileExtension(name, type, isRemote) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   const isImageFile = supportFileType === SupportUploadFileTypes.image | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |   const [imagePreviewUrl, setImagePreviewUrl] = useState('') | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |     <> | 
					
						
							|  |  |  |       <div className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |         'flex h-12 items-center rounded-lg border-[0.5px] border-components-panel-border bg-components-panel-on-panel-item-bg pr-3 shadow-xs', | 
					
						
							|  |  |  |         progress === -1 && 'border-state-destructive-border bg-state-destructive-hover', | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |       )}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |         <div className='flex h-12 w-12 items-center justify-center'> | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |           { | 
					
						
							|  |  |  |             isImageFile && ( | 
					
						
							|  |  |  |               <FileImageRender | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 className='h-8 w-8' | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |                 imageUrl={base64Url || url || ''} | 
					
						
							|  |  |  |               /> | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             !isImageFile && ( | 
					
						
							|  |  |  |               <FileTypeIcon | 
					
						
							|  |  |  |                 type={getFileAppearanceType(name, type)} | 
					
						
							|  |  |  |                 size='lg' | 
					
						
							|  |  |  |               /> | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |         <div className='mr-1 w-0 grow'> | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |           <div | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             className='system-xs-medium mb-0.5 flex items-center truncate text-text-secondary' | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |             title={file.name} | 
					
						
							|  |  |  |           > | 
					
						
							|  |  |  |             <div className='truncate'>{name}</div> | 
					
						
							|  |  |  |           </div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <div className='system-2xs-medium-uppercase flex items-center text-text-tertiary'> | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |             { | 
					
						
							|  |  |  |               ext && ( | 
					
						
							|  |  |  |                 <span>{ext.toLowerCase()}</span> | 
					
						
							|  |  |  |               ) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               ext && ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <span className='system-2xs-medium mx-1'>•</span> | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |               ) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               !!file.size && ( | 
					
						
							|  |  |  |                 <span>{formatFileSize(file.size)}</span> | 
					
						
							|  |  |  |               ) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |         <div className='flex shrink-0 items-center'> | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |           { | 
					
						
							|  |  |  |             progress >= 0 && !fileIsUploaded(file) && ( | 
					
						
							|  |  |  |               <ProgressCircle | 
					
						
							|  |  |  |                 className='mr-2.5' | 
					
						
							|  |  |  |                 percentage={progress} | 
					
						
							|  |  |  |               /> | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |           { | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |             progress === -1 && ( | 
					
						
							|  |  |  |               <ActionButton | 
					
						
							|  |  |  |                 className='mr-1' | 
					
						
							|  |  |  |                 onClick={() => onReUpload?.(id)} | 
					
						
							|  |  |  |               > | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <ReplayLine className='h-4 w-4 text-text-tertiary' /> | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |               </ActionButton> | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           { | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |             showDeleteAction && ( | 
					
						
							|  |  |  |               <ActionButton onClick={() => onRemove?.(id)}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <RiDeleteBinLine className='h-4 w-4' /> | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |               </ActionButton> | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2024-11-01 15:45:27 +08:00
										 |  |  |           { | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |             canPreview && isImageFile && ( | 
					
						
							|  |  |  |               <ActionButton className='mr-1' onClick={() => setImagePreviewUrl(url || '')}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <RiEyeLine className='h-4 w-4' /> | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |               </ActionButton> | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             showDownloadAction && ( | 
					
						
							|  |  |  |               <ActionButton onClick={(e) => { | 
					
						
							|  |  |  |                 e.stopPropagation() | 
					
						
							|  |  |  |                 downloadFile(url || base64Url || '', name) | 
					
						
							|  |  |  |               }}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <RiDownloadLine className='h-4 w-4' /> | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |               </ActionButton> | 
					
						
							| 
									
										
										
										
											2024-11-01 15:45:27 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |         </div> | 
					
						
							|  |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2024-12-05 16:58:39 +08:00
										 |  |  |       { | 
					
						
							|  |  |  |         imagePreviewUrl && canPreview && ( | 
					
						
							|  |  |  |           <ImagePreview | 
					
						
							|  |  |  |             title={name} | 
					
						
							|  |  |  |             url={imagePreviewUrl} | 
					
						
							|  |  |  |             onCancel={() => setImagePreviewUrl('')} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     </> | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default memo(FileInAttachmentItem) |