Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
topic_filter_pill: Extract shared typeahead helpers.
Aadithya-J38cae5f267ac
diff --git a/web/src/topic_filter_pill.ts b/web/src/topic_filter_pill.tsindex 06c4a5ebcbe..7759ef7cd6b 100644--- a/web/src/topic_filter_pill.ts+++ b/web/src/topic_filter_pill.ts@@ -3,6 +3,7 @@ import render_input_pill from "../templates/input_pill.hbs";import {$t} from "./i18n.ts";import type {InputPillConfig, InputPillContainer} from "./input_pill.ts";import * as input_pill from "./input_pill.ts";+import * as typeahead_helper from "./typeahead_helper.ts";export type TopicFilterPill = {type: "topic_filter";@@ -13,6 +14,14 @@ export type TopicFilterPill = {export type TopicFilterPillWidget = InputPillContainer<TopicFilterPill>;+type TopicFilterTypeaheadOptions = {+ item_html: (item: TopicFilterPill, query: string) => string | undefined;+ matcher: (item: TopicFilterPill, query: string) => boolean;+ sorter: (items: TopicFilterPill[], query: string) => TopicFilterPill[];+ stopAdvance: boolean;+ dropup: boolean;+};+export const filter_options: TopicFilterPill[] = [{type: "topic_filter",@@ -37,6 +46,59 @@ export const filter_options: TopicFilterPill[] = [},];+export function get_typeahead_base_options(): TopicFilterTypeaheadOptions {+ return {+ item_html(item: TopicFilterPill, _query: string) {+ return typeahead_helper.render_topic_state(item.label);+ },+ matcher(item: TopicFilterPill, query: string) {+ // This basically only matches if `is:` is in the query.+ return (+ query.includes(":") &&+ (item.syntax.toLowerCase().startsWith(query.toLowerCase()) ||+ (item.syntax.startsWith("-") &&+ item.syntax.slice(1).toLowerCase().startsWith(query.toLowerCase())))+ );+ },+ sorter(items: TopicFilterPill[], _query: string) {+ // This sort order places "Unresolved topics" first+ // always, which is good because that's almost always+ // what users will want.+ return items;+ },+ // Prevents key events from propagating to other handlers or+ // triggering default browser actions.+ stopAdvance: true,+ // Use dropup, to match compose typeahead.+ dropup: true,+ };+}++export function get_matching_filter_options({+ current_items,+ query,+ allow_resolved_topic_filters,+}: {+ current_items: TopicFilterPill[];+ query: string;+ allow_resolved_topic_filters: boolean;+}): TopicFilterPill[] {+ const current_syntaxes = new Set(current_items.map((item) => item.syntax));++ return filter_options.filter((option) => {+ if (!allow_resolved_topic_filters && option.syntax.endsWith("resolved")) {+ return false;+ }+ if (current_syntaxes.has(option.syntax)) {+ return false;+ }+ if (option.match_prefix_required && !query.startsWith(option.match_prefix_required)) {+ return false;+ }+ return true;+ });+}+export function create_item_from_syntax(syntax: string,current_items: TopicFilterPill[],diff --git a/web/src/topic_list.ts b/web/src/topic_list.tsindex 65ac4d4d4fe..7359e0bf1a8 100644--- a/web/src/topic_list.ts+++ b/web/src/topic_list.ts@@ -23,7 +23,6 @@ import * as topic_filter_pill from "./topic_filter_pill.ts";import type {TopicFilterPill, TopicFilterPillWidget} from "./topic_filter_pill.ts";import * as topic_list_data from "./topic_list_data.ts";import type {TopicInfo} from "./topic_list_data.ts";-import * as typeahead_helper from "./typeahead_helper.ts";import * as ui_util from "./ui_util.ts";import * as vdom from "./vdom.ts";@@ -626,56 +625,28 @@ export function setup_topic_search_typeahead(): void {};const options = {+ ...topic_filter_pill.get_typeahead_base_options(),source() {const stream_id = active_stream_id();assert(stream_id !== undefined);const pills = topic_filter_pill_widget!.items();- const current_syntaxes = new Set(pills.map((pill) => pill.syntax));const query = $("#topic_filter_query").text().trim();const has_locally_available_resolved_topics =stream_topic_history.stream_has_locally_available_resolved_topics(stream_id);- return topic_filter_pill.filter_options.filter((option) => {- if (!has_locally_available_resolved_topics && option.syntax.endsWith("resolved")) {- // Technically, it could still be useful to show- // the is:resolved option, as local data is not- // complete. But because zooming the topic list- // does the topic history fetch, it's reasonable to- // ignore that possibility and just only show the- // resolved topic options if we can confirm- // they're relevant.- return false;- }- if (current_syntaxes.has(option.syntax)) {- return false;- }- if (- option.match_prefix_required &&- !query.startsWith(option.match_prefix_required)- ) {- return false;- }- return true;+ return topic_filter_pill.get_matching_filter_options({+ current_items: pills,+ query,+ // Technically, it could still be useful to show+ // the is:resolved option, as local data is not+ // complete. But because zooming the topic list+ // does the topic history fetch, it's reasonable to+ // ignore that possibility and just only show the+ // resolved topic options if we can confirm+ // they're relevant.+ allow_resolved_topic_filters: has_locally_available_resolved_topics,});},- item_html(item: TopicFilterPill) {- return typeahead_helper.render_topic_state(item.label);- },- matcher(item: TopicFilterPill, query: string) {- // This basically only matches if `is:` is in the query.- return (- query.includes(":") &&- (item.syntax.toLowerCase().startsWith(query.toLowerCase()) ||- (item.syntax.startsWith("-") &&- item.syntax.slice(1).toLowerCase().startsWith(query.toLowerCase())))- );- },- sorter(items: TopicFilterPill[]) {- // This sort order places "Unresolved topics" first- // always, which is good because that's almost always what- // users will want.- return items;- },updater(item: TopicFilterPill) {assert(topic_filter_pill_widget !== null);topic_filter_pill_widget.clear(true);@@ -684,11 +655,6 @@ export function setup_topic_search_typeahead(): void {$input.trigger("focus");return get_left_sidebar_topic_search_term();},- // Prevents key events from propagating to other handlers or- // triggering default browser actions.- stopAdvance: true,- // Use dropup, to match compose typeahead.- dropup: true,};topic_state_typeahead = new Typeahead(typeahead_input, options);