Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use snjs tag notes index for note counts #810

Merged
merged 6 commits into from
Jan 17, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 15 additions & 39 deletions app/assets/javascripts/ui_models/app_state/tags_state.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { confirmDialog } from '@/services/alertService';
import { STRING_DELETE_TAG, STRING_MISSING_SYSTEM_TAG } from '@/strings';
import { STRING_DELETE_TAG } from '@/strings';
import {
ApplicationEvent,
ComponentAction,
ContentType,
MessageData,
SNApplication,
SNSmartTag,
SNTag,
TagMutator,
UuidString,
UuidString
} from '@standardnotes/snjs';
import {
action,
computed,
makeAutoObservable,
makeObservable,
observable,
runInAction,
runInAction
} from 'mobx';
import { WebApplication } from '../application';
import { FeaturesState, SMART_TAGS_FEATURE_NAME } from './features_state';
Expand Down Expand Up @@ -126,9 +125,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 +146,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 +386,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 +418,6 @@ export class TagsState {
}
}

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

Expand All @@ -453,13 +429,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