'use client' import { useContext } from 'use-context-selector' import { useRouter } from 'next/navigation' import { useCallback, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { RiMoreFill } from '@remixicon/react' import cn from '@/utils/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 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' import CornerLabel from '@/app/components/base/corner-label' import { useAppContext } from '@/context/app-context' 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) { } setShowConfirmDelete(false) }, [dataset.id, notify, onSuccess, t]) const Operations = (props: HtmlContentProps & { showDelete: boolean }) => { const onMouseLeave = async () => { props.onClose?.() } const onClickRename = async (e: React.MouseEvent) => { e.stopPropagation() props.onClick?.() e.preventDefault() setShowRenameModal(true) } const onClickDelete = async (e: React.MouseEvent) => { e.stopPropagation() props.onClick?.() e.preventDefault() detectIsUsedByApp() } return (
{t('common.operation.settings')}
{props.showDelete && ( <>
{t('common.operation.delete')}
)}
) } useEffect(() => { setTags(dataset.tags) }, [dataset]) return ( <>
{ e.preventDefault() isExternalProvider(dataset.provider) ? push(`/datasets/${dataset.id}/hitTesting`) : push(`/datasets/${dataset.id}/documents`) }} > {isExternalProvider(dataset.provider) && }
{dataset.name}
{!dataset.embedding_available && ( {t('dataset.unavailable')} )}
{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')} }
{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