// DatasetCard.js import React, { useContext, useCallback, useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { RiMoreFill } from '@remixicon/react'; import cn from 'classnames'; import Confirm from '@/app/components/base/confirm'; import { ToastContext } from '@/app/components/base/toast'; import { checkIsUsedInApp, deleteDataset } from '@/service/datasets'; import type { DataSet } from '@/models/datasets'; import Tooltip from '@/app/components/base/tooltip'; import { Folder } from '@/app/components/base/icons/src/vender/solid/files'; import CustomPopover from '@/app/components/base/popover'; import Divider from '@/app/components/base/divider'; import RenameDatasetModal from '@/app/components/datasets/rename-modal'; import TagSelector from '@/app/components/base/tag-management/selector'; import CornerLabel from '@/app/components/base/corner-label'; import { useAppContext } from '@/context/app-context'; import { useRouter } from 'next/navigation'; export type DatasetCardProps = { dataset: DataSet, onSuccess?: () => void, }; const DatasetCard = ({ dataset, onSuccess }: DatasetCardProps) => { const { t } = useTranslation(); const { notify } = useContext(ToastContext); const { push } = useRouter(); const EXTERNAL_PROVIDER = 'external' as const; const { isCurrentWorkspaceDatasetOperator } = useAppContext(); const [tags, setTags] = useState(dataset.tags); const [showRenameModal, setShowRenameModal] = useState(false); const [showConfirmDelete, setShowConfirmDelete] = useState(false); const [confirmMessage, setConfirmMessage] = useState(''); const isExternalProvider = (provider: string): boolean => provider === EXTERNAL_PROVIDER; 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]); const onConfirmDelete = useCallback(async () => { try { await deleteDataset(dataset.id); notify({ type: 'success', message: t('dataset.datasetDeleted') }); if (onSuccess) onSuccess(); } catch (e: any) { // Handle errors here } setShowConfirmDelete(false); }, [dataset.id, notify, onSuccess, t]); const Operations = ({ showDelete, ...props }) => { const onMouseLeave = async () => { props.onClose?.(); }; const onClickRename = (e) => { e.stopPropagation(); props.onClick?.(); e.preventDefault(); setShowRenameModal(true); }; const onClickDelete = (e) => { e.stopPropagation(); props.onClick?.(); e.preventDefault(); detectIsUsedByApp(); }; return (
{t('common.operation.settings')}
{showDelete && ( <>
{t('common.operation.delete')}
)}
); }; useEffect(() => { setTags(dataset.tags); }, [dataset]); return ( <>
{ e.preventDefault(); isExternalProvider(dataset.provider) ? push(`/database/${dataset.id}/hitTesting`) : push(`/database/${dataset.id}/documents`); }} > {isExternalProvider(dataset.provider) && ( )}
{dataset.name}
{!dataset.embedding_available && ( {t('dataset.unavailable')} )}
{dataset.description}
{ e.stopPropagation(); e.preventDefault(); }}>
tag.id)} selectedTags={tags} onCacheUpdate={setTags} onChange={onSuccess} />
} position="br" trigger="click" btnElement={
} 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'} />
{showRenameModal && ( setShowRenameModal(false)} onSuccess={onSuccess} /> )} {showConfirmDelete && ( setShowConfirmDelete(false)} /> )} ); }; export default DatasetCard;