Update the saved sort when it comes back with another direction
Follow-up to #30469, addressing the QA Scout failure: remove a saved sort, add it back on the same field with the other direction (Employees ASC → DESC), then "Update view". It fails with the toast "View sort already exists", and the sort is not saved.
Cause
The server allows one sort per field and view (IDX_VIEW_SORT_FIELD_METADATA_ID_VIEW_ID_UNIQUE on fieldMetadataId, viewId). The client matched saved and draft sorts on field and direction and sub-field (isSameViewSortTarget). The re-added sort comes back under a new id, so in the opposite-direction case:
getViewSortsToCreatefound no match and created it,getViewSortsToDeletefound no match and deleted the saved one,- but the save hook runs create before delete, so the create hit the unique index.
The same thing happened when a sort came back on another sub-field (e.g. a full-name field: first name → last name).
Changes
isSameViewSortTarget→isViewSortOnSameField, which matches onfieldMetadataIdonly, like the server constraint.getViewSortsToUpdatefalls back to matching a saved sort by field when the ids differ. It returns the update under the saved id, so the existing row's direction and sub-field are updated in place, and no create or delete happens.- Create and delete keep matching by id or field, so the three diffs stay consistent. After the save, the draft and the view compare as equal, and the view is not left dirty.
I kept the create → update → delete order in useSaveRecordSortsToViewSorts. With field matching, the diff can no longer produce a create on a field that already has a saved sort.
Tests
getViewSortsToUpdate: updates the saved sort (saved id) when it comes back with another direction or another sub-field, and doesn't update it when it comes back unchanged.getViewSortsToCreateandgetViewSortsToDelete: neither creates nor deletes in those cases. The two sub-field tests added in #30469 asserted delete + create, which is the colliding path, so they now assert the opposite.
QA scenarios to re-run: re-add the same field with the same direction, with the opposite direction, and replace the sort with one on another field.
Generated by Claude Code