| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 'use client' | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  | import React, { useEffect, useState } from 'react' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  | import { XMarkIcon } from '@heroicons/react/20/solid' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import s from './index.module.css' | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2023-08-16 23:14:27 +08:00
										 |  |  | import type { CustomFile as File } from '@/models/datasets' | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  | import { fetchFilePreview } from '@/service/common' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type IProps = { | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |   file?: File | 
					
						
							|  |  |  |   hidePreview: () => void | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const FilePreview = ({ | 
					
						
							|  |  |  |   file, | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |   hidePreview, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | }: IProps) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const [previewContent, setPreviewContent] = useState('') | 
					
						
							|  |  |  |   const [loading, setLoading] = useState(true) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const getPreviewContent = async (fileID: string) => { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const res = await fetchFilePreview({ fileID }) | 
					
						
							|  |  |  |       setPreviewContent(res.content) | 
					
						
							|  |  |  |       setLoading(false) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  |     catch { } | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const getFileName = (currentFile?: File) => { | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |     if (!currentFile) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       return '' | 
					
						
							|  |  |  |     const arr = currentFile.name.split('.') | 
					
						
							|  |  |  |     return arr.slice(0, -1).join() | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							| 
									
										
										
										
											2023-08-16 23:14:27 +08:00
										 |  |  |     if (file?.id) { | 
					
						
							| 
									
										
										
										
											2023-06-21 09:44:01 +08:00
										 |  |  |       setLoading(true) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       getPreviewContent(file.id) | 
					
						
							| 
									
										
										
										
											2023-06-21 09:44:01 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   }, [file]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2024-12-26 12:01:51 +08:00
										 |  |  |     <div className={cn(s.filePreview, 'h-full')}> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       <div className={cn(s.previewHeader)}> | 
					
						
							| 
									
										
										
										
											2025-03-19 11:19:57 +08:00
										 |  |  |         <div className={cn(s.title, 'title-md-semi-bold')}> | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |           <span>{t('datasetCreation.stepOne.filePreview')}</span> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <div className='flex h-6 w-6 cursor-pointer items-center justify-center' onClick={hidePreview}> | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             <XMarkIcon className='h-4 w-4'></XMarkIcon> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2025-03-19 11:19:57 +08:00
										 |  |  |         <div className={cn(s.fileName, 'system-xs-medium')}> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |           <span>{getFileName(file)}</span><span className={cn(s.filetype)}>.{file?.extension}</span> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |       <div className={cn(s.previewContent)}> | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  |         {loading && <div className={cn(s.loading)} />} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         {!loading && ( | 
					
						
							| 
									
										
										
										
											2024-12-26 12:01:51 +08:00
										 |  |  |           <div className={cn(s.fileContent, 'body-md-regular')}>{previewContent}</div> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         )} | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default FilePreview |