DatasetCard.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. 'use client'
  2. import { useContext } from 'use-context-selector'
  3. import { useRouter } from 'next/navigation'
  4. import { useCallback, useEffect, useState } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import { RiMoreFill } from '@remixicon/react'
  7. import cn from '@/utils/classnames'
  8. import Confirm from '@/app/components/base/confirm'
  9. import { ToastContext } from '@/app/components/base/toast'
  10. import { checkIsUsedInApp, deleteDataset } from '@/service/datasets'
  11. import type { DataSet } from '@/models/datasets'
  12. import Tooltip from '@/app/components/base/tooltip'
  13. import { Folder } from '@/app/components/base/icons/src/vender/solid/files'
  14. import type { HtmlContentProps } from '@/app/components/base/popover'
  15. import CustomPopover from '@/app/components/base/popover'
  16. import Divider from '@/app/components/base/divider'
  17. import RenameDatasetModal from '@/app/components/datasets/rename-modal'
  18. import type { Tag } from '@/app/components/base/tag-management/constant'
  19. import TagSelector from '@/app/components/base/tag-management/selector'
  20. import CornerLabel from '@/app/components/base/corner-label'
  21. import { useAppContext } from '@/context/app-context'
  22. export type DatasetCardProps = {
  23. dataset: DataSet
  24. onSuccess?: () => void
  25. }
  26. const DatasetCard = ({
  27. dataset,
  28. onSuccess,
  29. }: DatasetCardProps) => {
  30. const { t } = useTranslation()
  31. const { notify } = useContext(ToastContext)
  32. const { push } = useRouter()
  33. const EXTERNAL_PROVIDER = 'external' as const
  34. const { isCurrentWorkspaceDatasetOperator } = useAppContext()
  35. const [tags, setTags] = useState<Tag[]>(dataset.tags)
  36. const [showRenameModal, setShowRenameModal] = useState(false)
  37. const [showConfirmDelete, setShowConfirmDelete] = useState(false)
  38. const [confirmMessage, setConfirmMessage] = useState<string>('')
  39. const isExternalProvider = (provider: string): boolean => provider === EXTERNAL_PROVIDER
  40. const detectIsUsedByApp = useCallback(async () => {
  41. try {
  42. const { is_using: isUsedByApp } = await checkIsUsedInApp(dataset.id)
  43. setConfirmMessage(isUsedByApp ? t('dataset.datasetUsedByApp')! : t('dataset.deleteDatasetConfirmContent')!)
  44. }
  45. catch (e: any) {
  46. const res = await e.json()
  47. notify({ type: 'error', message: res?.message || 'Unknown error' })
  48. }
  49. setShowConfirmDelete(true)
  50. }, [dataset.id, notify, t])
  51. const onConfirmDelete = useCallback(async () => {
  52. try {
  53. await deleteDataset(dataset.id)
  54. notify({ type: 'success', message: t('dataset.datasetDeleted') })
  55. if (onSuccess)
  56. onSuccess()
  57. }
  58. catch (e: any) {
  59. }
  60. setShowConfirmDelete(false)
  61. }, [dataset.id, notify, onSuccess, t])
  62. const Operations = (props: HtmlContentProps & { showDelete: boolean }) => {
  63. const onMouseLeave = async () => {
  64. props.onClose?.()
  65. }
  66. const onClickRename = async (e: React.MouseEvent<HTMLDivElement>) => {
  67. e.stopPropagation()
  68. props.onClick?.()
  69. e.preventDefault()
  70. setShowRenameModal(true)
  71. }
  72. const onClickDelete = async (e: React.MouseEvent<HTMLDivElement>) => {
  73. e.stopPropagation()
  74. props.onClick?.()
  75. e.preventDefault()
  76. detectIsUsedByApp()
  77. }
  78. return (
  79. <div className="relative w-full py-1" onMouseLeave={onMouseLeave}>
  80. <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}>
  81. <span className='text-gray-700 text-sm'>{t('common.operation.settings')}</span>
  82. </div>
  83. {props.showDelete && (
  84. <>
  85. <Divider className="!my-1" />
  86. <div
  87. className='group h-8 py-[6px] px-3 mx-1 flex items-center gap-2 hover:bg-red-50 rounded-lg cursor-pointer'
  88. onClick={onClickDelete}
  89. >
  90. <span className={cn('text-gray-700 text-sm', 'group-hover:text-red-500')}>
  91. {t('common.operation.delete')}
  92. </span>
  93. </div>
  94. </>
  95. )}
  96. </div>
  97. )
  98. }
  99. useEffect(() => {
  100. setTags(dataset.tags)
  101. }, [dataset])
  102. return (
  103. <>
  104. <div
  105. className='group relative col-span-1 bg-white border-[0.5px] border-solid border-transparent rounded-xl shadow-sm min-h-[160px] flex flex-col transition-all duration-200 ease-in-out cursor-pointer hover:shadow-lg'
  106. data-disable-nprogress={true}
  107. onClick={(e) => {
  108. e.preventDefault()
  109. isExternalProvider(dataset.provider)
  110. ? push(`/datasets/${dataset.id}/hitTesting`)
  111. : push(`/datasets/${dataset.id}/documents`)
  112. }}
  113. >
  114. {isExternalProvider(dataset.provider) && <CornerLabel label='External' className='absolute right-0' labelClassName='rounded-tr-xl' />}
  115. <div className='flex pt-[14px] px-[14px] pb-3 h-[66px] items-center gap-3 grow-0 shrink-0'>
  116. <div className={cn(
  117. 'shrink-0 flex items-center justify-center p-2.5 bg-[#F5F8FF] rounded-md border-[0.5px] border-[#E0EAFF]',
  118. !dataset.embedding_available && 'opacity-50 hover:opacity-100',
  119. )}>
  120. <Folder className='w-5 h-5 text-[#444CE7]' />
  121. </div>
  122. <div className='grow w-0 py-[1px]'>
  123. <div className='flex items-center text-sm leading-5 font-semibold text-gray-800'>
  124. <div className={cn('truncate', !dataset.embedding_available && 'opacity-50 hover:opacity-100')} title={dataset.name}>{dataset.name}</div>
  125. {!dataset.embedding_available && (
  126. <Tooltip
  127. popupContent={t('dataset.unavailableTip')}
  128. >
  129. <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>
  130. </Tooltip>
  131. )}
  132. </div>
  133. {/* <div className='flex items-center mt-[1px] text-xs leading-[18px] text-gray-500'>
  134. <div
  135. className={cn('truncate', (!dataset.embedding_available || !dataset.document_count) && 'opacity-50')}
  136. 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')}`}
  137. >
  138. {dataset.provider === 'external'
  139. ? <>
  140. <span>{dataset.app_count}{t('dataset.appCount')}</span>
  141. </>
  142. : <>
  143. <span>{dataset.document_count}{t('dataset.documentCount')}</span>
  144. <span className='shrink-0 mx-0.5 w-1 text-gray-400'>·</span>
  145. <span>{Math.round(dataset.word_count / 1000)}{t('dataset.wordCount')}</span>
  146. <span className='shrink-0 mx-0.5 w-1 text-gray-400'>·</span>
  147. <span>{dataset.app_count}{t('dataset.appCount')}</span>
  148. </>
  149. }
  150. </div>
  151. </div> */}
  152. </div>
  153. </div>
  154. <div
  155. className={cn(
  156. 'grow mb-2 px-[14px] max-h-[72px] text-xs leading-normal text-gray-500 group-hover:line-clamp-2 group-hover:max-h-[36px]',
  157. tags.length ? 'line-clamp-2' : 'line-clamp-4',
  158. !dataset.embedding_available && 'opacity-50 hover:opacity-100',
  159. )}
  160. title={dataset.description}>
  161. {dataset.description}
  162. </div>
  163. <div className={cn(
  164. 'items-center shrink-0 mt-1 pt-1 pl-[14px] pr-[6px] pb-[6px] h-[42px]',
  165. tags.length ? 'flex' : '!hidden group-hover:!flex',
  166. )}>
  167. <div className={cn('grow flex items-center gap-1 w-0', !dataset.embedding_available && 'opacity-50 hover:opacity-100')} onClick={(e) => {
  168. e.stopPropagation()
  169. e.preventDefault()
  170. }}>
  171. <div className={cn(
  172. 'group-hover:!block group-hover:!mr-0 mr-[41px] grow w-full',
  173. tags.length ? '!block' : '!hidden',
  174. )}>
  175. <TagSelector
  176. position='bl'
  177. type='knowledge'
  178. targetID={dataset.id}
  179. value={tags.map(tag => tag.id)}
  180. selectedTags={tags}
  181. onCacheUpdate={setTags}
  182. onChange={onSuccess}
  183. />
  184. </div>
  185. </div>
  186. <div className='!hidden group-hover:!flex shrink-0 mx-1 w-[1px] h-[14px] bg-gray-200' />
  187. <div className='!hidden group-hover:!flex shrink-0'>
  188. <CustomPopover
  189. htmlContent={<Operations showDelete={!isCurrentWorkspaceDatasetOperator} />}
  190. position="br"
  191. trigger="click"
  192. btnElement={
  193. <div
  194. className='flex items-center justify-center w-8 h-8 cursor-pointer rounded-md'
  195. >
  196. <RiMoreFill className='w-4 h-4 text-gray-700' />
  197. </div>
  198. }
  199. btnClassName={open =>
  200. cn(
  201. open ? '!bg-black/5 !shadow-none' : '!bg-transparent',
  202. 'h-8 w-8 !p-2 rounded-md border-none hover:!bg-black/5',
  203. )
  204. }
  205. className={'!w-[128px] h-fit !z-20'}
  206. />
  207. </div>
  208. </div>
  209. </div>
  210. {showRenameModal && (
  211. <RenameDatasetModal
  212. show={showRenameModal}
  213. dataset={dataset}
  214. onClose={() => setShowRenameModal(false)}
  215. onSuccess={onSuccess}
  216. />
  217. )}
  218. {showConfirmDelete && (
  219. <Confirm
  220. title={t('dataset.deleteDatasetConfirmTitle')}
  221. content={confirmMessage}
  222. isShow={showConfirmDelete}
  223. onConfirm={onConfirmDelete}
  224. onCancel={() => setShowConfirmDelete(false)}
  225. />
  226. )}
  227. </>
  228. )
  229. }
  230. export default DatasetCard