dify/web/app/components/datasets/metadata/hooks/use-batch-edit-document-metadata.ts
FFXN 0e320290e1
feat: evaluation (#35353)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: jyong <718720800@qq.com>
Co-authored-by: Yansong Zhang <916125788@qq.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: hj24 <mambahj24@gmail.com>
Co-authored-by: hj24 <huangjian@dify.ai>
Co-authored-by: Joel <iamjoel007@gmail.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
Co-authored-by: CodingOnStar <hanxujiang@dify.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: 非法操作 <hjlarry@163.com>
Co-authored-by: Ayush Baluni <73417844+aayushbaluni@users.noreply.github.com>
Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
Co-authored-by: jimcody1995 <jjimcody@gmail.com>
Co-authored-by: James <63717587+jamesrayammons@users.noreply.github.com>
Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai>
Co-authored-by: Stephen Zhou <hi@hyoban.cc>
Co-authored-by: Coding On Star <447357187@qq.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: jerryzai <jerryzh8710@protonmail.com>
Co-authored-by: NVIDIAN <speedy.hpc@hotmail.com>
Co-authored-by: ai-hpc <ai-hpc@users.noreply.github.com>
Co-authored-by: Asuka Minato <i@asukaminato.eu.org>
Co-authored-by: Junghwan <70629228+shaun0927@users.noreply.github.com>
Co-authored-by: HeYinKazune <70251095+HeYin-OS@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: yyh <yuanyouhuilyz@gmail.com>
Co-authored-by: Jingyi <jingyi.qi@dify.ai>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: sxxtony <166789813+sxxtony@users.noreply.github.com>
2026-04-17 16:37:21 +08:00

131 lines
4.8 KiB
TypeScript

import type { MetadataBatchEditToServer, MetadataItemInBatchEdit, MetadataItemWithEdit, MetadataItemWithValue } from '../types'
import type { SimpleDocumentDetail } from '@/models/datasets'
import { useBoolean } from 'ahooks'
import { t } from 'i18next'
import { useMemo } from 'react'
import { toast } from '@/app/components/base/ui/toast'
import { useBatchUpdateDocMetadata } from '@/service/knowledge/use-metadata'
import { UpdateType } from '../types'
type Props = {
datasetId: string
docList: SimpleDocumentDetail[]
selectedDocumentIds?: string[]
onUpdate: () => void
}
const useBatchEditDocumentMetadata = ({ datasetId, docList, selectedDocumentIds, onUpdate }: Props) => {
const [isShowEditModal, { setTrue: showEditModal, setFalse: hideEditModal }] = useBoolean(false)
const metaDataList: MetadataItemWithValue[][] = (() => {
const res: MetadataItemWithValue[][] = []
docList.forEach((item) => {
if (item.doc_metadata) {
res.push(item.doc_metadata.filter(item => item.id !== 'built-in'))
return
}
res.push([])
})
return res
})()
// To check is key has multiple value
const originalList: MetadataItemInBatchEdit[] = useMemo(() => {
const idNameValue: Record<string, {
value: string | number | null
isMultipleValue: boolean
}> = {}
const res: MetadataItemInBatchEdit[] = []
metaDataList.forEach((metaData) => {
metaData.forEach((item) => {
if (idNameValue[item.id]?.isMultipleValue)
return
const itemInRes = res.find(i => i.id === item.id)
if (!idNameValue[item.id]) {
idNameValue[item.id] = {
value: item.value,
isMultipleValue: false,
}
}
if (itemInRes && itemInRes.value !== item.value) {
idNameValue[item.id]!.isMultipleValue = true
itemInRes.isMultipleValue = true
itemInRes.value = null
return
}
if (!itemInRes) {
res.push({
...item,
isMultipleValue: false,
})
}
})
})
return res
}, [metaDataList])
const toCleanMetadataItem = (item: MetadataItemWithValue | MetadataItemWithEdit | MetadataItemInBatchEdit): MetadataItemWithValue => ({
id: item.id,
name: item.name,
type: item.type,
value: item.value ?? null,
})
const formateToBackendList = (editedList: MetadataItemWithEdit[], addedList: MetadataItemInBatchEdit[], isApplyToAllSelectDocument: boolean) => {
const updatedList = editedList.filter((editedItem) => {
return editedItem.updateType === UpdateType.changeValue
})
const removedList = originalList.filter((originalItem) => {
const editedItem = editedList.find(i => i.id === originalItem.id)
if (!editedItem) // removed item
return true
return false
})
// Use selectedDocumentIds if available, otherwise fall back to docList
const documentIds = selectedDocumentIds || docList.map(doc => doc.id)
const res: MetadataBatchEditToServer = documentIds.map((documentId) => {
// Find the document in docList to get its metadata
const docIndex = docList.findIndex(doc => doc.id === documentId)
const oldMetadataList = docIndex >= 0 ? metaDataList[docIndex] : []
let newMetadataList: MetadataItemWithValue[] = [...(oldMetadataList ?? []), ...addedList]
.filter((item) => {
return !removedList.find(removedItem => removedItem.id === item.id)
})
.map(toCleanMetadataItem)
if (isApplyToAllSelectDocument) {
// add missing metadata item
updatedList.forEach((editedItem) => {
if (!newMetadataList.find(i => i.id === editedItem.id) && !editedItem.isMultipleValue)
newMetadataList.push(toCleanMetadataItem(editedItem))
})
}
newMetadataList = newMetadataList.map((item) => {
const editedItem = updatedList.find(i => i.id === item.id)
if (editedItem)
return toCleanMetadataItem(editedItem)
return item
})
return {
document_id: documentId,
metadata_list: newMetadataList,
partial_update: docIndex < 0,
}
})
return res
}
const { mutateAsync } = useBatchUpdateDocMetadata()
const handleSave = async (editedList: MetadataItemInBatchEdit[], addedList: MetadataItemInBatchEdit[], isApplyToAllSelectDocument: boolean) => {
const backendList = formateToBackendList(editedList, addedList, isApplyToAllSelectDocument)
await mutateAsync({
dataset_id: datasetId,
metadata_list: backendList,
})
onUpdate()
hideEditModal()
toast.success(t('actionMsg.modifiedSuccessfully', { ns: 'common' }))
}
return {
isShowEditModal,
showEditModal,
hideEditModal,
originalList,
handleSave,
}
}
export default useBatchEditDocumentMetadata