'use client' import type { FC } from 'react' import React, { useCallback, useState } from 'react' import Modal from '../../../base/modal' import type { BuiltInMetadataItem, MetadataItemInBatchEdit } from '../types' import { type MetadataItemWithEdit, UpdateType } from '../types' import EditMetadataBatchItem from './edit-row' import AddedMetadataItem from './add-row' import Button from '../../../base/button' import { useTranslation } from 'react-i18next' import Checkbox from '../../../base/checkbox' import Tooltip from '../../../base/tooltip' import SelectMetadataModal from '../metadata-dataset/select-metadata-modal' import { RiQuestionLine } from '@remixicon/react' import Divider from '@/app/components/base/divider' import AddMetadataButton from '../add-metadata-button' import produce from 'immer' import useCheckMetadataName from '../hooks/use-check-metadata-name' import Toast from '@/app/components/base/toast' import { useCreateMetaData } from '@/service/knowledge/use-metadata' const i18nPrefix = 'dataset.metadata.batchEditMetadata' type Props = { datasetId: string, documentNum: number list: MetadataItemInBatchEdit[] onSave: (editedList: MetadataItemInBatchEdit[], addedList: MetadataItemInBatchEdit[], isApplyToAllSelectDocument: boolean) => void onHide: () => void onShowManage: () => void } const EditMetadataBatchModal: FC = ({ datasetId, documentNum, list, onSave, onHide, onShowManage, }) => { const { t } = useTranslation() const [templeList, setTempleList] = useState(list) const handleTemplesChange = useCallback((payload: MetadataItemWithEdit) => { const newTempleList = produce(templeList, (draft) => { const index = draft.findIndex(i => i.id === payload.id) if (index !== -1) { draft[index] = payload draft[index].isUpdated = true draft[index].updateType = UpdateType.changeValue } }, ) setTempleList(newTempleList) }, [templeList]) const handleTempleItemRemove = useCallback((id: string) => { const newTempleList = produce(templeList, (draft) => { const index = draft.findIndex(i => i.id === id) if (index !== -1) { draft[index].isUpdated = true draft[index].updateType = UpdateType.delete } }) setTempleList(newTempleList) }, [templeList]) const handleItemReset = useCallback((id: string) => { const newTempleList = produce(templeList, (draft) => { const index = draft.findIndex(i => i.id === id) if (index !== -1) { draft[index] = { ...list[index] } draft[index].isUpdated = false delete draft[index].updateType } }) setTempleList(newTempleList) }, [list, templeList]) const { checkName } = useCheckMetadataName() const { mutate: doAddMetaData } = useCreateMetaData(datasetId) const handleAddMetaData = useCallback(async (payload: BuiltInMetadataItem) => { const errorMsg = checkName(payload.name).errorMsg if (errorMsg) { Toast.notify({ message: errorMsg, type: 'error', }) return Promise.reject(new Error(errorMsg)) } await doAddMetaData(payload) Toast.notify({ type: 'success', message: t('common.api.actionSuccess'), }) }, [checkName, doAddMetaData, t]) const [addedList, setAddedList] = useState([]) const handleAddedListChange = useCallback((payload: MetadataItemWithEdit) => { const newAddedList = addedList.map(i => i.id === payload.id ? payload : i) setAddedList(newAddedList) }, [addedList]) const handleAddedItemRemove = useCallback((removeIndex: number) => { return () => { const newAddedList = addedList.filter((i, index) => index !== removeIndex) setAddedList(newAddedList) } }, [addedList]) const [isApplyToAllSelectDocument, setIsApplyToAllSelectDocument] = useState(false) const handleSave = useCallback(() => { onSave(templeList.filter(item => item.updateType !== UpdateType.delete), addedList, isApplyToAllSelectDocument) }, [templeList, addedList, isApplyToAllSelectDocument, onSave]) return (
{t(`${i18nPrefix}.editDocumentsNum`, { num: documentNum })}
{templeList.map(item => ( ))}
{t('dataset.metadata.createMetadata.title')}
{addedList.map((item, i) => ( ))}
} onSave={handleAddMetaData} onSelect={data => setAddedList([...addedList, data as MetadataItemWithEdit])} onManage={onShowManage} />
setIsApplyToAllSelectDocument(!isApplyToAllSelectDocument)} />
{t(`${i18nPrefix}.applyToAllSelectDocument`)}
{t(`${i18nPrefix}.applyToAllSelectDocumentTip`)}
} >
) } export default React.memo(EditMetadataBatchModal)