From 77c614e0725c8c49da154e80b42244ff76315960 Mon Sep 17 00:00:00 2001 From: sanchi-t Date: Thu, 27 Jun 2024 20:06:22 +0530 Subject: [PATCH] set_up_stream: Update sorter function. Make sure that typeahead results are alphabetized. Fixes #30577. --- web/src/pill_typeahead.ts | 2 +- web/src/typeahead_helper.ts | 9 +++++++++ web/tests/pill_typeahead.test.js | 2 +- web/tests/typeahead_helper.test.js | 9 +++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/web/src/pill_typeahead.ts b/web/src/pill_typeahead.ts index 0d37cdc471..db21d131d7 100644 --- a/web/src/pill_typeahead.ts +++ b/web/src/pill_typeahead.ts @@ -115,7 +115,7 @@ export function set_up_stream( if (query.startsWith("#")) { query = query.slice(1); } - return typeahead_helper.sort_streams(stream_matches, query); + return typeahead_helper.sort_streams_by_name(stream_matches, query); }, updater(item: StreamPillData, _query: string): undefined { stream_pill.append_stream(item, pills, false); diff --git a/web/src/typeahead_helper.ts b/web/src/typeahead_helper.ts index f9132f745b..5b0a2fea7b 100644 --- a/web/src/typeahead_helper.ts +++ b/web/src/typeahead_helper.ts @@ -628,6 +628,10 @@ export function compare_by_activity( return util.strcmp(stream_a.name, stream_b.name); } +function compare_by_name(stream_a: StreamSubscription, stream_b: StreamSubscription): number { + return util.strcmp(stream_a.name, stream_b.name); +} + export function sort_streams(matches: StreamPillData[], query: string): StreamPillData[] { const name_results = typeahead.triage(query, matches, (x) => x.name, compare_by_activity); const desc_results = typeahead.triage( @@ -640,6 +644,11 @@ export function sort_streams(matches: StreamPillData[], query: string): StreamPi return [...name_results.matches, ...desc_results.matches, ...desc_results.rest]; } +export function sort_streams_by_name(matches: StreamPillData[], query: string): StreamPillData[] { + const results = typeahead.triage(query, matches, (x) => x.name, compare_by_name); + return [...results.matches, ...results.rest]; +} + export function query_matches_person( query: string, person: UserPillData | UserOrMentionPillData, diff --git a/web/tests/pill_typeahead.test.js b/web/tests/pill_typeahead.test.js index a83e1625d8..6c5e0f2dc1 100644 --- a/web/tests/pill_typeahead.test.js +++ b/web/tests/pill_typeahead.test.js @@ -226,7 +226,7 @@ run_test("set_up_user", ({mock_template, override, override_rewire}) => { run_test("set_up_stream", ({mock_template, override, override_rewire}) => { override_rewire(typeahead_helper, "render_stream", () => $fake_rendered_stream); - override_rewire(typeahead_helper, "sort_streams", ({streams}) => { + override_rewire(typeahead_helper, "sort_streams_by_name", ({streams}) => { sort_streams_called = true; return streams; }); diff --git a/web/tests/typeahead_helper.test.js b/web/tests/typeahead_helper.test.js index 5d4b6d5e61..b0bbe6b6e7 100644 --- a/web/tests/typeahead_helper.test.js +++ b/web/tests/typeahead_helper.test.js @@ -211,6 +211,15 @@ test("sort_streams", ({override, override_rewire}) => { assert.deepEqual(test_streams[4].name, "dead"); // Completely inactive stream assert.deepEqual(test_streams[5].name, "Derp"); // Muted stream last + // Sort streams by name + test_streams = th.sort_streams_by_name(test_streams, "d"); + assert.deepEqual(test_streams[0].name, "dead"); + assert.deepEqual(test_streams[1].name, "dead (almost)"); + assert.deepEqual(test_streams[2].name, "Denmark"); + assert.deepEqual(test_streams[3].name, "Derp"); + assert.deepEqual(test_streams[4].name, "Dev"); + assert.deepEqual(test_streams[5].name, "Docs"); + override_rewire(compose_state, "stream_name", () => "Different"); // Test sort streams with description test_streams = [