Fix Search error when user can't search

If there are no search sources, HTML for the Search component won't be rendered, so trying to attach listeners to it will likely error.

In this PR, we don't attach such listeners/logic if there are no sources. We also stop asserting that sources is defined to help avoid other similar issues in the future.
This commit is contained in:
Alexander Skvortsov 2021-12-26 20:04:48 -05:00
parent dc661bf144
commit 0c95d28e94
No known key found for this signature in database
GPG Key ID: C4E3BBF9C3412B4C

View File

@ -88,7 +88,7 @@ export default class Search<T extends SearchAttrs = SearchAttrs> extends Compone
/**
* An array of SearchSources.
*/
protected sources!: SearchSource[];
protected sources?: SearchSource[];
/**
* The number of sources that are still loading results.
@ -192,7 +192,7 @@ export default class Search<T extends SearchAttrs = SearchAttrs> extends Compone
this.setIndex(this.getCurrentNumericIndex());
// If there are no sources, the search view is not shown.
if (!this.sources.length) return;
if (!this.sources?.length) return;
this.updateMaxHeight();
}
@ -200,6 +200,10 @@ export default class Search<T extends SearchAttrs = SearchAttrs> extends Compone
oncreate(vnode: Mithril.VnodeDOM<T, this>) {
super.oncreate(vnode);
// If there are no sources, we shouldn't initialize logic for
// search elements, as they will not be shown.
if (!this.sources?.length) return;
const search = this;
const state = this.searchState;
@ -237,7 +241,7 @@ export default class Search<T extends SearchAttrs = SearchAttrs> extends Compone
if (state.isCached(query)) return;
if (query.length >= (search.constructor as typeof Search).MIN_SEARCH_LEN) {
search.sources.map((source) => {
search.sources?.map((source) => {
if (!source.search) return;
search.loadingSources++;