feat: use IDs in arrays for more semantic version diff #11671
+272
−21
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?
For arrays, where every entry has an
id
, use theid
to track the movement of elements when computing the version diff. This means that e.g. deleting an entry in the middle, or adding an entry in the middle no longer causes all following array elements to be shown as fully changed, making the version diff easier to read for humans (see screenshots below for how diffs would render now).Arrays where (some) entries lack the
id
are compared as before, by index.Version diff in array where elements have
id
when a new entry is created in 1st place:status quo
Version diff in array where elements have
id
when the first entry is deleted:status quo
Version diff in array where elements have
id
when a new entry is created in the middle:status quo
Version diff in array where elements have
id
when an entry is deleted in the middle:status quo
Version diff in array where elements have
id
when an an entry is added at the end:Version diff in array where elements have
id
when an entry is deleted at the end:Why?
Makes version diff easier to parse for humans, because only changed parts are highlighted.
Possible Follow-Ups
This change is an improvement, but the diff could be even more human readable: Right now, when an array element is removed, the version diff only shows properties that were set as removed, not the whole element. Likewise, when an element is moved to a different place in the array, there's no indicator that the element was merely moved, not 1 element removed, and 1 element added. So the "labelling" of rows in the version diff could still be more semantic.
How?
packages/next/src/views/Version/RenderFieldsToDiff/buildVersionFields.tsx
checks whether all elements of an array field, both in the comparison and the new version contain anid
property. If that's not the case, the current, index-based code path is used. But if it is the case, the new utilitypackages/next/src/views/Version/RenderFieldsToDiff/utilities/getArrayElementIdsInComparisonOrder.ts
is called to get the union of all array element IDs in a reasonable order, which is used to iteratively callbuildVersionFields
with thecomparisonSiblingData
of the array element with the matchingid
property (if existing).