dify/web/app/components/datasets/metadata/hooks/use-batch-edit-document-metadata.ts

149 lines
4.4 KiB
TypeScript
Raw Normal View History

2025-02-21 16:48:10 +08:00
import { useBoolean } from 'ahooks'
2025-02-28 11:50:46 +08:00
import type { MetadataBatchEditToServer, MetadataItemInBatchEdit, MetadataItemWithValue } from '../types'
2025-02-26 16:38:12 +08:00
import type { SimpleDocumentDetail } from '@/models/datasets'
import { useMemo } from 'react'
2025-02-27 15:34:59 +08:00
import { isEqual } from 'lodash-es'
2025-03-05 17:56:10 +08:00
import { useBatchUpdateDocMetadata } from '@/service/knowledge/use-metadata'
2025-03-06 14:26:53 +08:00
import Toast from '@/app/components/base/toast'
2025-02-26 16:38:12 +08:00
type Props = {
2025-03-05 17:56:10 +08:00
datasetId: string
2025-03-06 11:49:27 +08:00
docList: SimpleDocumentDetail[]
2025-03-06 14:26:53 +08:00
onUpdate: () => void
2025-02-26 16:38:12 +08:00
}
const useBatchEditDocumentMetadata = ({
2025-03-05 17:56:10 +08:00
datasetId,
2025-03-06 11:49:27 +08:00
docList,
2025-03-06 14:26:53 +08:00
onUpdate,
2025-02-26 16:38:12 +08:00
}: Props) => {
2025-02-21 16:48:10 +08:00
const [isShowEditModal, {
setTrue: showEditModal,
setFalse: hideEditModal,
}] = useBoolean(false)
2025-03-05 17:56:10 +08:00
const metaDataList: MetadataItemWithValue[][] = (() => {
const res: MetadataItemWithValue[][] = []
2025-03-06 11:49:27 +08:00
docList.forEach((item) => {
2025-03-05 17:56:10 +08:00
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
2025-02-27 11:33:37 +08:00
const originalList: MetadataItemInBatchEdit[] = useMemo(() => {
2025-02-26 16:38:12 +08:00
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
2025-03-05 17:56:10 +08:00
}, [metaDataList])
2025-02-27 11:33:37 +08:00
2025-03-06 11:49:27 +08:00
const formateToBackendList = (editedList: MetadataItemInBatchEdit[], addedList: MetadataItemInBatchEdit[], isApplyToAllSelectDocument: boolean) => {
2025-02-27 11:33:37 +08:00
const updatedList = editedList.filter((editedItem) => {
const originalItem = originalList.find(i => i.id === editedItem.id)
if (!originalItem) // added item
return true
2025-02-27 15:34:59 +08:00
if (!isEqual(originalItem, editedItem)) // no change
2025-02-27 11:33:37 +08:00
return true
return false
})
const removedList = originalList.filter((originalItem) => {
const editedItem = editedList.find(i => i.id === originalItem.id)
if (!editedItem) // removed item
return true
return false
})
2025-03-06 11:49:27 +08:00
const res: MetadataBatchEditToServer = docList.map((item, i) => {
2025-02-27 11:33:37 +08:00
// the new metadata will override the old one
2025-03-06 11:49:27 +08:00
const oldMetadataList = metaDataList[i]
let newMetadataList: MetadataItemWithValue[] = [...oldMetadataList, ...addedList]
2025-02-27 11:33:37 +08:00
.filter((item) => {
2025-03-06 11:49:27 +08:00
return !removedList.find(removedItem => removedItem.id === item.id)
2025-02-27 11:33:37 +08:00
})
2025-02-27 15:34:59 +08:00
.map(item => ({
id: item.id,
name: item.name,
type: item.type,
value: item.value,
}))
2025-02-27 11:33:37 +08:00
if (isApplyToAllSelectDocument) {
// add missing metadata item
updatedList.forEach((editedItem) => {
if (!newMetadataList.find(i => i.id === editedItem.id))
newMetadataList.push(editedItem)
})
}
2025-02-27 15:34:59 +08:00
newMetadataList = newMetadataList.map((item) => {
const editedItem = updatedList.find(i => i.id === item.id)
if (editedItem)
return editedItem
return item
})
2025-02-27 11:33:37 +08:00
return {
document_id: item.id,
metadata_list: newMetadataList,
}
}).filter(item => item.metadata_list.length > 0)
return res
}
2025-03-06 14:26:53 +08:00
const { mutateAsync } = useBatchUpdateDocMetadata()
2025-03-05 17:56:10 +08:00
2025-03-06 14:26:53 +08:00
const handleSave = async (editedList: MetadataItemInBatchEdit[], addedList: MetadataItemInBatchEdit[], isApplyToAllSelectDocument: boolean) => {
2025-03-06 11:49:27 +08:00
const backendList = formateToBackendList(editedList, addedList, isApplyToAllSelectDocument)
2025-03-06 14:26:53 +08:00
await mutateAsync({
2025-03-05 17:56:10 +08:00
dataset_id: datasetId,
metadata_list: backendList,
})
2025-03-06 14:26:53 +08:00
onUpdate()
hideEditModal()
Toast.notify({
type: 'success',
message: 'common.api.actionSuccess',
})
2025-02-27 11:33:37 +08:00
}
2025-02-26 16:38:12 +08:00
2025-02-21 16:48:10 +08:00
return {
isShowEditModal,
showEditModal,
hideEditModal,
2025-02-26 16:38:12 +08:00
originalList,
2025-02-27 11:33:37 +08:00
handleSave,
2025-02-21 16:48:10 +08:00
}
}
export default useBatchEditDocumentMetadata