fix(ui): remove stale thumbnails in bulkUpload after partial success (#10651)

This commit is contained in:
Said Akhrarov
2025-02-28 17:18:18 -05:00
committed by GitHub
parent 79a7b4ad02
commit e75d38ca82

View File

@@ -264,10 +264,19 @@ export function FormsManagerProvider({ children }: FormsManagerProps) {
[initializeSharedFormState, hasInitializedState, toggleLoadingOverlay],
)
const removeFile: FormsManagerContext['removeFile'] = React.useCallback((index) => {
dispatch({ type: 'REMOVE_FORM', index })
const removeThumbnails = React.useCallback((indexes: number[]) => {
thumbnailUrlsRef.current = thumbnailUrlsRef.current.filter((_, i) => !indexes.includes(i))
setRenderedThumbnails([...thumbnailUrlsRef.current])
}, [])
const removeFile: FormsManagerContext['removeFile'] = React.useCallback(
(index) => {
dispatch({ type: 'REMOVE_FORM', index })
removeThumbnails([index])
},
[removeThumbnails],
)
const setFormTotalErrorCount: FormsManagerContext['setFormTotalErrorCount'] = React.useCallback(
({ errorCount, index }) => {
dispatch({
@@ -367,7 +376,17 @@ export function FormsManagerProvider({ children }: FormsManagerProps) {
setLoadingText('')
setIsUploading(false)
const remainingForms = currentForms.filter(({ errorCount }) => errorCount > 0)
const remainingForms = []
const thumbnailIndexesToRemove = []
currentForms.forEach(({ errorCount }, i) => {
if (errorCount) {
remainingForms.push(currentForms[i])
} else {
thumbnailIndexesToRemove.push(i)
}
})
const successCount = Math.max(0, currentForms.length - remainingForms.length)
const errorCount = currentForms.length - successCount
@@ -377,6 +396,10 @@ export function FormsManagerProvider({ children }: FormsManagerProps) {
if (typeof onSuccess === 'function') {
onSuccess(newDocs, errorCount)
}
if (remainingForms.length && thumbnailIndexesToRemove.length) {
removeThumbnails(thumbnailIndexesToRemove)
}
}
if (errorCount) {
@@ -398,6 +421,7 @@ export function FormsManagerProvider({ children }: FormsManagerProps) {
actionURL,
activeIndex,
forms,
removeThumbnails,
onSuccess,
collectionSlug,
getUploadHandler,