Skip to content

Commit

Permalink
feat: use snjs tag notes index for note counts
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Jan 11, 2022
1 parent c1b7f60 commit 2ffc72a
Showing 1 changed file with 12 additions and 35 deletions.
47 changes: 12 additions & 35 deletions app/assets/javascripts/ui_models/app_state/tags_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ export class TagsState {
) as SNTag[];
this.smartTags = this.application.getSmartTags();

this.tagsCountsState.update(this.tags);
this.allNotesCount_ = this.countAllNotes();

const selectedTag = this.selected_;
if (selectedTag) {
const matchingTag = items.find(
Expand All @@ -150,13 +147,13 @@ export class TagsState {
);

appEventListeners.push(
this.application.addEventObserver(async (eventName) => {
switch (eventName) {
case ApplicationEvent.CompletedIncrementalSync:
runInAction(() => {
this.allNotesCount_ = this.countAllNotes();
});
break;
this.application.addNoteCountChangeObserver((tagUuid) => {
if (!tagUuid) {
this.allNotesCount_ = this.application.allCountableNotesCount();
} else {
this.tagsCountsState.update([
this.application.findItem(tagUuid) as SNTag,
]);
}
})
);
Expand Down Expand Up @@ -390,23 +387,6 @@ export class TagsState {
}
}

private countAllNotes(): number {
const allTag = this.application.getSmartTags().find((tag) => tag.isAllTag);

if (!allTag) {
console.error(STRING_MISSING_SYSTEM_TAG);
return -1;
}

const notes = this.application
.notesMatchingSmartTag(allTag)
.filter((note) => {
return !note.archived && !note.trashed;
});

return notes.length;
}

public onFoldersComponentMessage(
action: ComponentAction,
data: MessageData
Expand Down Expand Up @@ -439,9 +419,6 @@ export class TagsState {
}
}

/**
* Bug fix for issue 1201550111577311,
*/
class TagsCountsState {
public counts: { [uuid: string]: number } = {};

Expand All @@ -453,13 +430,13 @@ class TagsCountsState {
}

public update(tags: SNTag[]) {
const newCounts: { [uuid: string]: number } = {};
const newCounts: { [uuid: string]: number } = Object.assign(
{},
this.counts
);

tags.forEach((tag) => {
newCounts[tag.uuid] = this.application.referencesForItem(
tag,
ContentType.Note
).length;
newCounts[tag.uuid] = this.application.countableNotesForTag(tag);
});

this.counts = newCounts;
Expand Down

0 comments on commit 2ffc72a

Please sign in to comment.