Co-authored-by: crazywoola <427733928@qq.com>tags/1.7.1
| if (totalPages < currPage + 1) | if (totalPages < currPage + 1) | ||||
| setCurrPage(totalPages === 0 ? 0 : totalPages - 1) | setCurrPage(totalPages === 0 ? 0 : totalPages - 1) | ||||
| } | } | ||||
| // eslint-disable-next-line react-hooks/exhaustive-deps | |||||
| }, [documentsRes]) | }, [documentsRes]) | ||||
| const invalidDocumentDetail = useInvalidDocumentDetailKey() | const invalidDocumentDetail = useInvalidDocumentDetailKey() | ||||
| invalidChunkList() | invalidChunkList() | ||||
| invalidChildChunkList() | invalidChildChunkList() | ||||
| }, 5000) | }, 5000) | ||||
| // eslint-disable-next-line react-hooks/exhaustive-deps | |||||
| }, []) | }, []) | ||||
| const documentsWithProgress = useMemo(() => { | const documentsWithProgress = useMemo(() => { | ||||
| const documentsList = isDataSourceNotion ? documentsWithProgress?.data : documentsRes?.data | const documentsList = isDataSourceNotion ? documentsWithProgress?.data : documentsRes?.data | ||||
| const [selectedIds, setSelectedIds] = useState<string[]>([]) | const [selectedIds, setSelectedIds] = useState<string[]>([]) | ||||
| // Clear selection when search changes to avoid confusion | |||||
| useEffect(() => { | |||||
| if (searchValue !== query.keyword) | |||||
| setSelectedIds([]) | |||||
| }, [searchValue, query.keyword]) | |||||
| const { run: handleSearch } = useDebounceFn(() => { | const { run: handleSearch } = useDebounceFn(() => { | ||||
| setSearchValue(inputValue) | setSearchValue(inputValue) | ||||
| }, { wait: 500 }) | }, { wait: 500 }) |
| handleSave, | handleSave, | ||||
| } = useBatchEditDocumentMetadata({ | } = useBatchEditDocumentMetadata({ | ||||
| datasetId, | datasetId, | ||||
| docList: documents.filter(item => selectedIds.includes(item.id)), | |||||
| docList: documents.filter(doc => selectedIds.includes(doc.id)), | |||||
| selectedDocumentIds: selectedIds, // Pass all selected IDs separately | |||||
| onUpdate, | onUpdate, | ||||
| }) | }) | ||||
| type Props = { | type Props = { | ||||
| datasetId: string | datasetId: string | ||||
| docList: SimpleDocumentDetail[] | docList: SimpleDocumentDetail[] | ||||
| selectedDocumentIds?: string[] | |||||
| onUpdate: () => void | onUpdate: () => void | ||||
| } | } | ||||
| const useBatchEditDocumentMetadata = ({ | const useBatchEditDocumentMetadata = ({ | ||||
| datasetId, | datasetId, | ||||
| docList, | docList, | ||||
| selectedDocumentIds, | |||||
| onUpdate, | onUpdate, | ||||
| }: Props) => { | }: Props) => { | ||||
| const [isShowEditModal, { | const [isShowEditModal, { | ||||
| return false | return false | ||||
| }) | }) | ||||
| const res: MetadataBatchEditToServer = docList.map((item, i) => { | |||||
| // the new metadata will override the old one | |||||
| const oldMetadataList = metaDataList[i] | |||||
| // 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] | let newMetadataList: MetadataItemWithValue[] = [...oldMetadataList, ...addedList] | ||||
| .filter((item) => { | .filter((item) => { | ||||
| return !removedList.find(removedItem => removedItem.id === item.id) | return !removedList.find(removedItem => removedItem.id === item.id) | ||||
| }) | }) | ||||
| return { | return { | ||||
| document_id: item.id, | |||||
| document_id: documentId, | |||||
| metadata_list: newMetadataList, | metadata_list: newMetadataList, | ||||
| } | } | ||||
| }) | }) |