fix(ui): prevent removeThumbnail from desyncing thumbnail array in bulkUpload #11596
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
This PR fixes a pair of issues around thumbnails and their removal in the
BulkUpload
component. Namely:Adding non-image files followed by an image files simultaneously in the
BulkUpload
drawer causes a runtime error due to theThumbnail
component potentially rendering animg
element with an empty string, which, as the error points out, could cause Next to redownload the whole page supposedly.Adding non-image files with images simultaneously in the
BulkUpload
drawer and then removing them via theXIcon
button causes the thumbnails array to desync, showing incorrect thumbnails for subsequent forms. This is because thethumbnailUrlsRef
array is sparse and using.filter
on a sparse array omits empty holes in the array, causing it to desync.Why?
To properly preserve the thumbnail order and show the correct thumbnails for existing forms in the
BulkUpload
drawer, and to prevent a runtime error.How?
Solved by checking if
src
is truthy.Solved by preserving the sparseness of the array containing the thumbnail urls instead of using
.filter
. This PR also changes how thereducer
adds new forms to theBulkUpload
component as the existing way added new forms in the beginning of the array. This is problematic as thumbnails are generated and added sequentially to the array potentially causing another area of desynchronization between these two arrays.Before
Media-bulk-before--Payload.webm
After
Media-bulk-after--Payload.webm
Notes:
Map<string, string>
to map filenames to their urls, or permitting the thumbnail array to be dense.If this array was dense, then the fix for desynchronizing thumbnails becomes very simple. You would populate non-image indexes with
undefined
in thethumbnailUrlsRef
array instead of skipping them.removeThumbnails
, I didn't realize thatthumbnailUrlsRef
was indeed sparse. My mistake.