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