Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose_recipient: Use sort_streams function in channel picker.
Evy Kassirer4dde07490950
diff --git a/web/src/compose_recipient.ts b/web/src/compose_recipient.tsindex 8fb6b41cc51..2367d1cdd62 100644--- a/web/src/compose_recipient.ts+++ b/web/src/compose_recipient.ts@@ -22,6 +22,8 @@ import * as narrow_state from "./narrow_state.ts";import {realm} from "./state_data.ts";import * as stream_color from "./stream_color.ts";import * as stream_data from "./stream_data.ts";+import type {StreamSubscription} from "./sub_store.ts";+import * as typeahead_helper from "./typeahead_helper.ts";import * as ui_util from "./ui_util.ts";import * as user_groups from "./user_groups.ts";import * as util from "./util.ts";@@ -277,9 +279,7 @@ function item_click_callback(event: JQuery.ClickEvent, dropdown: tippy.Instance)event.stopPropagation();}-function get_options_for_recipient_widget(): Option[] {- const options: Option[] = stream_data.get_options_for_dropdown_widget();-+function add_direct_messages_option(options: Option[]): Option[] {const direct_messages_option = {is_direct_message: true,unique_id: compose_state.DIRECT_MESSAGE_ID,@@ -292,9 +292,14 @@ function get_options_for_recipient_widget(): Option[] {} else {options.push(direct_messages_option);}+return options;}+function get_options_for_recipient_widget(): Option[] {+ return add_direct_messages_option(stream_data.get_options_for_dropdown_widget());+}+export function toggle_compose_recipient_dropdown(): void {$("#compose_select_recipient_widget").trigger("click");}@@ -361,6 +366,33 @@ export function initialize(): void {}return "#private_message_recipient";},+ sort_list_by_filter_value(items: Option[], filter_value: string): Option[] {+ const non_stream_items = [];+ const stream_items_by_stream_id = new Map<+ number,+ Option & {stream: StreamSubscription}+ >();+ for (const item of items) {+ if (item.stream === undefined) {+ non_stream_items.push(item);+ } else {+ stream_items_by_stream_id.set(item.stream.stream_id, {+ ...item,+ // Needed for typescript to recognize stream is defined+ stream: item.stream,+ });+ }+ }+ const stream_items = [...stream_items_by_stream_id.values()].map((item) => item.stream);+ const sorted_streams = typeahead_helper.sort_streams(stream_items, filter_value);+ const sorted_stream_items = sorted_streams.map(+ (stream) => stream_items_by_stream_id.get(stream.stream_id)!,+ );++ assert(non_stream_items.length === 1);+ assert(util.the(non_stream_items).is_direct_message === true);+ return add_direct_messages_option(sorted_stream_items);+ },});compose_select_recipient_dropdown_widget.setup();diff --git a/web/src/dropdown_widget.ts b/web/src/dropdown_widget.tsindex aa18b851616..ed4b475cb59 100644--- a/web/src/dropdown_widget.ts+++ b/web/src/dropdown_widget.ts@@ -91,6 +91,7 @@ export type DropdownWidgetOptions = {// When this is set, pressing tab will move focus to the target element.tab_moves_focus_to_target?: string | (() => string);search_placeholder_text?: string;+ sort_list_by_filter_value?: (items: Option[], filter_value: string) => Option[];};export class DropdownWidget {@@ -139,6 +140,7 @@ export class DropdownWidget {// here, so should be generalized or reworked.item_clicked = false;search_placeholder_text: string;+ sort_list_by_filter_value: ((items: Option[], filter_value: string) => Option[]) | undefined;constructor(options: DropdownWidgetOptions) {this.widget_name = options.widget_name;@@ -180,6 +182,7 @@ export class DropdownWidget {this.tab_moves_focus_to_target = options.tab_moves_focus_to_target;this.current_hover_index = 0;this.search_placeholder_text = options.search_placeholder_text ?? "";+ this.sort_list_by_filter_value = options.sort_list_by_filter_value;}init(): void {@@ -398,6 +401,7 @@ export class DropdownWidget {},},$simplebar_container: $popper.find(".dropdown-list-wrapper"),+ sort_by_filter_value: this.sort_list_by_filter_value,},);diff --git a/web/src/list_widget.ts b/web/src/list_widget.tsindex a257c803e68..e30c884dd98 100644--- a/web/src/list_widget.ts+++ b/web/src/list_widget.ts@@ -13,6 +13,7 @@ type ListWidgetMeta<Key, Item = Key> = {sorting_function: SortingFunction<Item> | null;applied_sorting_functions: [SortingFunction<Item>, boolean][]; // This is used to keep track of the sorting functions applied.sorting_functions: Map<string, SortingFunction<Item>>;+ sort_by_filter_value?: (items: Item[], filter_value: string) => Item[];filter_value: string;has_active_filters: boolean;offset: number;@@ -57,6 +58,7 @@ type ListWidgetOpts<Key, Item = Key> = {sort_fields?: Record<string, SortingFunction<Item>>;$simplebar_container: JQuery;$parent_container?: JQuery;+ sort_by_filter_value?: ((items: Item[], filter_value: string) => Item[]) | undefined;};type BaseListWidget = {@@ -295,6 +297,10 @@ export function create<Key, Item = Key>($scroll_listening_element,};+ if (opts.sort_by_filter_value) {+ meta.sort_by_filter_value = opts.sort_by_filter_value;+ }+const widget: ListWidget<Key, Item> = {get_current_list() {return meta.filtered_list;@@ -307,6 +313,15 @@ export function create<Key, Item = Key>(filter_and_sort() {meta.filtered_list = get_filtered_items(meta.filter_value, meta.list, opts);+ if (meta.sort_by_filter_value) {+ assert(meta.sorting_function === null);+ meta.filtered_list = meta.sort_by_filter_value(+ meta.filtered_list,+ meta.filter_value,+ );+ return;+ }+if (meta.sorting_function) {// If the sorting function is already applied, remove it to avoid duplicate sorting.const existing_sorting_function_index = meta.applied_sorting_functions.findIndex(diff --git a/web/src/typeahead_helper.ts b/web/src/typeahead_helper.tsindex 48dddd5718c..921e3158539 100644--- a/web/src/typeahead_helper.ts+++ b/web/src/typeahead_helper.ts@@ -1031,7 +1031,7 @@ function compare_by_user_group_name(group_a: UserGroup, group_b: UserGroup): numreturn util.strcmp(group_a.name, group_b.name);}-export let sort_streams = (matches: StreamPillData[], query: string): StreamPillData[] => {+export let sort_streams = <T extends StreamSubscription>(matches: T[], query: string): T[] => {const name_results = typeahead.triage(query, matches, (x) => x.name, compare_by_activity);const desc_results = typeahead.triage(query,