fix: stream SmartTags

This commit is contained in:
Baptiste Grob
2020-08-21 12:54:59 +02:00
parent d09a75d4f7
commit 3679fc0eca

View File

@@ -25,7 +25,7 @@ type TagState = {
tags: SNTag[]
smartTags: SNSmartTag[]
noteCounts: NoteCounts
selectedTag: SNTag
selectedTag?: SNTag
/** If creating a new tag, the previously selected tag will be set here, so that if new
* tag creation is canceled, the previous tag is re-selected */
previousTag?: SNTag
@@ -35,7 +35,7 @@ type TagState = {
templateTag?: SNTag
}
class TagsViewCtrl extends PureViewCtrl {
class TagsViewCtrl extends PureViewCtrl<{}, TagState> {
/** Passed through template */
readonly application!: WebApplication
@@ -46,7 +46,7 @@ class TagsViewCtrl extends PureViewCtrl {
private editingOriginalName?: string
formData: { tagTitle?: string } = {}
titles: Partial<Record<UuidString, string>> = {}
private removeTagsObserver!: () => void
private removeTagsObserver?: () => void
/* @ngInject */
constructor(
@@ -59,8 +59,8 @@ class TagsViewCtrl extends PureViewCtrl {
}
deinit() {
this.removeTagsObserver();
(this.removeTagsObserver as any) = undefined;
this.removeTagsObserver?.();
this.removeTagsObserver = undefined;
this.unregisterComponent();
this.unregisterComponent = undefined;
super.deinit();
@@ -78,10 +78,6 @@ class TagsViewCtrl extends PureViewCtrl {
return this.state as TagState;
}
async setTagState(state: Partial<TagState>) {
return this.setState(state);
}
async onAppStart() {
super.onAppStart();
this.registerComponentHandler();
@@ -92,7 +88,7 @@ class TagsViewCtrl extends PureViewCtrl {
this.loadPreferences();
this.beginStreamingItems();
const smartTags = this.application.getSmartTags();
this.setTagState({
this.setState({
smartTags: smartTags,
});
this.selectTag(smartTags[0]);
@@ -117,24 +113,29 @@ class TagsViewCtrl extends PureViewCtrl {
beginStreamingItems() {
this.removeTagsObserver = this.application.streamItems(
ContentType.Tag,
[ContentType.Tag, ContentType.SmartTag],
async (items) => {
await this.setTagState({
await this.setState({
tags: this.getMappedTags(),
smartTags: this.application.getSmartTags(),
});
this.reloadTitles(items as SNTag[]);
for (const tag of items as Array<SNTag | SNSmartTag>) {
this.titles[tag.uuid] = tag.title;
}
this.reloadNoteCounts();
if (this.getState().selectedTag) {
const selectedTag = this.state.selectedTag;
if (selectedTag) {
/** If the selected tag has been deleted, revert to All view. */
const matchingTag = items.find((tag) => {
return tag.uuid === this.getState().selectedTag.uuid;
return tag.uuid === selectedTag.uuid;
}) as SNTag;
if (matchingTag) {
if (matchingTag.deleted) {
this.selectTag(this.getState().smartTags[0]);
} else {
this.setTagState({
this.setState({
selectedTag: matchingTag
})
}
@@ -144,18 +145,12 @@ class TagsViewCtrl extends PureViewCtrl {
);
}
reloadTitles(tags: Array<SNTag | SNSmartTag>) {
for (const tag of tags) {
this.titles[tag.uuid] = tag.title;
}
}
/** @override */
onAppStateEvent(eventName: AppStateEvent, data?: any) {
if (eventName === AppStateEvent.PreferencesChanged) {
this.loadPreferences();
} else if (eventName === AppStateEvent.TagChanged) {
this.setTagState({
this.setState({
selectedTag: this.application.getAppState().getSelectedTag()
});
}
@@ -197,7 +192,7 @@ class TagsViewCtrl extends PureViewCtrl {
noteCounts[tag.uuid] = notes.length;
}
}
this.setTagState({
this.setState({
noteCounts: noteCounts
});
}
@@ -283,7 +278,7 @@ class TagsViewCtrl extends PureViewCtrl {
const newTag = await this.application.createTemplateItem(
ContentType.Tag
) as SNTag;
this.setTagState({
this.setState({
tags: [newTag].concat(this.getState().tags),
previousTag: this.getState().selectedTag,
selectedTag: newTag,
@@ -293,7 +288,7 @@ class TagsViewCtrl extends PureViewCtrl {
}
onTagTitleChange(tag: SNTag | SNSmartTag) {
this.setTagState({
this.setState({
editingTag: tag
});
}
@@ -324,7 +319,7 @@ class TagsViewCtrl extends PureViewCtrl {
await this.application.changeAndSaveItem<TagMutator>(tag.uuid, (mutator) => {
mutator.title = newTitle;
});
await this.setTagState({
await this.setState({
editingTag: undefined
});
}
@@ -333,7 +328,7 @@ class TagsViewCtrl extends PureViewCtrl {
const newTag = this.getState().templateTag!;
const newTitle = this.titles[newTag.uuid] || '';
if (newTitle.length === 0) {
await this.setTagState({
await this.setState({
templateTag: undefined
});
return;
@@ -349,7 +344,7 @@ class TagsViewCtrl extends PureViewCtrl {
const changedTag = await this.application.changeItem<TagMutator>(insertedTag.uuid, (m) => {
m.title = newTitle;
});
await this.setTagState({
await this.setState({
templateTag: undefined,
editingTag: undefined
});
@@ -359,7 +354,7 @@ class TagsViewCtrl extends PureViewCtrl {
async selectedRenameTag(tag: SNTag) {
this.editingOriginalName = tag.title;
await this.setTagState({
await this.setState({
editingTag: tag
});
document.getElementById('tag-' + tag.uuid)!.focus();