Commit 5ffeba96 authored by Robert Knight's avatar Robert Knight

Simplify code using `filter`/`map`

This makes it more obvious what is going on at a glance. Refactoring the
code in this way revealed to TS a hazard where `newGroupId` could, at
least based only on local reasoning, be null. In practice this shouldn't
happen due to the contexts when the method is called, but I've added a
check to be safe and keep TS happy.
parent 97eae5d4
......@@ -446,17 +446,13 @@ export class GroupsService {
const newGroupId = this._store.focusedGroupId();
const groupHasChanged = prevGroupId !== newGroupId && prevGroupId !== null;
if (groupHasChanged) {
if (groupHasChanged && newGroupId) {
// Move any top-level new annotations to the newly-focused group.
// Leave replies where they are.
const updatedAnnotations = [];
this._store.newAnnotations().forEach(annot => {
if (!isReply(annot)) {
updatedAnnotations.push(
Object.assign({}, annot, { group: newGroupId })
);
}
});
const updatedAnnotations = this._store
.newAnnotations()
.filter(ann => !isReply(ann))
.map(ann => ({ ...ann, group: newGroupId }));
if (updatedAnnotations.length) {
this._store.addAnnotations(updatedAnnotations);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment