DatabaseCard.tsx 8.3 KB

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