This PR solves small bugs around the view picker. - Couldn’t obtain optimistic update after a re-order of a view by drag and drop, we needed to refresh the page - Picking a new icon wouldn’t trigger optimistic update (same problem) - Picking a new icon would change the view (difficult to understand behavior) - Picking a new icon would trigger left drawer collapse (z-index problem) Since core views are not being handled by object metadata items anymore, and that all view logic is plugged on coreViewsState, this PR implemented optimistic effect by upserting into this state. Fixes https://github.com/twentyhq/twenty/issues/15422 Fixes https://github.com/twentyhq/twenty/issues/16986 # Before https://github.com/user-attachments/assets/64099c21-df9f-4772-ab0d-9ea449aed761 # After https://github.com/user-attachments/assets/f4e844b3-6530-4178-abdb-b7a10d2327b8 --------- Co-authored-by: Charles Bochet <charles@twenty.com>
23 lines
577 B
TypeScript
23 lines
577 B
TypeScript
import { findById } from '@/utils/array/findById';
|
|
|
|
export const upsertIntoArrayOfObjectsComparingId = <T extends { id: string }>(
|
|
arrayToUpsertInto: T[],
|
|
itemToUpsert: T,
|
|
): T[] => {
|
|
const alreadyExistingItemIndex = arrayToUpsertInto.findIndex(
|
|
findById(itemToUpsert.id),
|
|
);
|
|
|
|
const shouldReplaceItem = alreadyExistingItemIndex > -1;
|
|
|
|
if (shouldReplaceItem) {
|
|
const newArray = [...arrayToUpsertInto];
|
|
|
|
newArray.splice(alreadyExistingItemIndex, 1, itemToUpsert);
|
|
|
|
return newArray;
|
|
} else {
|
|
return arrayToUpsertInto.concat(itemToUpsert);
|
|
}
|
|
};
|