Skip to content

Commit 6da2550

Browse files
apoorvapendsetimabbott
authored andcommitted
1 parent 9e856b0 commit 6da2550

File tree

3 files changed

+74
-24
lines changed

3 files changed

+74
-24
lines changed

web/src/composebox_typeahead.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ export type LanguageSuggestion = {
8080
};
8181

8282
export type TopicSuggestion = {
83-
topic: string;
83+
topic_display_name: string;
8484
type: "topic_list";
85+
is_empty_string_topic: boolean;
8586
// is_channel_link will be used when we want to only render the stream as an
8687
// option in the topic typeahead while having #**stream_name> as the token.
8788
is_channel_link: boolean;
@@ -954,9 +955,17 @@ export function get_candidates(
954955
topic_list.push(token);
955956
}
956957
const matcher = get_topic_matcher(token);
958+
// We want the empty topic's display name to match the query
959+
// while searching for topics in the typeahead.
960+
const empty_string_topic_display_name = util.get_final_topic_display_name("");
961+
const empty_string_topic_idx = topic_list.indexOf("");
962+
if (empty_string_topic_idx !== -1) {
963+
topic_list[empty_string_topic_idx] = empty_string_topic_display_name;
964+
}
957965
const matches = topic_list.filter((item) => matcher(item));
958966
const matches_list: TopicSuggestion[] = matches.map((topic) => ({
959-
topic,
967+
topic_display_name: topic,
968+
is_empty_string_topic: topic === empty_string_topic_display_name,
960969
type: "topic_list",
961970
is_channel_link: false,
962971
stream_data: {
@@ -970,13 +979,14 @@ export function get_candidates(
970979
const topic_suggestion_candidates = typeahead_helper.sorter(
971980
token,
972981
matches_list,
973-
(x) => x.topic,
982+
(x) => x.topic_display_name,
974983
);
975984

976985
// Add link to channel if and only if nothing is typed after '>'
977986
if (token.length === 0) {
978987
topic_suggestion_candidates.unshift({
979-
topic: stream_name,
988+
topic_display_name: stream_name,
989+
is_empty_string_topic: false,
980990
type: "topic_list",
981991
is_channel_link: true,
982992
stream_data: {
@@ -1231,10 +1241,14 @@ export function content_typeahead_selected(
12311241
beginning = beginning.slice(0, syntax_start_index) + "#**" + stream_name + "** ";
12321242
break;
12331243
}
1234-
beginning =
1235-
beginning.slice(0, syntax_start_index) +
1236-
topic_link_util.get_stream_topic_link_syntax(stream_name, item.topic) +
1237-
" ";
1244+
const stream_topic_link_syntax =
1245+
item.topic_display_name === util.get_final_topic_display_name("")
1246+
? topic_link_util.get_stream_topic_link_syntax(stream_name, "")
1247+
: topic_link_util.get_stream_topic_link_syntax(
1248+
stream_name,
1249+
item.topic_display_name,
1250+
);
1251+
beginning = beginning.slice(0, syntax_start_index) + stream_topic_link_syntax + " ";
12381252
break;
12391253
}
12401254
case "time_jump": {

web/templates/typeahead_list_item.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
<span role="button" class="conversation-arrow zulip-icon zulip-icon-chevron-right stream-to-topic-arrow"></span>
2525
</div>
2626
<div class="typeahead-text-container">
27-
<strong class="typeahead-strong-section">
28-
{{~ topic ~}}
27+
<strong class="typeahead-strong-section{{#if is_empty_string_topic}} empty-topic-display{{/if}}">
28+
{{~ topic_display_name ~}}
2929
</strong>
3030
</div>
3131
{{else}}
3232
{{!-- Separate container to ensure overflowing text remains in this container. --}}
3333
<div class="typeahead-text-container{{#if has_secondary_html}} has_secondary_html{{/if}}">
34-
<strong class="typeahead-strong-section{{#if is_empty_string_topic}} empty-topic-display{{/if}}">
34+
<strong class="typeahead-strong-section">
3535
{{~#if stream~}}
3636
{{~> inline_decorated_stream_name stream=stream ~}}
3737
{{~else~}}

web/tests/composebox_typeahead.test.cjs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
const assert = require("node:assert/strict");
44

5+
const {get_final_topic_display_name} = require("../src/util.ts");
6+
57
const {mock_banners} = require("./lib/compose_banner.cjs");
68
const {mock_esm, set_global, with_overrides, zrequire} = require("./lib/namespace.cjs");
79
const {run_test, noop} = require("./lib/test.cjs");
810
const $ = require("./lib/zjquery.cjs");
911

1012
let autosize_called;
13+
const REALM_EMPTY_TOPIC_DISPLAY_NAME = "general chat";
1114

1215
const bootstrap_typeahead = mock_esm("../src/bootstrap_typeahead");
1316
const compose_ui = mock_esm("../src/compose_ui", {
@@ -61,7 +64,7 @@ const {initialize_user_settings} = zrequire("user_settings");
6164

6265
const current_user = {};
6366
set_current_user(current_user);
64-
const realm = {};
67+
const realm = {realm_empty_topic_display_name: REALM_EMPTY_TOPIC_DISPLAY_NAME};
6568
set_realm(realm);
6669
const user_settings = {};
6770
initialize_user_settings({user_settings});
@@ -853,7 +856,7 @@ test("content_typeahead_selected", ({override}) => {
853856
ct.get_or_set_token_for_testing("test");
854857
actual_value = ct.content_typeahead_selected(
855858
{
856-
topic: "testing",
859+
topic_display_name: "testing",
857860
type: "topic_list",
858861
},
859862
query,
@@ -866,7 +869,7 @@ test("content_typeahead_selected", ({override}) => {
866869
ct.get_or_set_token_for_testing("");
867870
actual_value = ct.content_typeahead_selected(
868871
{
869-
topic: "testing",
872+
topic_display_name: "testing",
870873
type: "topic_list",
871874
},
872875
query,
@@ -879,7 +882,7 @@ test("content_typeahead_selected", ({override}) => {
879882
ct.get_or_set_token_for_testing("");
880883
actual_value = ct.content_typeahead_selected(
881884
{
882-
topic: "Sweden",
885+
topic_display_name: "Sweden",
883886
type: "topic_list",
884887
is_channel_link: false,
885888
},
@@ -889,10 +892,24 @@ test("content_typeahead_selected", ({override}) => {
889892
expected_value = "Hello #**Sweden>Sweden** ";
890893
assert.equal(actual_value, expected_value);
891894

895+
query = "Hello #**Sweden>general";
896+
ct.get_or_set_token_for_testing("");
897+
actual_value = ct.content_typeahead_selected(
898+
{
899+
topic_display_name: get_final_topic_display_name(""),
900+
type: "topic_list",
901+
is_channel_link: false,
902+
},
903+
query,
904+
input_element,
905+
);
906+
expected_value = `Hello #**Sweden>** `;
907+
assert.equal(actual_value, expected_value);
908+
892909
ct.get_or_set_token_for_testing("");
893910
actual_value = ct.content_typeahead_selected(
894911
{
895-
topic: "Sweden",
912+
topic_display_name: "Sweden",
896913
type: "topic_list",
897914
is_channel_link: true,
898915
},
@@ -955,7 +972,15 @@ function sorted_names_from(subs) {
955972
return subs.map((sub) => sub.name).sort();
956973
}
957974

958-
const sweden_topics_to_show = ["<&>", "even more ice", "furniture", "ice", "kronor", "more ice"];
975+
const sweden_topics_to_show = [
976+
"<&>",
977+
"even more ice",
978+
"furniture",
979+
"ice",
980+
"kronor",
981+
"more ice",
982+
"",
983+
];
959984

960985
test("initialize", ({override, override_rewire, mock_template}) => {
961986
mock_banners();
@@ -1923,21 +1948,32 @@ test("begins_typeahead", ({override, override_rewire}) => {
19231948

19241949
// topic_list
19251950
// includes "more ice"
1926-
function typed_topics(topics) {
1927-
const matches_list = topics.map((topic) => ({
1928-
is_channel_link: false,
1951+
function typed_topics(stream, topics) {
1952+
const matches_list = topics.map((topic, index) => ({
1953+
is_channel_link: topic === stream && index === 0,
19291954
stream_data: {
19301955
...stream_data.get_sub_by_name("Sweden"),
19311956
rendered_description: "",
19321957
},
1933-
topic,
1958+
is_empty_string_topic: topic === get_final_topic_display_name(""),
1959+
topic_display_name: get_final_topic_display_name(topic),
19341960
type: "topic_list",
19351961
}));
19361962
return matches_list;
19371963
}
1938-
assert_typeahead_equals("#**Sweden>more ice", typed_topics(["more ice", "even more ice"]));
1939-
assert_typeahead_equals("#**Sweden>totally new topic", typed_topics(["totally new topic"]));
1940-
assert_typeahead_equals("#**Sweden>\n\nmore ice", typed_topics([]));
1964+
assert_typeahead_equals(
1965+
"#**Sweden>more ice",
1966+
typed_topics("Sweden", ["more ice", "even more ice"]),
1967+
);
1968+
assert_typeahead_equals(
1969+
"#**Sweden>",
1970+
typed_topics("Sweden", ["Sweden", ...sweden_topics_to_show]),
1971+
);
1972+
assert_typeahead_equals(
1973+
"#**Sweden>totally new topic",
1974+
typed_topics("Sweden", ["totally new topic"]),
1975+
);
1976+
assert_typeahead_equals("#**Sweden>\n\nmore ice", typed_topics("Sweden", []));
19411977

19421978
// time_jump
19431979
const time_jump = [

0 commit comments

Comments
 (0)