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

147 lines
4.7 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 { DataType } from '../types'
import type { SimpleDocumentDetail } from '@/models/datasets'
import { useMemo } from 'react'
2025-02-27 15:34:59 +08:00
import { isEqual } from 'lodash-es'
2025-02-26 16:38:12 +08:00
// compare
// original and edited list.
// Use the edited list, except the original and edited value is both multiple value.
const testMetadataList: MetadataItemWithValue[][] = [
[
{ id: 'str-same-value', name: 'name', type: DataType.string, value: 'Joel' },
{ id: 'num', name: 'age', type: DataType.number, value: 10 },
2025-02-27 15:34:59 +08:00
{ id: 'date', name: 'date', type: DataType.time, value: null },
2025-02-26 16:38:12 +08:00
{ id: 'str-with-different-value', name: 'hobby', type: DataType.string, value: 'bbb' },
],
[
{ id: 'str-same-value', name: 'name', type: DataType.string, value: 'Joel' },
{ id: 'str-with-different-value', name: 'hobby', type: DataType.string, value: 'ccc' },
2025-02-27 15:34:59 +08:00
{ id: 'str-extra', name: 'extra', type: DataType.string, value: 'ddd' },
2025-02-26 16:38:12 +08:00
],
]
type Props = {
list: SimpleDocumentDetail[]
}
const useBatchEditDocumentMetadata = ({
list,
}: Props) => {
2025-02-21 16:48:10 +08:00
const [isShowEditModal, {
setTrue: showEditModal,
setFalse: hideEditModal,
}] = useBoolean(false)
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 }> = {}
// TODO: mock backend data struct
// const metaDataList: MetadataItemWithValue[][] = list.map((item, i) => {
// if (item.doc_metadata)
// return item.doc_metadata
// return testMetadataList[i] || []
// })
const metaDataList = testMetadataList
const res: MetadataItemInBatchEdit[] = []
metaDataList.forEach((metaData) => {
metaData.forEach((item) => {
// if (item.value === 'ccc') {
// debugger
// }
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-02-27 11:33:37 +08:00
}, [])
const formateToBackendList = (editedList: MetadataItemInBatchEdit[], isApplyToAllSelectDocument: boolean) => {
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-02-28 11:50:46 +08:00
const res: MetadataBatchEditToServer = list.map((item, i) => {
2025-02-27 11:33:37 +08:00
// the new metadata will override the old one
const oldMetadataList = item.doc_metadata || testMetadataList[i] // TODO: used mock data
2025-02-27 15:34:59 +08:00
let newMetadataList: MetadataItemWithValue[] = oldMetadataList
2025-02-27 11:33:37 +08:00
.filter((item) => {
return !removedList.find(removedItem => removedItem.id === item.id)
})
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
}
const handleSave = (editedList: MetadataItemInBatchEdit[], isApplyToAllSelectDocument: boolean) => {
const backendList = formateToBackendList(editedList, isApplyToAllSelectDocument)
console.log(backendList)
}
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