| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 23:19:36 +08:00
										 |  |  | import { useContext } from 'use-context-selector' | 
					
						
							| 
									
										
										
										
											2024-08-05 12:51:26 +08:00
										 |  |  | import { useRouter } from 'next/navigation' | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | import { useCallback, useEffect, useState } from 'react' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							| 
									
										
										
										
											2024-08-26 13:00:02 +08:00
										 |  |  | import { RiMoreFill } from '@remixicon/react' | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import Confirm from '@/app/components/base/confirm' | 
					
						
							|  |  |  | import { ToastContext } from '@/app/components/base/toast' | 
					
						
							| 
									
										
										
										
											2024-07-01 16:14:49 +08:00
										 |  |  | import { checkIsUsedInApp, deleteDataset } from '@/service/datasets' | 
					
						
							| 
									
										
										
										
											2023-06-01 23:19:36 +08:00
										 |  |  | import type { DataSet } from '@/models/datasets' | 
					
						
							| 
									
										
										
										
											2023-08-18 17:37:31 +08:00
										 |  |  | import Tooltip from '@/app/components/base/tooltip' | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  | import { Folder } from '@/app/components/base/icons/src/vender/solid/files' | 
					
						
							|  |  |  | import type { HtmlContentProps } from '@/app/components/base/popover' | 
					
						
							|  |  |  | import CustomPopover from '@/app/components/base/popover' | 
					
						
							|  |  |  | import Divider from '@/app/components/base/divider' | 
					
						
							|  |  |  | import RenameDatasetModal from '@/app/components/datasets/rename-modal' | 
					
						
							|  |  |  | import type { Tag } from '@/app/components/base/tag-management/constant' | 
					
						
							|  |  |  | import TagSelector from '@/app/components/base/tag-management/selector' | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  | import CornerLabel from '@/app/components/base/corner-label' | 
					
						
							| 
									
										
										
										
											2024-07-09 17:47:54 +08:00
										 |  |  | import { useAppContext } from '@/context/app-context' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export type DatasetCardProps = { | 
					
						
							|  |  |  |   dataset: DataSet | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |   onSuccess?: () => void | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const DatasetCard = ({ | 
					
						
							|  |  |  |   dataset, | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |   onSuccess, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | }: DatasetCardProps) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const { notify } = useContext(ToastContext) | 
					
						
							| 
									
										
										
										
											2024-08-05 12:51:26 +08:00
										 |  |  |   const { push } = useRouter() | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  |   const EXTERNAL_PROVIDER = 'external' as const | 
					
						
							| 
									
										
										
										
											2024-08-05 12:51:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-09 17:47:54 +08:00
										 |  |  |   const { isCurrentWorkspaceDatasetOperator } = useAppContext() | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |   const [tags, setTags] = useState<Tag[]>(dataset.tags) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |   const [showRenameModal, setShowRenameModal] = useState(false) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   const [showConfirmDelete, setShowConfirmDelete] = useState(false) | 
					
						
							| 
									
										
										
										
											2024-07-01 16:14:49 +08:00
										 |  |  |   const [confirmMessage, setConfirmMessage] = useState<string>('') | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  |   const isExternalProvider = (provider: string): boolean => provider === EXTERNAL_PROVIDER | 
					
						
							| 
									
										
										
										
											2024-07-01 16:14:49 +08:00
										 |  |  |   const detectIsUsedByApp = useCallback(async () => { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const { is_using: isUsedByApp } = await checkIsUsedInApp(dataset.id) | 
					
						
							|  |  |  |       setConfirmMessage(isUsedByApp ? t('dataset.datasetUsedByApp')! : t('dataset.deleteDatasetConfirmContent')!) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (e: any) { | 
					
						
							|  |  |  |       const res = await e.json() | 
					
						
							|  |  |  |       notify({ type: 'error', message: res?.message || 'Unknown error' }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setShowConfirmDelete(true) | 
					
						
							|  |  |  |   }, [dataset.id, notify, t]) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   const onConfirmDelete = useCallback(async () => { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       await deleteDataset(dataset.id) | 
					
						
							|  |  |  |       notify({ type: 'success', message: t('dataset.datasetDeleted') }) | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |       if (onSuccess) | 
					
						
							|  |  |  |         onSuccess() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     catch (e: any) { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     setShowConfirmDelete(false) | 
					
						
							| 
									
										
										
										
											2024-07-01 16:14:49 +08:00
										 |  |  |   }, [dataset.id, notify, onSuccess, t]) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-09 17:47:54 +08:00
										 |  |  |   const Operations = (props: HtmlContentProps & { showDelete: boolean }) => { | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |     const onMouseLeave = async () => { | 
					
						
							|  |  |  |       props.onClose?.() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const onClickRename = async (e: React.MouseEvent<HTMLDivElement>) => { | 
					
						
							|  |  |  |       e.stopPropagation() | 
					
						
							|  |  |  |       props.onClick?.() | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							|  |  |  |       setShowRenameModal(true) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const onClickDelete = async (e: React.MouseEvent<HTMLDivElement>) => { | 
					
						
							|  |  |  |       e.stopPropagation() | 
					
						
							|  |  |  |       props.onClick?.() | 
					
						
							|  |  |  |       e.preventDefault() | 
					
						
							| 
									
										
										
										
											2024-07-01 16:14:49 +08:00
										 |  |  |       detectIsUsedByApp() | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return ( | 
					
						
							|  |  |  |       <div className="relative w-full py-1" onMouseLeave={onMouseLeave}> | 
					
						
							|  |  |  |         <div className='h-8 py-[6px] px-3 mx-1 flex items-center gap-2 hover:bg-gray-100 rounded-lg cursor-pointer' onClick={onClickRename}> | 
					
						
							|  |  |  |           <span className='text-gray-700 text-sm'>{t('common.operation.settings')}</span> | 
					
						
							|  |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2024-07-09 17:47:54 +08:00
										 |  |  |         {props.showDelete && ( | 
					
						
							|  |  |  |           <> | 
					
						
							|  |  |  |             <Divider className="!my-1" /> | 
					
						
							|  |  |  |             <div | 
					
						
							|  |  |  |               className='group h-8 py-[6px] px-3 mx-1 flex items-center gap-2 hover:bg-red-50 rounded-lg cursor-pointer' | 
					
						
							|  |  |  |               onClick={onClickDelete} | 
					
						
							|  |  |  |             > | 
					
						
							|  |  |  |               <span className={cn('text-gray-700 text-sm', 'group-hover:text-red-500')}> | 
					
						
							|  |  |  |                 {t('common.operation.delete')} | 
					
						
							|  |  |  |               </span> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           </> | 
					
						
							|  |  |  |         )} | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |       </div> | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     setTags(dataset.tags) | 
					
						
							|  |  |  |   }, [dataset]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   return ( | 
					
						
							|  |  |  |     <> | 
					
						
							| 
									
										
										
										
											2024-08-05 12:51:26 +08:00
										 |  |  |       <div | 
					
						
							| 
									
										
										
										
											2024-12-26 12:01:51 +08:00
										 |  |  |         className='group relative col-span-1 bg-components-card-bg border-[0.5px] border-solid border-components-card-border rounded-xl shadow-sm min-h-[160px] flex flex-col transition-all duration-200 ease-in-out cursor-pointer hover:shadow-lg' | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |         data-disable-nprogress={true} | 
					
						
							| 
									
										
										
										
											2024-08-05 12:51:26 +08:00
										 |  |  |         onClick={(e) => { | 
					
						
							|  |  |  |           e.preventDefault() | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  |           isExternalProvider(dataset.provider) | 
					
						
							|  |  |  |             ? push(`/datasets/${dataset.id}/hitTesting`) | 
					
						
							|  |  |  |             : push(`/datasets/${dataset.id}/documents`) | 
					
						
							| 
									
										
										
										
											2024-08-05 12:51:26 +08:00
										 |  |  |         }} | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |       > | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  |         {isExternalProvider(dataset.provider) && <CornerLabel label='External' className='absolute right-0' labelClassName='rounded-tr-xl' />} | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |         <div className='flex pt-[14px] px-[14px] pb-3 h-[66px] items-center gap-3 grow-0 shrink-0'> | 
					
						
							|  |  |  |           <div className={cn( | 
					
						
							|  |  |  |             'shrink-0 flex items-center justify-center p-2.5 bg-[#F5F8FF] rounded-md border-[0.5px] border-[#E0EAFF]', | 
					
						
							|  |  |  |             !dataset.embedding_available && 'opacity-50 hover:opacity-100', | 
					
						
							|  |  |  |           )}> | 
					
						
							|  |  |  |             <Folder className='w-5 h-5 text-[#444CE7]' /> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |           <div className='grow w-0 py-[1px]'> | 
					
						
							| 
									
										
										
										
											2024-12-26 12:01:51 +08:00
										 |  |  |             <div className='flex items-center text-sm leading-5 font-semibold text-text-secondary'> | 
					
						
							|  |  |  |               <div className={cn('truncate', !dataset.embedding_available && 'opacity-50 hover:opacity-100 text-text-tertiary')} title={dataset.name}>{dataset.name}</div> | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |               {!dataset.embedding_available && ( | 
					
						
							|  |  |  |                 <Tooltip | 
					
						
							| 
									
										
										
										
											2024-08-26 13:00:02 +08:00
										 |  |  |                   popupContent={t('dataset.unavailableTip')} | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |                 > | 
					
						
							| 
									
										
										
										
											2024-08-26 13:00:02 +08:00
										 |  |  |                   <span className='shrink-0 inline-flex w-max ml-1 px-1 border border-gray-200 rounded-md text-gray-500 text-xs font-normal leading-[18px]'>{t('dataset.unavailable')}</span> | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |                 </Tooltip> | 
					
						
							|  |  |  |               )} | 
					
						
							|  |  |  |             </div> | 
					
						
							| 
									
										
										
										
											2024-12-26 12:01:51 +08:00
										 |  |  |             <div className='flex items-center mt-[1px] text-xs leading-[18px] text-text-tertiary'> | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |               <div | 
					
						
							|  |  |  |                 className={cn('truncate', (!dataset.embedding_available || !dataset.document_count) && 'opacity-50')} | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  |                 title={dataset.provider === 'external' ? `${dataset.app_count}${t('dataset.appCount')}` : `${dataset.document_count}${t('dataset.documentCount')} · ${Math.round(dataset.word_count / 1000)}${t('dataset.wordCount')} · ${dataset.app_count}${t('dataset.appCount')}`} | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |               > | 
					
						
							| 
									
										
										
										
											2024-09-30 15:38:43 +08:00
										 |  |  |                 {dataset.provider === 'external' | 
					
						
							|  |  |  |                   ? <> | 
					
						
							|  |  |  |                     <span>{dataset.app_count}{t('dataset.appCount')}</span> | 
					
						
							|  |  |  |                   </> | 
					
						
							|  |  |  |                   : <> | 
					
						
							|  |  |  |                     <span>{dataset.document_count}{t('dataset.documentCount')}</span> | 
					
						
							|  |  |  |                     <span className='shrink-0 mx-0.5 w-1 text-gray-400'>·</span> | 
					
						
							|  |  |  |                     <span>{Math.round(dataset.word_count / 1000)}{t('dataset.wordCount')}</span> | 
					
						
							|  |  |  |                     <span className='shrink-0 mx-0.5 w-1 text-gray-400'>·</span> | 
					
						
							|  |  |  |                     <span>{dataset.app_count}{t('dataset.appCount')}</span> | 
					
						
							|  |  |  |                   </> | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |               </div> | 
					
						
							| 
									
										
										
										
											2023-08-18 17:37:31 +08:00
										 |  |  |             </div> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |           </div> | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |         </div> | 
					
						
							|  |  |  |         <div | 
					
						
							|  |  |  |           className={cn( | 
					
						
							| 
									
										
										
										
											2024-12-26 12:01:51 +08:00
										 |  |  |             'grow mb-2 px-[14px] max-h-[72px] text-xs leading-normal text-text-tertiary group-hover:line-clamp-2 group-hover:max-h-[36px]', | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |             tags.length ? 'line-clamp-2' : 'line-clamp-4', | 
					
						
							|  |  |  |             !dataset.embedding_available && 'opacity-50 hover:opacity-100', | 
					
						
							| 
									
										
										
										
											2023-08-18 17:37:31 +08:00
										 |  |  |           )} | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |           title={dataset.description}> | 
					
						
							|  |  |  |           {dataset.description} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |         <div className={cn( | 
					
						
							|  |  |  |           'items-center shrink-0 mt-1 pt-1 pl-[14px] pr-[6px] pb-[6px] h-[42px]', | 
					
						
							|  |  |  |           tags.length ? 'flex' : '!hidden group-hover:!flex', | 
					
						
							|  |  |  |         )}> | 
					
						
							|  |  |  |           <div className={cn('grow flex items-center gap-1 w-0', !dataset.embedding_available && 'opacity-50 hover:opacity-100')} onClick={(e) => { | 
					
						
							|  |  |  |             e.stopPropagation() | 
					
						
							|  |  |  |             e.preventDefault() | 
					
						
							|  |  |  |           }}> | 
					
						
							|  |  |  |             <div className={cn( | 
					
						
							|  |  |  |               'group-hover:!block group-hover:!mr-0 mr-[41px] grow w-full', | 
					
						
							|  |  |  |               tags.length ? '!block' : '!hidden', | 
					
						
							|  |  |  |             )}> | 
					
						
							|  |  |  |               <TagSelector | 
					
						
							|  |  |  |                 position='bl' | 
					
						
							|  |  |  |                 type='knowledge' | 
					
						
							|  |  |  |                 targetID={dataset.id} | 
					
						
							|  |  |  |                 value={tags.map(tag => tag.id)} | 
					
						
							|  |  |  |                 selectedTags={tags} | 
					
						
							|  |  |  |                 onCacheUpdate={setTags} | 
					
						
							|  |  |  |                 onChange={onSuccess} | 
					
						
							|  |  |  |               /> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           </div> | 
					
						
							| 
									
										
										
										
											2024-07-01 16:14:49 +08:00
										 |  |  |           <div className='!hidden group-hover:!flex shrink-0 mx-1 w-[1px] h-[14px] bg-gray-200' /> | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |           <div className='!hidden group-hover:!flex shrink-0'> | 
					
						
							|  |  |  |             <CustomPopover | 
					
						
							| 
									
										
										
										
											2024-07-09 17:47:54 +08:00
										 |  |  |               htmlContent={<Operations showDelete={!isCurrentWorkspaceDatasetOperator} />} | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |               position="br" | 
					
						
							|  |  |  |               trigger="click" | 
					
						
							|  |  |  |               btnElement={ | 
					
						
							|  |  |  |                 <div | 
					
						
							|  |  |  |                   className='flex items-center justify-center w-8 h-8 cursor-pointer rounded-md' | 
					
						
							|  |  |  |                 > | 
					
						
							| 
									
										
										
										
											2024-06-20 11:05:08 +08:00
										 |  |  |                   <RiMoreFill className='w-4 h-4 text-gray-700' /> | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |                 </div> | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |               btnClassName={open => | 
					
						
							|  |  |  |                 cn( | 
					
						
							|  |  |  |                   open ? '!bg-black/5 !shadow-none' : '!bg-transparent', | 
					
						
							|  |  |  |                   'h-8 w-8 !p-2 rounded-md border-none hover:!bg-black/5', | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |               className={'!w-[128px] h-fit !z-20'} | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           </div> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2024-08-05 12:51:26 +08:00
										 |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |       {showRenameModal && ( | 
					
						
							|  |  |  |         <RenameDatasetModal | 
					
						
							|  |  |  |           show={showRenameModal} | 
					
						
							|  |  |  |           dataset={dataset} | 
					
						
							|  |  |  |           onClose={() => setShowRenameModal(false)} | 
					
						
							|  |  |  |           onSuccess={onSuccess} | 
					
						
							|  |  |  |         /> | 
					
						
							|  |  |  |       )} | 
					
						
							|  |  |  |       {showConfirmDelete && ( | 
					
						
							|  |  |  |         <Confirm | 
					
						
							|  |  |  |           title={t('dataset.deleteDatasetConfirmTitle')} | 
					
						
							| 
									
										
										
										
											2024-07-01 16:14:49 +08:00
										 |  |  |           content={confirmMessage} | 
					
						
							| 
									
										
										
										
											2024-04-24 15:02:29 +08:00
										 |  |  |           isShow={showConfirmDelete} | 
					
						
							|  |  |  |           onConfirm={onConfirmDelete} | 
					
						
							|  |  |  |           onCancel={() => setShowConfirmDelete(false)} | 
					
						
							|  |  |  |         /> | 
					
						
							|  |  |  |       )} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     </> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default DatasetCard |