Skip to content

Commit

Permalink
Optimization: only recalculate isSelected when media items are deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
mtalcott committed Sep 4, 2023
1 parent 55b92b8 commit 3f4515c
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions client/src/utils/useTaskResultsReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,24 @@ function taskResultsReducer(
[action.mediaItemId]: { ...mediaItem, ...action.attributes },
},
};
// Also update hasDuplicates on groups. Only need the group that contains
// this mediaItem, but we have no reverse lookup from mediaItem to
// group, so update all groups.
newState.groups = Object.fromEntries(
Object.entries(state.groups).map(([groupId, group]) => {
const hasDuplicates = groupHasDuplicates(group, newState);
return [
groupId,
{
...group,
hasDuplicates: hasDuplicates,
isSelected: group.isSelected && hasDuplicates,
},
];
})
);
// If deletedAt changed, also update hasDuplicates on groups. Only need
// the group that contains this mediaItem, but we have no reverse lookup
// from mediaItem to group, so update all groups.
if (action.attributes.deletedAt !== undefined) {
newState.groups = Object.fromEntries(
Object.entries(state.groups).map(([groupId, group]) => {
const hasDuplicates = groupHasDuplicates(group, newState);
return [
groupId,
{
...group,
hasDuplicates: hasDuplicates,
isSelected: group.isSelected && hasDuplicates,
},
];
})
);
}
return newState;
} else if (action.type === "clearMediaItemErrors") {
return {
Expand All @@ -106,7 +108,7 @@ function taskResultsReducer(
),
};
} else {
throw new Error(`Unregognized action type: ${action.type}`);
throw new Error(`Unrecognized action type: ${action.type}`);
}
}

Expand Down

0 comments on commit 3f4515c

Please sign in to comment.